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 |
||
| 28 | abstract class AbstractCollection extends Arrayy implements CollectionInterface |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | * @psalm-var array<T> |
||
| 33 | */ |
||
| 34 | protected $array = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var ArrayyRewindableGenerator|null |
||
| 38 | * @psalm-var \Arrayy\ArrayyRewindableGenerator<T>|null |
||
| 39 | */ |
||
| 40 | protected $generator; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $checkPropertyTypes = true; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $checkPropertiesMismatch = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $checkForMissingPropertiesInConstructor = true; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constructs a collection object of the specified type, optionally with the |
||
| 59 | * specified data. |
||
| 60 | * |
||
| 61 | * @param mixed $data |
||
| 62 | * <p> |
||
| 63 | * The initial items to store in the collection. |
||
| 64 | * </p> |
||
| 65 | * @param string $iteratorClass optional <p> |
||
| 66 | * You can overwrite the ArrayyIterator, but mostly you don't |
||
| 67 | * need this option. |
||
| 68 | * </p> |
||
| 69 | * @param bool $checkPropertiesInConstructor optional <p> |
||
| 70 | * You need to extend the "Arrayy"-class and you need to set |
||
| 71 | * the $checkPropertiesMismatchInConstructor class property |
||
| 72 | * to |
||
| 73 | * true, otherwise this option didn't not work anyway. |
||
| 74 | * </p> |
||
| 75 | * |
||
| 76 | * @psalm-param array<T> $data |
||
| 77 | * @psalm-param class-string<\ArrayIterator> $iteratorClass |
||
| 78 | */ |
||
| 79 | 70 | public function __construct( |
|
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | 4 | View Code Duplication | public function append($value, $key = null): Arrayy |
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | 1 | public function offsetSet($offset, $value) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritdoc} |
||
| 153 | */ |
||
| 154 | 3 | View Code Duplication | public function prepend($value, $key = null): Arrayy |
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | 1 | public function column(string $keyOrPropertyOrMethod): array |
|
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | 6 | public function getCollection(): array |
|
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | abstract public function getType(); |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | 1 | public function merge(CollectionInterface ...$collections): self |
|
| 217 | |||
| 218 | /** |
||
| 219 | * {@inheritdoc} |
||
| 220 | */ |
||
| 221 | 1 | public function where(string $keyOrPropertyOrMethod, $value): self |
|
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | 68 | protected function internalSet( |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param mixed $type |
||
| 268 | * |
||
| 269 | * @return TypeCheckArray |
||
| 270 | */ |
||
| 271 | 70 | protected static function convertIntoTypeCheckArray($type): TypeCheckArray |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Extracts the value of the given property or method from the object. |
||
| 291 | * |
||
| 292 | * @param \Arrayy\Arrayy $object <p>The object to extract the value from.</p> |
||
| 293 | * @param string $keyOrPropertyOrMethod <p>The property or method for which the |
||
| 294 | * value should be extracted.</p> |
||
| 295 | * |
||
| 296 | * @throws \InvalidArgumentException if the method or property is not defined |
||
| 297 | * |
||
| 298 | * @return mixed |
||
| 299 | * <p>The value extracted from the specified property or method.</p> |
||
| 300 | */ |
||
| 301 | 2 | private function extractValue(Arrayy $object, string $keyOrPropertyOrMethod) |
|
| 323 | } |
||
| 324 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..