Completed
Push — master ( 5a501b...3982a6 )
by Gaetano
08:04
created

ReferenceMatcher::match()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 41

Duplication

Lines 16
Ratio 39.02 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 16
loc 41
ccs 0
cts 35
cp 0
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 1
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Matcher;
4
5
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
6
use Symfony\Component\Validator\Validator\ValidatorInterface;
7
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface;
8
9
/**
10
 * We abuse a bit the 'matcher' framework to set up a 'constraint' matcher which is used to tell whether a reference
11
 * matches a given condition
12
 */
13
class ReferenceMatcher extends AbstractMatcher
14
{
15
    //const MATCH_REFERENCE = 'reference';
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
16
17
    protected $allowedConditions = array(
18
        self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT,
19
        //self::MATCH_REFERENCE
20
    );
21
22
    // since this is used for recursive calls as well, we have to unlock it for now
23
    /// @todo allow this to be set in a more flexible way
24
    protected $maxConditions = 0;
25
26
    protected $validator;
27
    /** @var ReferenceResolverInterface $referenceResolver */
28
    protected $referenceResolver;
29
30
    /// @todo add more operators
31
    static protected $operatorsMap = array(
32
        'eq' => '\Symfony\Component\Validator\Constraints\EqualTo',
33
        'gt' => '\Symfony\Component\Validator\Constraints\GreaterThan',
34
        'gte' => '\Symfony\Component\Validator\Constraints\GreaterThanOrEqual',
35
        'lt' => '\Symfony\Component\Validator\Constraints\LessThan',
36
        'lte' => '\Symfony\Component\Validator\Constraints\LessThanOrEqual',
37
        'ne' => '\Symfony\Component\Validator\Constraints\NotEqualTo',
38
39
        'count' => '\Symfony\Component\Validator\Constraints\Count',
40
        'length' => '\Symfony\Component\Validator\Constraints\Length',
41
        'regex' => '\Symfony\Component\Validator\Constraints\Regex',
42
        //'in' => Operator::IN,
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
43
        //'between' => Operator::BETWEEN, => use count/length with min & max sub-members
44
        //'like' => Operator::LIKE, => use regex
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
        //'contains' => Operator::CONTAINS,
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
        Operator::EQ => '\Symfony\Component\Validator\Constraints\EqualTo',
47
        Operator::GT => '\Symfony\Component\Validator\Constraints\GreaterThan',
48
        Operator::GTE => '\Symfony\Component\Validator\Constraints\GreaterThanOrEqual',
49
        Operator::LT => '\Symfony\Component\Validator\Constraints\LessThan',
50
        Operator::LTE => '\Symfony\Component\Validator\Constraints\LessThanOrEqual',
51
        '!=' => '\Symfony\Component\Validator\Constraints\NotEqualTo',
52
        '<>' => '\Symfony\Component\Validator\Constraints\NotEqualTo',
53
    );
54
55
    public function __construct(ReferenceResolverInterface $referenceResolver, ValidatorInterface $validator)
56
    {
57
        $this->referenceResolver = $referenceResolver;
58
        $this->validator = $validator;
59
    }
60
61
    protected function validateConditions(array $conditions)
62
    {
63
        foreach ($conditions as $key => $val) {
64
            if ($this->referenceResolver->isReference($key)) {
65
                return true;
66
            }
67
        }
68
69
        return parent::validateConditions($conditions);
70
    }
71
72
    /**
73
     * @param array $conditions key: condition, value: int / string / int[] / string[]
74
     * @return array 1 element with the value true/false
75
     */
76
    public function match(array $conditions)
77
    {
78
        $this->validateConditions($conditions);
79
80
        foreach ($conditions as $key => $values) {
81
82
            switch ($key) {
83 View Code Duplication
                case self::MATCH_AND:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
84
                    foreach($values as $subCriterion) {
85
                        $value = $this->match($subCriterion);
86
                        if (!reset($value)) {
87
                            return $value;
88
                        }
89
                    }
90
                    return array(true);
91
92 View Code Duplication
                case self::MATCH_OR:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
93
                    foreach($values as $subCriterion) {
94
                        $value = $this->match($subCriterion);
95
                        if (reset($value)) {
96
                            return $value;
97
                        }
98
                    }
99
                    return array(false);
100
101
                case self::MATCH_NOT:
102
                    return array(!reset($this->match($values)));
0 ignored issues
show
Bug introduced by
$this->match($values) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
103
104
                default:
105
                    // we assume that all are refs because of the call to validate()
106
                    $currentValue = $this->referenceResolver->resolveReference($key);
107
                    $targetValue = reset($values);
108
                    $constraint = key($values);
109
                    $errorList = $this->validator->validate($currentValue, $this->getConstraint($constraint, $targetValue));
110
                    if (0 === count($errorList)) {
111
                        return array(true);
112
                    }
113
                    return array(false);
114
            }
115
        }
116
    }
117
118
    /**
119
     * @param string $constraint
120
     * @param $targetValue
121
     * @return mixed
122
     * @throws \Exception for unsupported keys
123
     */
124
    protected function getConstraint($constraint, $targetValue)
125
    {
126
        /*if (!is_array($values)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127
            $values = array($values);
128
        }*/
129
130
        if (!isset(self::$operatorsMap[$constraint])) {
131
            throw new \Exception("Matching condition '$constraint' is not supported. Supported conditions are: " .
132
                implode(', ', array_keys(self::$operatorsMap))
133
            );
134
        }
135
136
        $class = self::$operatorsMap[$constraint];
137
        return new $class($targetValue);
138
    }
139
}
140