Total Complexity | 44 |
Total Lines | 283 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like InRule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InRule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class InRule extends OrRule |
||
14 | { |
||
15 | /** @var string operator */ |
||
16 | const operator = 'in'; |
||
17 | |||
18 | /** @var string $field */ |
||
19 | protected $field; |
||
20 | |||
21 | /** @var array $native_possibilities */ |
||
22 | protected $native_possibilities = []; |
||
23 | |||
24 | /** @var array $cache */ |
||
25 | protected $cache = [ |
||
26 | 'array' => null, |
||
27 | 'string' => null, |
||
28 | 'semantic_id' => null, |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * @param string $field The field to apply the rule on. |
||
33 | * @param mixed $possibilities The values the field can belong to. |
||
34 | */ |
||
35 | public function __construct( $field, $possibilities, array $options=[] ) |
||
36 | { |
||
37 | if (!empty($options)) { |
||
38 | $this->setOptions($options); |
||
39 | } |
||
40 | |||
41 | $this->field = $field; |
||
42 | $this->addPossibilities( $possibilities ); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @return string The field |
||
47 | */ |
||
48 | public function getField() |
||
49 | { |
||
50 | return $this->field; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return $this |
||
55 | */ |
||
56 | public function setField($field) |
||
57 | { |
||
58 | if ($this->field != $field) { |
||
59 | $this->field = $field; |
||
60 | $this->flushCache(); |
||
61 | } |
||
62 | |||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param array|callable Associative array of renamings or callable |
||
|
|||
68 | * that would rename the fields. |
||
69 | * |
||
70 | * @return AbstractAtomicRule $this |
||
71 | */ |
||
72 | public final function renameFields($renamings) |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return array |
||
98 | */ |
||
99 | public function getPossibilities() |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param mixed possibilities |
||
106 | * |
||
107 | * @return InRule $this |
||
108 | */ |
||
109 | public function addPossibilities($possibilities) |
||
110 | { |
||
111 | if ( is_object($possibilities) |
||
112 | && $possibilities instanceof \IteratorAggregate |
||
113 | && method_exists($possibilities, 'toArray') |
||
114 | ) { |
||
115 | $possibilities = $possibilities->toArray(); |
||
116 | } |
||
117 | |||
118 | if (!is_array($possibilities)) |
||
119 | $possibilities = [$possibilities]; |
||
120 | |||
121 | $possibilities = array_map([$this, 'checkOperandAndExtractValue'], $possibilities); |
||
122 | |||
123 | // unique possibilities |
||
124 | foreach ($possibilities as &$possibility) { |
||
125 | |||
126 | if (is_scalar($possibility)) |
||
127 | $id = hash('crc32b', $possibility); |
||
128 | else |
||
129 | $id = hash('crc32b', serialize($possibility)); |
||
130 | |||
131 | if (!isset($this->native_possibilities[ $id ])) { |
||
132 | $this->native_possibilities[ $id ] = $possibility; |
||
133 | $require_cache_flush = true; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | if (isset($require_cache_flush)) |
||
138 | $this->flushCache(); |
||
139 | |||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param array possibilities |
||
145 | * |
||
146 | * @return InRule $this |
||
147 | */ |
||
148 | public function addOperand( AbstractRule $operand ) |
||
149 | { |
||
150 | $this->addPossibilities([$operand->getValue()]); |
||
151 | |||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param mixed possibilities |
||
157 | * |
||
158 | * @return InRule $this |
||
159 | */ |
||
160 | public function setPossibilities($possibilities) |
||
161 | { |
||
162 | $this->native_possibilities = []; |
||
163 | $this->addPossibilities($possibilities); |
||
164 | $this->flushCache(); |
||
165 | |||
166 | return $this; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param array possibilities |
||
171 | * |
||
172 | * @return InRule $this |
||
173 | */ |
||
174 | public function setOperands(array $operands) |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * |
||
183 | */ |
||
184 | protected function checkOperandAndExtractValue($operand) |
||
185 | { |
||
186 | if (! $operand instanceof AbstractAtomicRule) |
||
187 | return $operand; |
||
188 | |||
189 | if ( ! ($operand instanceof EqualRule && $operand->getField() == $this->field) ) { |
||
190 | throw new \InvalidArgumentException( |
||
191 | "Trying to set an invalid operand of an InRule: " |
||
192 | .var_export($operand, true) |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | return $operand->getValue(); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return InRule $this |
||
201 | */ |
||
202 | public function getOperands() |
||
203 | { |
||
204 | if (!empty($this->cache['operands'])) { |
||
205 | return $this->cache['operands']; |
||
206 | } |
||
207 | |||
208 | $operands = []; |
||
209 | foreach ($this->native_possibilities as $value) |
||
210 | $operands[] = new EqualRule($this->field, $value); |
||
211 | |||
212 | return $this->cache['operands'] = $operands; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * @return array |
||
217 | */ |
||
218 | public function getValues() |
||
219 | { |
||
220 | return $this->getPossibilities(); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param array $options + show_instance=false Display the operator of the rule or its instance id |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | public function toArray(array $options=[]) |
||
229 | { |
||
230 | $default_options = [ |
||
231 | 'show_instance' => false, |
||
232 | ]; |
||
233 | foreach ($default_options as $default_option => &$default_value) { |
||
234 | if (!isset($options[ $default_option ])) |
||
235 | $options[ $default_option ] = $default_value; |
||
236 | } |
||
237 | |||
238 | $class = get_class($this); |
||
239 | |||
240 | if (!$options['show_instance'] && isset($this->cache['array'])) |
||
241 | return $this->cache['array']; |
||
242 | |||
243 | $array = [ |
||
244 | $this->getField(), |
||
245 | $options['show_instance'] ? $this->getInstanceId() : $class::operator, |
||
246 | $this->getValues(), |
||
247 | ]; |
||
248 | |||
249 | if (!$options['show_instance']) |
||
250 | return $this->cache['array'] = $array; |
||
251 | else |
||
252 | return $array; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | */ |
||
257 | public function toString(array $options=[]) |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | */ |
||
273 | public function isNormalizationAllowed(array $contextual_options) |
||
274 | { |
||
275 | if (($threshold = $this->getOption('in.normalization_threshold', $contextual_options)) === null) { |
||
276 | return false; |
||
277 | } |
||
278 | |||
279 | return count($this->native_possibilities) <= $threshold; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @return bool If the InRule can have a solution or not |
||
284 | */ |
||
285 | public function hasSolution(array $contextual_options=[]) |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * There is no negations into an InRule |
||
292 | */ |
||
293 | public function removeNegations(array $contextual_options) |
||
296 | } |
||
297 | |||
298 | /**/ |
||
299 | } |
||
300 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths