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 |
||
| 9 | class QueryBuilderRequest extends Request |
||
| 10 | { |
||
| 11 | private static $includesArrayValueDelimiter = ','; |
||
| 12 | private static $appendsArrayValueDelimiter = ','; |
||
| 13 | private static $fieldsArrayValueDelimiter = ','; |
||
| 14 | private static $sortsArrayValueDelimiter = ','; |
||
| 15 | private static $filterArrayValueDelimiter = ','; |
||
| 16 | |||
| 17 | public static function setArrayValueDelimiter(string $delimiter): void |
||
| 18 | { |
||
| 19 | static::$filterArrayValueDelimiter = $delimiter; |
||
|
|
|||
| 20 | static::$includesArrayValueDelimiter = $delimiter; |
||
| 21 | static::$appendsArrayValueDelimiter = $delimiter; |
||
| 22 | static::$fieldsArrayValueDelimiter = $delimiter; |
||
| 23 | static::$sortsArrayValueDelimiter = $delimiter; |
||
| 24 | } |
||
| 25 | |||
| 26 | public static function fromRequest(Request $request): self |
||
| 27 | { |
||
| 28 | return static::createFrom($request, new self()); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function includes(): Collection |
||
| 32 | { |
||
| 33 | $includeParameterName = config('query-builder.parameters.include'); |
||
| 34 | |||
| 35 | $includeParts = $this->query($includeParameterName); |
||
| 36 | |||
| 37 | if (!is_array($includeParts)) { |
||
| 38 | $includeParts = explode(static::getIncludesArrayValueDelimiter(), $this->query($includeParameterName)); |
||
| 39 | } |
||
| 40 | |||
| 41 | return collect($includeParts) |
||
| 42 | ->filter() |
||
| 43 | ->map([Str::class, 'camel']); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function appends(): Collection |
||
| 47 | { |
||
| 48 | $appendParameterName = config('query-builder.parameters.append'); |
||
| 49 | |||
| 50 | $appendParts = $this->query($appendParameterName); |
||
| 51 | |||
| 52 | if (!is_array($appendParts)) { |
||
| 53 | $appendParts = explode(static::getAppendsArrayValueDelimiter(), strtolower($appendParts)); |
||
| 54 | } |
||
| 55 | |||
| 56 | return collect($appendParts)->filter(); |
||
| 57 | } |
||
| 58 | |||
| 59 | View Code Duplication | public function fields(): Collection |
|
| 73 | |||
| 74 | View Code Duplication | public function sorts(): Collection |
|
| 86 | |||
| 87 | public function filters(): Collection |
||
| 88 | { |
||
| 89 | $filterParameterName = config('query-builder.parameters.filter'); |
||
| 90 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @param $value |
||
| 106 | * |
||
| 107 | * @return array|bool |
||
| 108 | */ |
||
| 109 | protected function getFilterValue($value) |
||
| 131 | |||
| 132 | public static function setIncludesArrayValueDelimiter(string $includesArrayValueDelimiter): void |
||
| 136 | |||
| 137 | public static function setAppendsArrayValueDelimiter(string $appendsArrayValueDelimiter): void |
||
| 141 | |||
| 142 | public static function setFieldsArrayValueDelimiter(string $fieldsArrayValueDelimiter): void |
||
| 146 | |||
| 147 | public static function setSortsArrayValueDelimiter(string $sortsArrayValueDelimiter): void |
||
| 151 | |||
| 152 | public static function setFilterArrayValueDelimiter(string $filterArrayValueDelimiter): void |
||
| 156 | |||
| 157 | public static function getIncludesArrayValueDelimiter(): string |
||
| 161 | |||
| 162 | public static function getAppendsArrayValueDelimiter(): string |
||
| 166 | |||
| 167 | public static function getFieldsArrayValueDelimiter(): string |
||
| 171 | |||
| 172 | public static function getSortsArrayValueDelimiter(): string |
||
| 176 | |||
| 177 | public static function getFilterArrayValueDelimiter(): string |
||
| 181 | } |
||
| 182 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: