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 |
||
16 | abstract class Criterion implements CriterionInterface |
||
17 | { |
||
18 | /** |
||
19 | * The operator used by the Criterion. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | public $operator; |
||
24 | |||
25 | /** |
||
26 | * The value(s) matched by the criteria. |
||
27 | * |
||
28 | * @var array(int|string) |
||
29 | */ |
||
30 | public $value; |
||
31 | |||
32 | /** |
||
33 | * The target used by the criteria (field, metadata...). |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | public $target; |
||
38 | |||
39 | /** |
||
40 | * Additional value data, required by some criterions, MapLocationDistance for instance. |
||
41 | * |
||
42 | * @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion\Value |
||
43 | */ |
||
44 | public $valueData; |
||
45 | |||
46 | /** |
||
47 | * Performs operator validation based on the Criterion specifications returned by {@see getSpecifications()}. |
||
48 | * |
||
49 | * @param string|null $target The target the Criterion applies to: metadata identifier, field identifier... |
||
50 | * @param string|null $operator |
||
51 | * The operator the Criterion uses. If null is given, will default to Operator::IN if $value is an array, |
||
52 | * Operator::EQ if it is not. |
||
53 | * @param string[]|int[]|int|string $value |
||
54 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion\Value $valueData |
||
55 | * |
||
56 | * @todo Add a dedicated exception |
||
57 | * |
||
58 | * @throws \InvalidArgumentException if the provided operator isn't supported |
||
59 | */ |
||
60 | public function __construct($target, $operator, $value, Value $valueData = null) |
||
61 | { |
||
62 | if ($operator === null) { |
||
63 | $operator = is_array($value) ? Operator::IN : Operator::EQ; |
||
64 | } |
||
65 | |||
66 | $operatorFound = false; |
||
67 | |||
68 | // we loop on each specified operator. |
||
69 | // If the provided operator ain't found, an exception will be thrown at the end |
||
70 | foreach ($this->getSpecifications() as $operatorSpecifications) { |
||
71 | if ($operatorSpecifications->operator != $operator) { |
||
72 | continue; |
||
73 | } |
||
74 | $operatorFound = true; |
||
75 | |||
76 | // input format check (single/array) |
||
77 | switch ($operatorSpecifications->valueFormat) { |
||
78 | case Specifications::FORMAT_SINGLE: |
||
79 | if (is_array($value)) { |
||
80 | throw new InvalidArgumentException('The Criterion expects a single value'); |
||
81 | } |
||
82 | break; |
||
83 | |||
84 | case Specifications::FORMAT_ARRAY: |
||
85 | if (!is_array($value)) { |
||
86 | throw new InvalidArgumentException('The criterion expects an array of values'); |
||
87 | } |
||
88 | break; |
||
89 | } |
||
90 | |||
91 | // input value check |
||
92 | if ($operatorSpecifications->valueTypes !== null) { |
||
93 | $callback = $this->getValueTypeCheckCallback($operatorSpecifications->valueTypes); |
||
94 | if (!is_array($value)) { |
||
95 | $value = [$value]; |
||
96 | } |
||
97 | foreach ($value as $item) { |
||
98 | if ($callback($item) === false) { |
||
99 | throw new InvalidArgumentException('Unsupported value (' . gettype($item) . ")$item"); |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | // Operator wasn't found in the criterion specifications |
||
106 | if ($operatorFound === false) { |
||
107 | throw new InvalidArgumentException("Operator $operator isn't supported by the Criterion " . get_class($this)); |
||
108 | } |
||
109 | |||
110 | $this->operator = $operator; |
||
111 | $this->value = $value; |
||
112 | $this->target = $target; |
||
113 | $this->valueData = $valueData; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Criterion description function. |
||
118 | * |
||
119 | * Returns the combination of the Criterion's supported operator/value, |
||
120 | * as an array of eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator\Specifications objects |
||
121 | * - Operator is one supported Operator, as an Operator::* constant |
||
122 | * - ValueType is the type of input value this operator requires, either array or single |
||
123 | * - SupportedTypes is an array of types the operator will accept |
||
124 | * - ValueCountLimitation is an integer saying how many values are expected. |
||
125 | * |
||
126 | * <code> |
||
127 | * // IN and EQ are supported |
||
128 | * return [ |
||
129 | * // The EQ operator expects a single value, either as an integer or a string |
||
130 | * new Specifications( |
||
131 | * Operator::EQ, |
||
132 | * Specifications::INPUT_TYPE_SINGLE, |
||
133 | * [Specifications::INPUT_VALUE_INTEGER, Specifications::INPUT_VALUE_STRING], |
||
134 | * ), |
||
135 | * // The IN operator expects an array of values, of either integers or strings |
||
136 | * new Specifications( |
||
137 | * Operator::IN, |
||
138 | * Specifications::INPUT_TYPE_ARRAY, |
||
139 | * [Specifications::INPUT_VALUE_INTEGER, Specifications::INPUT_VALUE_STRING] |
||
140 | * ) |
||
141 | * ] |
||
142 | * </code> |
||
143 | * |
||
144 | * @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator\Specifications[] |
||
145 | */ |
||
146 | abstract public function getSpecifications(); |
||
147 | |||
148 | /** |
||
149 | * Returns a callback that checks the values types depending on the operator specifications. |
||
150 | * |
||
151 | * @param int $valueTypes The accepted values, as a bit field of Specifications::TYPE_* constants |
||
152 | * |
||
153 | * @return \Closure |
||
154 | */ |
||
155 | private function getValueTypeCheckCallback($valueTypes) |
||
180 | |||
181 | /** |
||
182 | * @deprecated since 7.2, will be removed in 8.0. Use the constructor directly instead. |
||
183 | */ |
||
184 | public static function createFromQueryBuilder($target, $operator, $value) |
||
190 | } |
||
191 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.