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 | abstract class AbstractProcessor implements ConfigurationProcessorInterface |
||
15 | { |
||
16 | /** |
||
17 | * Pattern of "resource" parameter of link annotation |
||
18 | */ |
||
19 | const RESOURCE_PATTERN = '~^(?<repository>[a-z_][a-z0-9_]*)\.(?<link>[a-z_][a-z0-9_]*)$~i'; |
||
20 | |||
21 | /** |
||
22 | * Create link |
||
23 | * |
||
24 | * @param string $name |
||
25 | * @param array $data |
||
26 | * @return Link |
||
27 | */ |
||
28 | 2 | public function createLink(string $name, array $data): Link |
|
45 | |||
46 | /** |
||
47 | * Resolve getter of related object |
||
48 | * |
||
49 | * @param \ReflectionClass $reflection |
||
50 | * @param string $name |
||
51 | * @return string |
||
52 | */ |
||
53 | protected function resolveGetter(\ReflectionClass $reflection, string $name) |
||
70 | |||
71 | /** |
||
72 | * Resolve getter of related object |
||
73 | * |
||
74 | * @param \ReflectionClass $reflection |
||
75 | * @param string $name |
||
76 | * @return string | null |
||
77 | */ |
||
78 | 16 | protected function resolveSetter(\ReflectionClass $reflection, string $name) |
|
86 | } |
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.