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 |
||
| 14 | class Collection implements Contract\Struct\Collection, Contract\Data\ValueObject { |
||
| 15 | use Mixin\StructureOfElementsMethods; |
||
| 16 | use Mixin\CountableMethods; |
||
| 17 | use Mixin\IteratorMethods; |
||
| 18 | use Mixin\JsonSerializableMethods; |
||
| 19 | use Properties; |
||
| 20 | |||
| 21 | public function __construct($elements = []) { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Named constructor. |
||
| 27 | * |
||
| 28 | * @param array|iterable $elements elements of collection. |
||
| 29 | * |
||
| 30 | * @return Collection new collection of specified elements. |
||
| 31 | * |
||
| 32 | * @since 1.0 |
||
| 33 | */ |
||
| 34 | public static function of($elements): Contract\Struct\Collection { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Add element to the end of the collection. |
||
| 40 | * |
||
| 41 | * @param mixed $element element to add. |
||
| 42 | * |
||
| 43 | * @since 1.0 |
||
| 44 | */ |
||
| 45 | public function add($element): void { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Push one or more elements onto the end of collection. |
||
| 51 | * Example: |
||
| 52 | * ```php |
||
| 53 | * $collection->push(1, 2, 3, 4); |
||
| 54 | * ``` |
||
| 55 | * |
||
| 56 | * @param array ...$elements elements. |
||
| 57 | * |
||
| 58 | * @return int the new number of elements in the array. |
||
| 59 | * |
||
| 60 | * @since 1.0 |
||
| 61 | */ |
||
|
|
|||
| 62 | public function push(...$elements): int { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Pad collection to the specified length with a value. |
||
| 68 | * |
||
| 69 | * @param int $size new size of the collection. If size is |
||
| 70 | * positive then the collection is padded on the right, if it's negative then |
||
| 71 | * on the left. If the absolute value of size is less than or equal to |
||
| 72 | * the length of the collection then no padding takes place. |
||
| 73 | * @param mixed $value Value to pad if input is less than `size`. |
||
| 74 | * |
||
| 75 | * @since 1.0 |
||
| 76 | */ |
||
| 77 | public function pad(int $size, $value = null): void { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Split collection into chunks of specified size. |
||
| 83 | * |
||
| 84 | * @param int $size size of each chunk. |
||
| 85 | * |
||
| 86 | * @return Collection a multidimensional numerically indexed collection, starting with zero, |
||
| 87 | * with each dimension containing collection of specified size. |
||
| 88 | * |
||
| 89 | * @since 1.0 |
||
| 90 | */ |
||
| 91 | View Code Duplication | public function chunkBy(int $size): Contract\Struct\Collection { |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Split collection into chunks of specified size keeping original indexes. |
||
| 103 | * |
||
| 104 | * @param int $size size of each chunk. |
||
| 105 | * |
||
| 106 | * @return Collection a multidimensional numerically indexed collection, starting with zero, |
||
| 107 | * with each dimension containing collection of specified size and keys equal to original keys |
||
| 108 | * of elements. |
||
| 109 | * |
||
| 110 | * @since 1.0 |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function chunkKeepingKeysBy(int $size): Contract\Struct\Collection { |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Gets keys of the collection. |
||
| 124 | * |
||
| 125 | * @return Collection collection of the collection keys. |
||
| 126 | */ |
||
| 127 | public function getKeys(): Contract\Struct\Collection { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Gets values of the collection. |
||
| 133 | * |
||
| 134 | * @return Contract\Struct\Collection collection of the collection keys. |
||
| 135 | */ |
||
| 136 | public function getValues(): Contract\Struct\Collection { |
||
| 139 | |||
| 140 | ///endregion |
||
| 141 | /// |
||
| 142 | /// region ----------------- ARRAY ACCESS METHODS ----------------- |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Whether a offset exists |
||
| 146 | * |
||
| 147 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 148 | * |
||
| 149 | * @param mixed $offset An offset to check for. |
||
| 150 | * |
||
| 151 | * @return boolean true on success or false on failure. |
||
| 152 | * The return value will be casted to boolean if non-boolean was returned. |
||
| 153 | * |
||
| 154 | * @since 1.0 |
||
| 155 | */ |
||
| 156 | public function offsetExists($offset) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Offset to retrieve |
||
| 162 | * |
||
| 163 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 164 | * |
||
| 165 | * @param mixed $offset the offset to retrieve. |
||
| 166 | * |
||
| 167 | * @return mixed Can return all value types. |
||
| 168 | * |
||
| 169 | * @since 1.0 |
||
| 170 | */ |
||
| 171 | public function offsetGet($offset) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Offset to set |
||
| 177 | * |
||
| 178 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 179 | * |
||
| 180 | * @param mixed $offset The offset to assign the value to. |
||
| 181 | * @param mixed $value the value to set. |
||
| 182 | * |
||
| 183 | * @return void |
||
| 184 | * |
||
| 185 | * @since 1.0 |
||
| 186 | */ |
||
| 187 | public function offsetSet($offset, $value) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Offset to unset |
||
| 197 | * |
||
| 198 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 199 | * |
||
| 200 | * @param mixed $offset the offset to unset. |
||
| 201 | * |
||
| 202 | * @return void |
||
| 203 | * |
||
| 204 | * @since 1.0 |
||
| 205 | */ |
||
| 206 | public function offsetUnset($offset) { |
||
| 209 | /// endregion |
||
| 210 | } |