Completed
Push — master ( 05840b...9b2a73 )
by Richard
05:47
created

_Or   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 55%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 42
loc 42
ccs 11
cts 20
cp 0.55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A _compile() 12 12 3
A compile_to_source() 13 13 2
A for_types() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received 
8
 * a copy of the license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Graph\Predicate;
12
13
use Lechimp\Dicto\Graph\Entity;
14
15
/**
16
 * A predicate that is true if any of its subpredicates are true.
17
 */
18 View Code Duplication
class _Or extends _Combined {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 43
    public function compile_to_source(array &$custom_closures) {
39
        $code =
40 43
            "    while(true) {\n";
41 43
        foreach ($this->predicates as $predicate) {
42
            $code .=
43 43
                 $predicate->compile_to_source($custom_closures).
44 43
            "    if (\$value) break;\n";
45 43
        }
46
        return
47
            $code.
48 43
            "break;\n".
49 43
            "}\n";
50
    }
51
52
    /**
53
     * @inheritdocs
54
     */
55 33
    public function for_types(array $existing_types) {
56 33
        $tss = $this->for_types_of_predicates($existing_types);
57 33
        return array_values(array_unique(call_user_func_array("array_merge", $tss)));
58
    }
59
}
60