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 |
||
| 30 | abstract class AbstractCollection extends Arrayy implements CollectionInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | * @phpstan-var array<T> |
||
| 35 | */ |
||
| 36 | protected $array = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var ArrayyRewindableGenerator|null |
||
| 40 | * @phpstan-var \Arrayy\ArrayyRewindableGenerator<TKey,T>|null |
||
| 41 | */ |
||
| 42 | protected $generator; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $checkPropertyTypes = true; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected $checkPropertiesMismatch = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $checkForMissingPropertiesInConstructor = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Constructs a collection object of the specified type, optionally with the |
||
| 61 | * specified data. |
||
| 62 | * |
||
| 63 | * @param mixed $data |
||
| 64 | * <p> |
||
| 65 | * The initial items to store in the collection. |
||
| 66 | * </p> |
||
| 67 | * @param string $iteratorClass optional <p> |
||
| 68 | * You can overwrite the ArrayyIterator, but mostly you don't |
||
| 69 | * need this option. |
||
| 70 | * </p> |
||
| 71 | * @param bool $checkPropertiesInConstructor optional <p> |
||
| 72 | * You need to extend the "Arrayy"-class and you need to set |
||
| 73 | * the $checkPropertiesMismatchInConstructor class property |
||
| 74 | * to |
||
| 75 | * true, otherwise this option didn't not work anyway. |
||
| 76 | * </p> |
||
| 77 | * |
||
| 78 | * @phpstan-param array<TKey,T>|\Arrayy\Arrayy<TKey,T>|\Closure():array<TKey,T>|mixed $data |
||
| 79 | * @phpstan-param class-string<\Arrayy\ArrayyIterator> $iteratorClass |
||
| 80 | */ |
||
| 81 | 100 | public function __construct( |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Append a (key) + value to the current array. |
||
| 112 | * |
||
| 113 | * @param mixed $value |
||
| 114 | * @param mixed $key |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | * <p>(Mutable) Return this CollectionInterface object, with the appended values.</p> |
||
| 118 | * |
||
| 119 | * @phpstan-param T|static $value |
||
| 120 | * @phpstan-param TKey|null $key |
||
| 121 | * @phpstan-return static<TKey,T> |
||
| 122 | */ |
||
| 123 | 6 | View Code Duplication | public function append($value, $key = null): Arrayy |
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | 2 | public function offsetSet($offset, $value) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Prepend a (key) + value to the current array. |
||
| 167 | * |
||
| 168 | * @param mixed $value |
||
| 169 | * @param mixed $key |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | * <p>(Mutable) Return this CollectionInterface object, with the prepended value.</p> |
||
| 173 | * |
||
| 174 | * @phpstan-param T|static $value |
||
| 175 | * @phpstan-param TKey|null $key |
||
| 176 | * @phpstan-return static<TKey,T> |
||
| 177 | */ |
||
| 178 | 3 | View Code Duplication | public function prepend($value, $key = null): Arrayy |
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | */ |
||
| 203 | 1 | public function column(string $keyOrPropertyOrMethod): array |
|
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritdoc} |
||
| 217 | */ |
||
| 218 | 7 | public function getCollection(): array |
|
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | abstract public function getType(); |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Merge current items and items of given collections into a new one. |
||
| 230 | * |
||
| 231 | * @param CollectionInterface|static ...$collections |
||
| 232 | * <p>The collections to merge.</p> |
||
| 233 | * |
||
| 234 | *@throws \InvalidArgumentException if any of the given collections are not of the same type |
||
| 235 | * |
||
| 236 | * @return $this |
||
| 237 | * |
||
| 238 | * @phpstan-param CollectionInterface<TKey,T> ...$collections |
||
| 239 | * @phpstan-return static<TKey,T> |
||
| 240 | */ |
||
| 241 | 1 | public function merge(CollectionInterface ...$collections): self |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Creates an CollectionInterface object. |
||
| 254 | * |
||
| 255 | * @param mixed $data |
||
| 256 | * @param string $iteratorClass |
||
| 257 | * @param bool $checkPropertiesInConstructor |
||
| 258 | * |
||
| 259 | * @return static |
||
| 260 | * <p>(Immutable) Returns an new instance of the CollectionInterface object.</p> |
||
| 261 | * |
||
| 262 | * @template TKeyCreate as int|string |
||
| 263 | * @template TCreate |
||
| 264 | * |
||
| 265 | * @phpstan-param array<TKeyCreate,TCreate> $data |
||
| 266 | * @phpstan-param class-string<\Arrayy\ArrayyIterator> $iteratorClass |
||
| 267 | * @phpstan-return static<TKeyCreate,TCreate> |
||
| 268 | * |
||
| 269 | * @psalm-mutation-free |
||
| 270 | */ |
||
| 271 | 26 | public static function create( |
|
| 272 | $data = [], |
||
| 273 | string $iteratorClass = ArrayyIterator::class, |
||
| 274 | bool $checkPropertiesInConstructor = true |
||
| 275 | ) { |
||
| 276 | 26 | return new static( |
|
| 277 | 26 | $data, |
|
| 278 | $iteratorClass, |
||
| 279 | $checkPropertiesInConstructor |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param string $json |
||
| 285 | * |
||
| 286 | * @return static |
||
| 287 | * <p>(Immutable) Returns an new instance of the CollectionInterface object.</p> |
||
| 288 | * |
||
| 289 | * @phpstan-return static<int,T> |
||
| 290 | * |
||
| 291 | * @psalm-mutation-free |
||
| 292 | */ |
||
| 293 | 7 | public static function createFromJsonMapper(string $json) |
|
| 294 | { |
||
| 295 | // init |
||
| 296 | 7 | $return = static::create(); |
|
| 297 | 7 | $jsonObject = \json_decode($json, false); |
|
| 298 | 7 | $mapper = new \Arrayy\Mapper\Json(); |
|
| 299 | 1 | View Code Duplication | $mapper->undefinedPropertyHandler = static function ($object, $key, $jsonValue) use ($return) { |
| 300 | 1 | if ($return->checkForMissingPropertiesInConstructor) { |
|
| 301 | 1 | throw new \TypeError('Property mismatch - input: ' . \print_r(['key' => $key, 'jsonValue' => $jsonValue], true) . ' for object: ' . \get_class($object)); |
|
| 302 | } |
||
| 303 | }; |
||
| 304 | |||
| 305 | 7 | $type = $return->getType(); |
|
| 306 | |||
| 307 | if ( |
||
| 308 | 7 | \is_string($type) |
|
| 309 | && |
||
| 310 | 7 | \class_exists($type) |
|
| 311 | ) { |
||
| 312 | 3 | if (\is_array($jsonObject)) { |
|
| 313 | 1 | foreach ($jsonObject as $jsonObjectSingle) { |
|
| 314 | 1 | $collectionData = $mapper->map($jsonObjectSingle, $type); |
|
| 315 | 1 | $return->add($collectionData); |
|
| 316 | } |
||
| 317 | } else { |
||
| 318 | 2 | $collectionData = $mapper->map($jsonObject, $type); |
|
| 319 | 2 | $return->add($collectionData); |
|
| 320 | } |
||
| 321 | } else { |
||
| 322 | 4 | foreach ($jsonObject as $key => $jsonValue) { |
|
| 323 | 4 | $return->add($jsonValue, $key); |
|
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** @phpstan-var static<int,T> */ |
||
| 328 | 4 | return $return; |
|
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Internal mechanic of set method. |
||
| 333 | * |
||
| 334 | * @param int|string|null $key |
||
| 335 | * @param mixed $value |
||
| 336 | * @param bool $checkProperties |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | 93 | protected function internalSet( |
|
| 341 | $key, |
||
| 342 | &$value, |
||
| 343 | bool $checkProperties = true |
||
| 344 | ): bool { |
||
| 345 | if ( |
||
| 346 | 93 | $value instanceof self |
|
| 347 | && |
||
| 348 | 93 | !$value instanceof TypeInterface |
|
| 349 | ) { |
||
| 350 | foreach ($value as $valueTmp) { |
||
| 351 | parent::internalSet( |
||
| 352 | $key, |
||
| 353 | $valueTmp, |
||
| 354 | $checkProperties |
||
| 355 | ); |
||
| 356 | } |
||
| 357 | |||
| 358 | return true; |
||
| 359 | } |
||
| 360 | |||
| 361 | 93 | return parent::internalSet( |
|
| 362 | 93 | $key, |
|
| 363 | $value, |
||
| 364 | $checkProperties |
||
| 365 | ); |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param string|string[]|TypeCheckArray|TypeCheckInterface[]|null $type |
||
| 370 | * |
||
| 371 | * @return TypeCheckArray |
||
| 372 | * |
||
| 373 | * @phpstan-param null|string|string[]|class-string|class-string[]|TypeCheckArray<array-key,TypeCheckInterface>|array<array-key,TypeCheckInterface>|mixed $type |
||
| 374 | * @phpstan-return TypeCheckArray<array-key,TypeCheckInterface> |
||
| 375 | */ |
||
| 376 | 100 | protected static function convertIntoTypeCheckArray($type): TypeCheckArray |
|
| 394 | } |
||
| 395 |
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..