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 |
||
| 13 | class InRule extends OrRule |
||
| 14 | { |
||
| 15 | use Trait_RuleWithField; |
||
| 16 | |||
| 17 | /** @var string operator */ |
||
| 18 | const operator = 'in'; |
||
| 19 | |||
| 20 | /** @var array $native_possibilities */ |
||
| 21 | protected $native_possibilities = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param string $field The field to apply the rule on. |
||
| 25 | * @param mixed $possibilities The values the field can belong to. |
||
| 26 | */ |
||
| 27 | 81 | public function __construct($field, $possibilities, array $options=[]) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * @return array |
||
| 39 | */ |
||
| 40 | 78 | public function getPossibilities() |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @param mixed possibilities |
||
| 47 | * |
||
| 48 | * @return InRule $this |
||
| 49 | */ |
||
| 50 | 81 | public function addPossibilities($possibilities) |
|
| 51 | { |
||
| 52 | 81 | if ( is_object($possibilities) |
|
| 53 | 81 | && $possibilities instanceof \IteratorAggregate |
|
| 54 | 81 | && method_exists($possibilities, 'toArray') |
|
| 55 | 81 | ) { |
|
| 56 | 2 | $possibilities = $possibilities->toArray(); |
|
|
|
|||
| 57 | 2 | } |
|
| 58 | |||
| 59 | 81 | if ( ! is_array($possibilities)) { |
|
| 60 | $possibilities = [$possibilities]; |
||
| 61 | } |
||
| 62 | |||
| 63 | 81 | $possibilities = array_map([$this, 'checkOperandAndExtractValue'], $possibilities); |
|
| 64 | |||
| 65 | // unique possibilities |
||
| 66 | 81 | foreach ($possibilities as &$possibility) { |
|
| 67 | 78 | if (is_scalar($possibility)) { |
|
| 68 | 78 | $id = hash('crc32b', $possibility); |
|
| 69 | 78 | } |
|
| 70 | else { |
||
| 71 | 8 | $id = hash('crc32b', serialize($possibility)); |
|
| 72 | } |
||
| 73 | |||
| 74 | 78 | if ( ! isset($this->native_possibilities[ $id ])) { |
|
| 75 | 78 | $this->native_possibilities[ $id ] = $possibility; |
|
| 76 | 78 | $require_cache_flush = true; |
|
| 77 | 78 | } |
|
| 78 | 81 | } |
|
| 79 | |||
| 80 | 81 | if (isset($require_cache_flush)) { |
|
| 81 | 78 | $this->flushCache(); |
|
| 82 | 78 | } |
|
| 83 | |||
| 84 | 81 | return $this; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param array possibilities |
||
| 89 | * |
||
| 90 | * @return InRule $this |
||
| 91 | */ |
||
| 92 | public function addOperand( AbstractRule $operand ) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param mixed possibilities |
||
| 101 | * |
||
| 102 | * @return InRule $this |
||
| 103 | */ |
||
| 104 | 17 | public function setPossibilities($possibilities) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param array possibilities |
||
| 115 | * |
||
| 116 | * @return InRule $this |
||
| 117 | */ |
||
| 118 | 5 | public function setOperands(array $operands) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * |
||
| 127 | */ |
||
| 128 | 78 | protected function checkOperandAndExtractValue($operand) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @return InRule $this |
||
| 146 | */ |
||
| 147 | 25 | public function getOperands() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | 77 | public function getValues() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param array $options + show_instance=false Display the operator of the rule or its instance id |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 77 | View Code Duplication | public function toArray(array $options=[]) |
| 204 | |||
| 205 | /** |
||
| 206 | */ |
||
| 207 | 1 | public function toString(array $options=[]) |
|
| 221 | |||
| 222 | /** |
||
| 223 | */ |
||
| 224 | 44 | public function isNormalizationAllowed(array $contextual_options) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @return bool If the InRule can have a solution or not |
||
| 235 | */ |
||
| 236 | 3 | public function hasSolution(array $contextual_options=[]) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * There is no negations into an InRule |
||
| 243 | */ |
||
| 244 | 32 | public function removeNegations(array $contextual_options) |
|
| 248 | |||
| 249 | /**/ |
||
| 250 | } |
||
| 251 |