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 implements PropertyInterface |
||
20 | { |
||
21 | /** @var string Injectable dependency */ |
||
22 | protected $dependency; |
||
23 | |||
24 | /** |
||
25 | * Inject constructor. |
||
26 | * |
||
27 | * @param array $valueOrValues |
||
28 | */ |
||
29 | 16 | public function __construct(array $valueOrValues) |
|
40 | |||
41 | /** {@inheritdoc} */ |
||
42 | 16 | public function toPropertyMetadata(PropertyMetadata $propertyMetadata) |
|
53 | |||
54 | /** |
||
55 | * Validate dependency. |
||
56 | * |
||
57 | * @param string $type |
||
58 | * @param string $dependency |
||
59 | * @param string $namespace |
||
60 | */ |
||
61 | 16 | protected function validate(&$type, &$dependency, $namespace) |
|
62 | { |
||
63 | //$dependency = $this->buildFullClassName($dependency, $namespace); |
||
64 | 16 | $type = $this->buildFullClassName($type, $namespace); |
|
65 | |||
66 | // // Check for inheritance violation |
||
67 | // if ($this->checkInheritanceViolation($type, $dependency)) { |
||
68 | // throw new \InvalidArgumentException( |
||
69 | // '@Inject dependency violates ' . $type . ' inheritance with ' . $dependency |
||
70 | // ); |
||
71 | // } |
||
72 | // |
||
73 | // if ($this->checkInterfaceWithoutClassName($type, $dependency)) { |
||
74 | // throw new \InvalidArgumentException( |
||
75 | // 'Cannot @Inject interface, inherited class name should be specified |
||
76 | // '); |
||
77 | // } |
||
78 | |||
79 | // Empty @Inject with type hint - use type hine as dependency |
||
80 | 16 | if ($dependency === null && $type !== null) { |
|
81 | 12 | $dependency = $type; |
|
82 | } |
||
83 | 16 | } |
|
84 | |||
85 | /** |
||
86 | * Build full class name. |
||
87 | * |
||
88 | * @param string $className Full or short class name |
||
89 | * @param string $namespace Name space |
||
90 | * |
||
91 | * @return string Full class name |
||
92 | */ |
||
93 | 16 | View Code Duplication | protected function buildFullClassName($className, $namespace) |
102 | |||
103 | /** |
||
104 | * Check if @Inject violates inheritance. |
||
105 | * |
||
106 | * @param string $type Property/Parameter type |
||
107 | * @param string $dependency @Inject value |
||
108 | * |
||
109 | * @return bool True if @Inject violates inheritance |
||
110 | */ |
||
111 | protected function checkInheritanceViolation($type, $dependency) : bool |
||
125 | |||
126 | /** |
||
127 | * Check if @Inject has no class name and type hint is interface. |
||
128 | * |
||
129 | * @param string $type Property/Parameter type |
||
130 | * @param string $dependency @Inject value |
||
131 | * |
||
132 | * @return bool True if @Inject has no class name and type hint is interface. |
||
133 | */ |
||
134 | protected function checkInterfaceWithoutClassName($type, $dependency) : bool |
||
140 | } |
||
141 |
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.