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 |
||
10 | View Code Duplication | class TreePathHash implements Buildable |
|
|
|||
11 | { |
||
12 | const MACRO_METHOD = 'treePathHash'; |
||
13 | |||
14 | /** |
||
15 | * @var ExtensibleClassMetadata |
||
16 | */ |
||
17 | protected $classMetadata; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $fieldName; |
||
23 | |||
24 | /** |
||
25 | * @param ExtensibleClassMetadata $classMetadata |
||
26 | * @param string $fieldName |
||
27 | */ |
||
28 | 2 | public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName) |
|
33 | |||
34 | /** |
||
35 | * Enable TreePathHash |
||
36 | */ |
||
37 | public static function enable() |
||
43 | |||
44 | /** |
||
45 | * Execute the build process |
||
46 | */ |
||
47 | 1 | public function build() |
|
53 | |||
54 | /** |
||
55 | * Return the name of the actual extension. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | 1 | public function getExtensionName() |
|
63 | } |
||
64 |
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.