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 |
||
19 | class Inject extends CollectionValue implements PropertyInterface |
||
20 | { |
||
21 | /** @var string Injectable dependency */ |
||
22 | protected $dependency; |
||
23 | 1 | ||
24 | /** |
||
25 | 1 | * Inject constructor. |
|
26 | 1 | * |
|
27 | * @param array $valueOrValues |
||
28 | */ |
||
29 | 5 | public function __construct(array $valueOrValues) |
|
39 | |||
40 | 5 | /** {@inheritdoc} */ |
|
41 | 1 | public function toPropertyMetadata(PropertyMetadata $propertyMetadata) |
|
52 | 3 | ||
53 | /** |
||
54 | * Validate dependency. |
||
55 | * |
||
56 | * @param string $type |
||
57 | * @param string $dependency |
||
58 | * @param string $namespace |
||
59 | */ |
||
60 | protected function validate(&$type, &$dependency, $namespace) |
||
83 | 4 | ||
84 | /** |
||
85 | * Build full class name. |
||
86 | * |
||
87 | * @param string $className Full or short class name |
||
88 | * @param string $namespace Name space |
||
89 | * |
||
90 | * @return string Full class name |
||
91 | */ |
||
92 | View Code Duplication | protected function buildFullClassName($className, $namespace) |
|
101 | |||
102 | /** |
||
103 | * Check if @Inject violates inheritance. |
||
104 | * |
||
105 | * @param string $type Property/Parameter type |
||
106 | * @param string $dependency @Inject value |
||
107 | * |
||
108 | * @return bool True if @Inject violates inheritance |
||
109 | */ |
||
110 | protected function checkInheritanceViolation($type, $dependency) : bool |
||
124 | |||
125 | /** |
||
126 | * Check if @Inject has no class name and type hint is interface. |
||
127 | * |
||
128 | * @param string $type Property/Parameter type |
||
129 | * @param string $dependency @Inject value |
||
130 | * |
||
131 | * @return bool True if @Inject has no class name and type hint is interface. |
||
132 | */ |
||
133 | protected function checkInterfaceWithoutClassName($type, $dependency) : bool |
||
139 | } |
||
140 |
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.