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 | final class Structure |
||
21 | { |
||
22 | /** @var string[] */ |
||
23 | public $constants = []; |
||
24 | |||
25 | /** @var ClassMethod[] */ |
||
26 | public $methods = []; |
||
27 | |||
28 | /** @var bool */ |
||
29 | public $hasClone; |
||
30 | |||
31 | /** @var SplObjectStorage */ |
||
32 | private $properties; |
||
33 | |||
34 | /** |
||
35 | * Structure constructor. |
||
36 | */ |
||
37 | protected function __construct() |
||
40 | |||
41 | /** |
||
42 | * @param array $constants |
||
43 | * @param SplObjectStorage $properties |
||
44 | * @param bool $hasClone |
||
45 | * @param ClassMethod ...$methods |
||
46 | * @return Structure |
||
47 | */ |
||
48 | public static function create( |
||
62 | |||
63 | /** |
||
64 | * A list of properties to be unset due to they are initialized (have a default value). |
||
65 | * @return string[] |
||
66 | */ |
||
67 | View Code Duplication | public function toBeUnsetProperties(): array |
|
79 | |||
80 | /** |
||
81 | * A list of public properties. Any access to them be proxied. |
||
82 | * @return string[] |
||
83 | */ |
||
84 | View Code Duplication | public function publicProperties(): array |
|
96 | |||
97 | /** |
||
98 | * @return string[] |
||
99 | */ |
||
100 | public function properties(): array |
||
110 | |||
111 | /** |
||
112 | * @return array |
||
113 | */ |
||
114 | public function methodNames(): array |
||
123 | |||
124 | /** |
||
125 | * Since php7.4.1 the behaviour changed as it was before php7.4.0. All properties should be unset. |
||
126 | * @see https://github.com/php/php-src/pull/4974 |
||
127 | * @param ReflectionProperty $property |
||
128 | * @return bool |
||
129 | */ |
||
130 | private function doesDefaultMatter(ReflectionProperty $property): bool |
||
134 | } |
||
135 |
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.