Code Duplication    Length = 42-42 lines in 2 locations

src/Graph/Predicate/_And.php 1 location

@@ 18-59 (lines=42) @@
15
/**
16
 * A predicate that is true if all of its subpredicates are true.
17
 */
18
class _And extends _Combined {
19
    /**
20
     * @inheritdocs
21
     */
22
    public function _compile() {
23
        $compiled = $this->compiled_predicates();
24
25
        return function(Entity $e) use ($compiled) { 
26
            foreach ($compiled as $predicate) {
27
                if (!$predicate($e)) {
28
                    return false;
29
                }
30
            }
31
            return true;
32
        };
33
    }
34
35
    /**
36
     * @inheritdocs
37
     */
38
    public function compile_to_source(array &$custom_closures) {
39
        $code =
40
            "    while(true) {\n";
41
        foreach ($this->predicates as $predicate) {
42
            $code .=
43
                 $predicate->compile_to_source($custom_closures).
44
            "    if (!\$value) break;\n";
45
        }
46
        return
47
            $code.
48
            "break;\n".
49
            "}\n";
50
    }
51
52
    /**
53
     * @inheritdocs
54
     */
55
    public function for_types(array $existing_types) {
56
        $tss = $this->for_types_of_predicates($existing_types);
57
        return array_values(call_user_func_array("array_intersect", $tss));
58
    }
59
}
60

src/Graph/Predicate/_Or.php 1 location

@@ 18-59 (lines=42) @@
15
/**
16
 * A predicate that is true if any of its subpredicates are true.
17
 */
18
class _Or extends _Combined {
19
    /**
20
     * @inheritdocs
21
     */
22
    public function _compile() {
23
        $compiled = $this->compiled_predicates();
24
25
        return function(Entity $e) use ($compiled) { 
26
            foreach ($compiled as $predicate) {
27
                if ($predicate($e)) {
28
                    return true;
29
                }
30
            }
31
            return false;
32
        };
33
    }
34
35
    /**
36
     * @inheritdocs
37
     */
38
    public function compile_to_source(array &$custom_closures) {
39
        $code =
40
            "    while(true) {\n";
41
        foreach ($this->predicates as $predicate) {
42
            $code .=
43
                 $predicate->compile_to_source($custom_closures).
44
            "    if (\$value) break;\n";
45
        }
46
        return
47
            $code.
48
            "break;\n".
49
            "}\n";
50
    }
51
52
    /**
53
     * @inheritdocs
54
     */
55
    public function for_types(array $existing_types) {
56
        $tss = $this->for_types_of_predicates($existing_types);
57
        return array_values(array_unique(call_user_func_array("array_merge", $tss)));
58
    }
59
}
60