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 AttributeProcessor extends AbstractProcessor |
||
15 | { |
||
16 | /** |
||
17 | * Pattern of "type" parameter of attribute annotation |
||
18 | */ |
||
19 | const DATATYPE_PATTERN = '~^(?<type>[a-z_][a-z0-9_]*(?:\.[a-z_][a-z0-9_]*)*)\s*(?:\((?<params>[^\)]*)\))?(?<many>\[\])?$~i'; |
||
20 | |||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | 17 | View Code Duplication | public function process(array $config, Definition $definition) |
39 | |||
40 | /** |
||
41 | * Create attribute |
||
42 | * |
||
43 | * @param string $name |
||
44 | * @param array $data |
||
45 | * @return Attribute |
||
46 | */ |
||
47 | 17 | protected function createAttribute(\ReflectionClass $reflection, string $name, array $data): Attribute |
|
67 | |||
68 | /** |
||
69 | * Process optional properties of attribute |
||
70 | * |
||
71 | * @param array $data |
||
72 | * @param Attribute $attribute |
||
73 | */ |
||
74 | 17 | protected function processAttributeOptions(array $data, Attribute $attribute) |
|
88 | |||
89 | /** |
||
90 | * Process data-type |
||
91 | * |
||
92 | * @param string $definition |
||
93 | * @param Attribute $attribute |
||
94 | */ |
||
95 | 17 | View Code Duplication | protected function processDataType(string $definition, Attribute $attribute) |
116 | } |
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.