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 |
||
| 20 | abstract class AbstractCollection extends Arrayy implements CollectionInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | protected $checkPropertyTypes = true; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $checkPropertiesMismatch = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | protected $checkForMissingPropertiesInConstructor = true; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Constructs a collection object of the specified type, optionally with the |
||
| 39 | * specified data. |
||
| 40 | * |
||
| 41 | * @param mixed $data |
||
| 42 | * <p> |
||
| 43 | * The initial items to store in the collection. |
||
| 44 | * </p> |
||
| 45 | * @param string $iteratorClass optional <p> |
||
| 46 | * You can overwrite the ArrayyIterator, but mostly you don't |
||
| 47 | * need this option. |
||
| 48 | * </p> |
||
| 49 | * @param bool $checkPropertiesInConstructor optional <p> |
||
| 50 | * You need to extend the "Arrayy"-class and you need to set |
||
| 51 | * the $checkPropertiesMismatchInConstructor class property |
||
| 52 | * to |
||
| 53 | * true, otherwise this option didn't not work anyway. |
||
| 54 | * </p> |
||
| 55 | */ |
||
| 56 | 67 | public function __construct( |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @return static[] |
||
| 87 | */ |
||
| 88 | 6 | public function getCollection(): array |
|
| 92 | |||
| 93 | /** |
||
| 94 | * The type (FQCN) associated with this collection. |
||
| 95 | * |
||
| 96 | * @return string|string[]|TypeCheckArray|TypeCheckInterface[] |
||
| 97 | */ |
||
| 98 | abstract public function getType(); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Merge current items and items of given collections into a new one. |
||
| 102 | * |
||
| 103 | * @param CollectionInterface ...$collections The collections to merge. |
||
| 104 | * |
||
| 105 | * @throws \InvalidArgumentException if any of the given collections are not of the same type |
||
| 106 | * |
||
| 107 | * @return static |
||
| 108 | */ |
||
| 109 | 1 | public function merge(CollectionInterface ...$collections): CollectionInterface |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Assigns a value to the specified offset + check the type. |
||
| 124 | * |
||
| 125 | * @param int|string|null $offset |
||
| 126 | * @param mixed $value |
||
| 127 | */ |
||
| 128 | 1 | public function offsetSet($offset, $value) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Prepend a (key) + value to the current array. |
||
| 147 | * |
||
| 148 | * @param mixed $value |
||
| 149 | * @param mixed $key |
||
| 150 | * |
||
| 151 | * @return static |
||
| 152 | * <p>(Mutable) Return this Arrayy object, with the prepended value.</p> |
||
| 153 | */ |
||
| 154 | 3 | View Code Duplication | public function prepend($value, $key = null): Arrayy |
| 170 | |||
| 171 | /** |
||
| 172 | * Append a (key) + value to the current array. |
||
| 173 | * |
||
| 174 | * @param mixed $value |
||
| 175 | * @param mixed $key |
||
| 176 | * |
||
| 177 | * @return static |
||
| 178 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
| 179 | */ |
||
| 180 | 4 | View Code Duplication | public function append($value, $key = null): Arrayy |
| 196 | |||
| 197 | /** |
||
| 198 | * Returns the values from given property or method. |
||
| 199 | * |
||
| 200 | * @param string $keyOrPropertyOrMethod the property or method name to filter by |
||
| 201 | * |
||
| 202 | * @throws \InvalidArgumentException if property or method is not defined |
||
| 203 | * |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | 1 | public function column(string $keyOrPropertyOrMethod): array |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Returns a collection of matching items. |
||
| 220 | * |
||
| 221 | * @param string $keyOrPropertyOrMethod the property or method to evaluate |
||
| 222 | * @param mixed $value the value to match |
||
| 223 | * |
||
| 224 | * @throws \InvalidArgumentException if property or method is not defined |
||
| 225 | * |
||
| 226 | * @return static |
||
| 227 | */ |
||
| 228 | 1 | public function where(string $keyOrPropertyOrMethod, $value): CollectionInterface |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Internal mechanic of set method. |
||
| 244 | * |
||
| 245 | * @param string|null $key |
||
| 246 | * @param mixed $value |
||
| 247 | * @param bool $checkPropertiesAndType |
||
| 248 | * |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | 65 | View Code Duplication | protected function internalSet($key, &$value, $checkPropertiesAndType = true): bool |
| 275 | |||
| 276 | /** |
||
| 277 | * @param mixed $type |
||
| 278 | * |
||
| 279 | * @return TypeCheckArray |
||
| 280 | */ |
||
| 281 | 67 | protected static function convertIntoTypeCheckArray($type): TypeCheckArray |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Extracts the value of the given property or method from the object. |
||
| 301 | * |
||
| 302 | * @param Arrayy $object the object to extract the value from |
||
| 303 | * @param string $keyOrPropertyOrMethod the property or method for which the |
||
| 304 | * value should be extracted |
||
| 305 | * |
||
| 306 | * @throws \InvalidArgumentException if the method or property is not defined |
||
| 307 | * |
||
| 308 | * @return mixed the value extracted from the specified property or method |
||
| 309 | */ |
||
| 310 | 2 | private function extractValue(Arrayy $object, string $keyOrPropertyOrMethod) |
|
| 332 | } |
||
| 333 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.