Passed
Push — master ( 8fa25b...9eb080 )
by Jean
02:43
created
src/Rule/AbstractOperationRule.php 2 patches
Spacing   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -20,7 +20,7 @@  discard block
 block discarded – undo
20 20
     const remove_negations        = 'remove_negations';
21 21
     const rootify_disjunctions    = 'rootify_disjunctions';
22 22
     const unify_atomic_operands   = 'unify_atomic_operands';
23
-    const remove_invalid_branches = 'remove_invalid_branches';    // simplified after this step
23
+    const remove_invalid_branches = 'remove_invalid_branches'; // simplified after this step
24 24
 
25 25
     const simplified              = self::remove_invalid_branches;
26 26
 
@@ -43,9 +43,9 @@  discard block
 block discarded – undo
43 43
 
44 44
     /**
45 45
      */
46
-    public function __construct( array $operands=[] )
46
+    public function __construct(array $operands = [])
47 47
     {
48
-        $this->setOperands( $operands );
48
+        $this->setOperands($operands);
49 49
         $this->flushCache();
50 50
     }
51 51
 
@@ -64,10 +64,10 @@  discard block
 block discarded – undo
64 64
      *
65 65
      * @return $this
66 66
      */
67
-    public function addOperand( AbstractRule $new_operand )
67
+    public function addOperand(AbstractRule $new_operand)
68 68
     {
69
-        if (!isset($this->operands[ $id = $new_operand->getSemanticId() ])) {
70
-            $this->operands[ $id ] = $new_operand;
69
+        if (!isset($this->operands[$id = $new_operand->getSemanticId()])) {
70
+            $this->operands[$id] = $new_operand;
71 71
 
72 72
             if ($this->current_simplification_step)
73 73
                 $this->current_simplification_step = null;
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
      */
84 84
     public function getOperands()
85 85
     {
86
-        return array_values( $this->operands );
86
+        return array_values($this->operands);
87 87
     }
88 88
 
89 89
     /**
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
      * @param array  $simplification_options
126 126
      * @param bool   $force
127 127
      */
128
-    public function moveSimplificationStepForward($step_to_go_to, array $simplification_options, $force=false)
128
+    public function moveSimplificationStepForward($step_to_go_to, array $simplification_options, $force = false)
129 129
     {
130 130
         if (!in_array($step_to_go_to, self::simplification_steps)) {
131 131
             throw new \InvalidArgumentException(
@@ -137,18 +137,18 @@  discard block
 block discarded – undo
137 137
         if (!$force && $this->current_simplification_step !== null) {
138 138
             $steps_indices = array_flip(self::simplification_steps);
139 139
 
140
-            $current_index = $steps_indices[ $this->current_simplification_step ];
141
-            $target_index  = $steps_indices[ $step_to_go_to ];
140
+            $current_index = $steps_indices[$this->current_simplification_step];
141
+            $target_index  = $steps_indices[$step_to_go_to];
142 142
 
143
-            if ( $current_index >= $target_index ) {
143
+            if ($current_index >= $target_index) {
144 144
                 // allow recall of previous step without going back
145 145
                 return;
146 146
             }
147
-            elseif ( $current_index < $target_index - 1 ) {
147
+            elseif ($current_index < $target_index - 1) {
148 148
                 throw new \LogicException(
149
-                    "$step_to_go_to MUST be fullfilled after " . self::simplification_steps[$target_index - 1]
150
-                    . " instead of the current step: " . $this->current_simplification_step
151
-                    ."\nfor: " . $this
149
+                    "$step_to_go_to MUST be fullfilled after ".self::simplification_steps[$target_index - 1]
150
+                    . " instead of the current step: ".$this->current_simplification_step
151
+                    ."\nfor: ".$this
152 152
                 );
153 153
             }
154 154
         }
@@ -176,8 +176,8 @@  discard block
 block discarded – undo
176 176
 
177 177
         $steps_indices = array_flip(self::simplification_steps);
178 178
 
179
-        $current_index = $steps_indices[ $this->current_simplification_step ];
180
-        $step_index    = $steps_indices[ $step ];
179
+        $current_index = $steps_indices[$this->current_simplification_step];
180
+        $step_index    = $steps_indices[$step];
181 181
 
182 182
         return $current_index >= $step_index;
183 183
     }
@@ -204,7 +204,7 @@  discard block
 block discarded – undo
204 204
                 }
205 205
 
206 206
                 if ($operands[$i] instanceof AbstractOperationRule) {
207
-                    $operands[$i]->removeNegations( $contextual_options );
207
+                    $operands[$i]->removeNegations($contextual_options);
208 208
                 }
209 209
             }
210 210
 
@@ -224,10 +224,10 @@  discard block
 block discarded – undo
224 224
      *
225 225
      * @return $this;
226 226
      */
227
-    public function cleanOperations($simplification_options, $recurse=true)
227
+    public function cleanOperations($simplification_options, $recurse = true)
228 228
     {
229 229
         if ($recurse) foreach ($this->operands as $i => $operand) {
230
-            if (    $operand instanceof AbstractOperationRule
230
+            if ($operand instanceof AbstractOperationRule
231 231
                 && !$operand instanceof InRule
232 232
                 && !$operand instanceof NotEqualRule
233 233
                 && !$operand instanceof NotInRule
@@ -296,7 +296,7 @@  discard block
 block discarded – undo
296 296
     public function unifyAtomicOperands($simplification_strategy_step = false, array $contextual_options)
297 297
     {
298 298
         if ($simplification_strategy_step)
299
-            $this->moveSimplificationStepForward( self::unify_atomic_operands, $contextual_options );
299
+            $this->moveSimplificationStepForward(self::unify_atomic_operands, $contextual_options);
300 300
 
301 301
         // $this->dump(true);
302 302
 
@@ -330,7 +330,7 @@  discard block
 block discarded – undo
330 330
                 catch (\Exception $e) {
331 331
                     VisibilityViolator::setHiddenProperty(
332 332
                         $e, 'message',
333
-                        $e->getMessage() . "\n" . var_export($operandsByOperator, true)
333
+                        $e->getMessage()."\n".var_export($operandsByOperator, true)
334 334
                     );
335 335
 
336 336
                     throw $e;
@@ -338,7 +338,7 @@  discard block
 block discarded – undo
338 338
             }
339 339
         }
340 340
 
341
-        return $this->setOperandsOrReplaceByOperation( $unifiedOperands, $contextual_options );
341
+        return $this->setOperandsOrReplaceByOperation($unifiedOperands, $contextual_options);
342 342
     }
343 343
 
344 344
     private static $simplification_cache = [];
@@ -354,10 +354,10 @@  discard block
 block discarded – undo
354 354
      *
355 355
      * @return AbstractRule the simplified rule
356 356
      */
357
-    final public function simplify($options=[])
357
+    final public function simplify($options = [])
358 358
     {
359
-        $step_to_stop_before = !empty($options['stop_before'])        ? $options['stop_before'] : null;
360
-        $step_to_stop_after  = !empty($options['stop_after'])         ? $options['stop_after']  : null;
359
+        $step_to_stop_before = !empty($options['stop_before']) ? $options['stop_before'] : null;
360
+        $step_to_stop_after  = !empty($options['stop_after']) ? $options['stop_after'] : null;
361 361
         $force_logical_core  = !empty($options['force_logical_core']) ? $options['force_logical_core'] : false;
362 362
 
363 363
         if ($step_to_stop_before && !in_array($step_to_stop_before, self::simplification_steps)) {
@@ -392,8 +392,8 @@  discard block
 block discarded – undo
392 392
 
393 393
         // $instance->dump(true);
394 394
 
395
-        if ($step_to_stop_after  == self::remove_negations ||
396
-            $step_to_stop_before == self::rootify_disjunctions )
395
+        if ($step_to_stop_after == self::remove_negations ||
396
+            $step_to_stop_before == self::rootify_disjunctions)
397 397
             return $instance;
398 398
 
399 399
         // $instance->dump(true);
@@ -405,8 +405,8 @@  discard block
 block discarded – undo
405 405
 
406 406
         // $instance->dump(true);
407 407
 
408
-        if ($step_to_stop_after  == self::rootify_disjunctions ||
409
-            $step_to_stop_before == self::unify_atomic_operands )
408
+        if ($step_to_stop_after == self::rootify_disjunctions ||
409
+            $step_to_stop_before == self::unify_atomic_operands)
410 410
             return $instance;
411 411
 
412 412
         if (!$instance instanceof AbstractAtomicRule) {
@@ -416,8 +416,8 @@  discard block
 block discarded – undo
416 416
 
417 417
             // $instance->dump(true);
418 418
 
419
-            if ($step_to_stop_after  == self::unify_atomic_operands ||
420
-                $step_to_stop_before == self::remove_invalid_branches )
419
+            if ($step_to_stop_after == self::unify_atomic_operands ||
420
+                $step_to_stop_before == self::remove_invalid_branches)
421 421
                 return $instance;
422 422
 
423 423
             $instance->cleanOperations($options);
@@ -433,7 +433,7 @@  discard block
 block discarded – undo
433 433
         // last non recursive clean
434 434
         // TODO kind of monad|become|cese
435 435
         //@see https://github.com/jclaveau/php-logical-filter/issues/20
436
-        if ($instance instanceof AndRule || $instance instanceof OrRule ) {
436
+        if ($instance instanceof AndRule || $instance instanceof OrRule) {
437 437
 
438 438
             if (!$instance->getOperands())
439 439
                 return $instance;
@@ -472,7 +472,7 @@  discard block
 block discarded – undo
472 472
 
473 473
         $cache_keys[] = $instance->getSemanticId().'-'.$options_id;
474 474
         foreach ($cache_keys as $cache_key) {
475
-            self::$simplification_cache[ $cache_key ] = $instance;
475
+            self::$simplification_cache[$cache_key] = $instance;
476 476
         }
477 477
 
478 478
         return $instance->copy();
@@ -495,13 +495,13 @@  discard block
 block discarded – undo
495 495
             // For FilteredValue and FilteredKey
496 496
             $field = (string) $field;
497 497
 
498
-            if (!isset($operandsByFields[ $field ]))
499
-                $operandsByFields[ $field ] = [];
498
+            if (!isset($operandsByFields[$field]))
499
+                $operandsByFields[$field] = [];
500 500
 
501
-            if (!isset($operandsByFields[ $field ][ $operand::operator ]))
502
-                $operandsByFields[ $field ][ $operand::operator ] = [];
501
+            if (!isset($operandsByFields[$field][$operand::operator]))
502
+                $operandsByFields[$field][$operand::operator] = [];
503 503
 
504
-            $operandsByFields[ $field ][ $operand::operator ][] = $operand;
504
+            $operandsByFields[$field][$operand::operator][] = $operand;
505 505
         }
506 506
 
507 507
         return $operandsByFields;
@@ -524,13 +524,13 @@  discard block
 block discarded – undo
524 524
             // For FilteredValue and FilteredKey
525 525
             $field = (string) $field;
526 526
 
527
-            if (!isset($operandsByFields[ $field ]))
528
-                $operandsByFields[ $field ] = [];
527
+            if (!isset($operandsByFields[$field]))
528
+                $operandsByFields[$field] = [];
529 529
 
530
-            if (!isset($operandsByFields[ $field ][ $operand::operator ]))
531
-                $operandsByFields[ $field ][ $operand::operator ] = [];
530
+            if (!isset($operandsByFields[$field][$operand::operator]))
531
+                $operandsByFields[$field][$operand::operator] = [];
532 532
 
533
-            $operandsByFields[ $field ][ $operand::operator ][] = $operand;
533
+            $operandsByFields[$field][$operand::operator][] = $operand;
534 534
         }
535 535
 
536 536
         return $operandsByFields;
@@ -568,7 +568,7 @@  discard block
 block discarded – undo
568 568
      *
569 569
      * @return AbstractRule|null The operand if it exists or null
570 570
      */
571
-    protected function getOperandAt($index=0)
571
+    protected function getOperandAt($index = 0)
572 572
     {
573 573
         $operands = array_values($this->operands);
574 574
         if (isset($operands[$index]))
Please login to merge, or discard this patch.
Braces   +74 added lines, -53 removed lines patch added patch discarded remove patch
@@ -69,8 +69,9 @@  discard block
 block discarded – undo
69 69
         if (!isset($this->operands[ $id = $new_operand->getSemanticId() ])) {
70 70
             $this->operands[ $id ] = $new_operand;
71 71
 
72
-            if ($this->current_simplification_step)
73
-                $this->current_simplification_step = null;
72
+            if ($this->current_simplification_step) {
73
+                            $this->current_simplification_step = null;
74
+            }
74 75
 
75 76
             $this->flushCache();
76 77
         }
@@ -108,10 +109,11 @@  discard block
 block discarded – undo
108 109
     public function renameFields($renamings)
109 110
     {
110 111
         foreach ($this->operands as $operand) {
111
-            if (method_exists($operand, 'renameField'))
112
-                $operand->renameField($renamings);
113
-            else
114
-                $operand->renameFields($renamings);
112
+            if (method_exists($operand, 'renameField')) {
113
+                            $operand->renameField($renamings);
114
+            } else {
115
+                            $operand->renameFields($renamings);
116
+            }
115 117
         }
116 118
 
117 119
         // TODO flush cache only in case of change?
@@ -143,8 +145,7 @@  discard block
 block discarded – undo
143 145
             if ( $current_index >= $target_index ) {
144 146
                 // allow recall of previous step without going back
145 147
                 return;
146
-            }
147
-            elseif ( $current_index < $target_index - 1 ) {
148
+            } elseif ( $current_index < $target_index - 1 ) {
148 149
                 throw new \LogicException(
149 150
                     "$step_to_go_to MUST be fullfilled after " . self::simplification_steps[$target_index - 1]
150 151
                     . " instead of the current step: " . $this->current_simplification_step
@@ -171,8 +172,9 @@  discard block
 block discarded – undo
171 172
             );
172 173
         }
173 174
 
174
-        if ($this->current_simplification_step === null)
175
-            return false;
175
+        if ($this->current_simplification_step === null) {
176
+                    return false;
177
+        }
176 178
 
177 179
         $steps_indices = array_flip(self::simplification_steps);
178 180
 
@@ -189,8 +191,9 @@  discard block
 block discarded – undo
189 191
      */
190 192
     public function removeNegations(array $contextual_options)
191 193
     {
192
-        if (!$this->isNormalizationAllowed($contextual_options))
193
-            return $this;
194
+        if (!$this->isNormalizationAllowed($contextual_options)) {
195
+                    return $this;
196
+        }
194 197
 
195 198
         $this->moveSimplificationStepForward(self::remove_negations, $contextual_options);
196 199
 
@@ -226,28 +229,33 @@  discard block
 block discarded – undo
226 229
      */
227 230
     public function cleanOperations($simplification_options, $recurse=true)
228 231
     {
229
-        if ($recurse) foreach ($this->operands as $i => $operand) {
232
+        if ($recurse) {
233
+            foreach ($this->operands as $i => $operand) {
230 234
             if (    $operand instanceof AbstractOperationRule
231 235
                 && !$operand instanceof InRule
232 236
                 && !$operand instanceof NotEqualRule
233 237
                 && !$operand instanceof NotInRule
234 238
             ) {
235 239
                 $this->operands[$i] = $operand->cleanOperations($simplification_options);
240
+        }
236 241
             }
237 242
         }
238 243
 
239
-        if ($this instanceof NotRule)
240
-            return $this;
244
+        if ($this instanceof NotRule) {
245
+                    return $this;
246
+        }
241 247
 
242 248
         $is_modified = true;
243 249
         while ($is_modified) {
244 250
             $is_modified = false;
245 251
 
246
-            if ($this->removeMonooperandOperationsOperands($simplification_options))
247
-                $is_modified = true;
252
+            if ($this->removeMonooperandOperationsOperands($simplification_options)) {
253
+                            $is_modified = true;
254
+            }
248 255
 
249
-            if ($this->removeSameOperationOperands($simplification_options))
250
-                $is_modified = true;
256
+            if ($this->removeSameOperationOperands($simplification_options)) {
257
+                            $is_modified = true;
258
+            }
251 259
         }
252 260
 
253 261
         return $this;
@@ -265,13 +273,13 @@  discard block
 block discarded – undo
265 273
     public function removeMonooperandOperationsOperands(array $simplification_options)
266 274
     {
267 275
         foreach ($this->operands as $i => $operand) {
268
-            if (!$operand instanceof AbstractOperationRule || $operand instanceof NotRule)
269
-                continue;
276
+            if (!$operand instanceof AbstractOperationRule || $operand instanceof NotRule) {
277
+                            continue;
278
+            }
270 279
 
271 280
             if ($operand instanceof InRule && !$operand->isNormalizationAllowed($simplification_options)) {
272 281
                 $count = count($operand->getPossibilities());
273
-            }
274
-            else {
282
+            } else {
275 283
                 $count = count($operand->getOperands());
276 284
             }
277 285
 
@@ -295,13 +303,15 @@  discard block
 block discarded – undo
295 303
      */
296 304
     public function unifyAtomicOperands($simplification_strategy_step = false, array $contextual_options)
297 305
     {
298
-        if ($simplification_strategy_step)
299
-            $this->moveSimplificationStepForward( self::unify_atomic_operands, $contextual_options );
306
+        if ($simplification_strategy_step) {
307
+                    $this->moveSimplificationStepForward( self::unify_atomic_operands, $contextual_options );
308
+        }
300 309
 
301 310
         // $this->dump(true);
302 311
 
303
-        if (!$this->isNormalizationAllowed($contextual_options))
304
-            return $this;
312
+        if (!$this->isNormalizationAllowed($contextual_options)) {
313
+                    return $this;
314
+        }
305 315
 
306 316
         $operands = $this->getOperands();
307 317
         foreach ($operands as &$operand) {
@@ -326,8 +336,7 @@  discard block
 block discarded – undo
326 336
             foreach ($operandsByOperator as $operator => $operands) {
327 337
                 try {
328 338
                     $unifiedOperands = array_merge($unifiedOperands, $operands);
329
-                }
330
-                catch (\Exception $e) {
339
+                } catch (\Exception $e) {
331 340
                     VisibilityViolator::setHiddenProperty(
332 341
                         $e, 'message',
333 342
                         $e->getMessage() . "\n" . var_export($operandsByOperator, true)
@@ -370,8 +379,9 @@  discard block
 block discarded – undo
370 379
         $options_id = hash('md4', serialize($options));
371 380
 
372 381
         $id = $this->getSemanticId().'-'.$options_id;
373
-        if (isset(self::$simplification_cache[$id]))
374
-            return self::$simplification_cache[$id]->copy();
382
+        if (isset(self::$simplification_cache[$id])) {
383
+                    return self::$simplification_cache[$id]->copy();
384
+        }
375 385
 
376 386
         $this->flushCache();
377 387
 
@@ -384,8 +394,9 @@  discard block
 block discarded – undo
384 394
 
385 395
         $cache_keys[] = $instance->getSemanticId().'-'.$options_id;
386 396
 
387
-        if ($step_to_stop_before == self::remove_negations)
388
-            return $instance;
397
+        if ($step_to_stop_before == self::remove_negations) {
398
+                    return $instance;
399
+        }
389 400
 
390 401
         // $this->dump(!true);
391 402
         $instance = $instance->removeNegations($options);
@@ -393,8 +404,9 @@  discard block
 block discarded – undo
393 404
         // $instance->dump(true);
394 405
 
395 406
         if ($step_to_stop_after  == self::remove_negations ||
396
-            $step_to_stop_before == self::rootify_disjunctions )
397
-            return $instance;
407
+            $step_to_stop_before == self::rootify_disjunctions ) {
408
+                    return $instance;
409
+        }
398 410
 
399 411
         // $instance->dump(true);
400 412
 
@@ -406,8 +418,9 @@  discard block
 block discarded – undo
406 418
         // $instance->dump(true);
407 419
 
408 420
         if ($step_to_stop_after  == self::rootify_disjunctions ||
409
-            $step_to_stop_before == self::unify_atomic_operands )
410
-            return $instance;
421
+            $step_to_stop_before == self::unify_atomic_operands ) {
422
+                    return $instance;
423
+        }
411 424
 
412 425
         if (!$instance instanceof AbstractAtomicRule) {
413 426
 
@@ -417,8 +430,9 @@  discard block
 block discarded – undo
417 430
             // $instance->dump(true);
418 431
 
419 432
             if ($step_to_stop_after  == self::unify_atomic_operands ||
420
-                $step_to_stop_before == self::remove_invalid_branches )
421
-                return $instance;
433
+                $step_to_stop_before == self::remove_invalid_branches ) {
434
+                            return $instance;
435
+            }
422 436
 
423 437
             $instance->cleanOperations($options);
424 438
             if (method_exists($instance, 'removeInvalidBranches')) {
@@ -435,16 +449,18 @@  discard block
 block discarded – undo
435 449
         //@see https://github.com/jclaveau/php-logical-filter/issues/20
436 450
         if ($instance instanceof AndRule || $instance instanceof OrRule ) {
437 451
 
438
-            if (!$instance->getOperands())
439
-                return $instance;
452
+            if (!$instance->getOperands()) {
453
+                            return $instance;
454
+            }
440 455
 
441 456
             $operands = (new AndRule([$instance]))
442 457
                 ->cleanOperations($options, false)
443 458
                 // ->dump(true)
444 459
                 ->getOperands();
445 460
 
446
-            if (count($operands) == 1)
447
-                $instance = reset($operands);
461
+            if (count($operands) == 1) {
462
+                            $instance = reset($operands);
463
+            }
448 464
         }
449 465
 
450 466
 
@@ -495,11 +511,13 @@  discard block
 block discarded – undo
495 511
             // For FilteredValue and FilteredKey
496 512
             $field = (string) $field;
497 513
 
498
-            if (!isset($operandsByFields[ $field ]))
499
-                $operandsByFields[ $field ] = [];
514
+            if (!isset($operandsByFields[ $field ])) {
515
+                            $operandsByFields[ $field ] = [];
516
+            }
500 517
 
501
-            if (!isset($operandsByFields[ $field ][ $operand::operator ]))
502
-                $operandsByFields[ $field ][ $operand::operator ] = [];
518
+            if (!isset($operandsByFields[ $field ][ $operand::operator ])) {
519
+                            $operandsByFields[ $field ][ $operand::operator ] = [];
520
+            }
503 521
 
504 522
             $operandsByFields[ $field ][ $operand::operator ][] = $operand;
505 523
         }
@@ -524,11 +542,13 @@  discard block
 block discarded – undo
524 542
             // For FilteredValue and FilteredKey
525 543
             $field = (string) $field;
526 544
 
527
-            if (!isset($operandsByFields[ $field ]))
528
-                $operandsByFields[ $field ] = [];
545
+            if (!isset($operandsByFields[ $field ])) {
546
+                            $operandsByFields[ $field ] = [];
547
+            }
529 548
 
530
-            if (!isset($operandsByFields[ $field ][ $operand::operator ]))
531
-                $operandsByFields[ $field ][ $operand::operator ] = [];
549
+            if (!isset($operandsByFields[ $field ][ $operand::operator ])) {
550
+                            $operandsByFields[ $field ][ $operand::operator ] = [];
551
+            }
532 552
 
533 553
             $operandsByFields[ $field ][ $operand::operator ][] = $operand;
534 554
         }
@@ -571,8 +591,9 @@  discard block
 block discarded – undo
571 591
     protected function getOperandAt($index=0)
572 592
     {
573 593
         $operands = array_values($this->operands);
574
-        if (isset($operands[$index]))
575
-            return $operands[$index];
594
+        if (isset($operands[$index])) {
595
+                    return $operands[$index];
596
+        }
576 597
     }
577 598
 
578 599
     /**/
Please login to merge, or discard this patch.