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 |
||
13 | final class InstanceHelper extends ReflectionHelperAbstract |
||
14 | { |
||
15 | /** |
||
16 | * @param mixed $object |
||
17 | * |
||
18 | * @return ReflectionProperty[] |
||
19 | */ |
||
20 | 8 | public static function getReflectionProperties($object): array |
|
24 | |||
25 | /** |
||
26 | * @return mixed |
||
27 | */ |
||
28 | 1 | public static function createInstance(string $fqcn, array $constructorArguments = []) |
|
34 | |||
35 | /** |
||
36 | * @return mixed |
||
37 | */ |
||
38 | 3 | public static function createInstanceWithoutConstructor(string $fqcn) |
|
44 | |||
45 | 3 | View Code Duplication | public static function setProtectedProperty($object, string $propertyName, $value) |
53 | |||
54 | 3 | View Code Duplication | public static function getProtectedProperty($object, string $propertyName) |
63 | |||
64 | /** |
||
65 | * @param callable $input Can be a callable array (method defaults to '__construct'), callable object or Closure |
||
66 | * |
||
67 | * @throws InvalidArgumentException |
||
68 | * |
||
69 | * @return array [index => [name, class]] |
||
70 | */ |
||
71 | 4 | View Code Duplication | public static function getParameters($input): array |
88 | |||
89 | /** |
||
90 | * @throws InvalidArgumentException |
||
91 | * |
||
92 | * @return ReflectionFunction|ReflectionMethod |
||
93 | */ |
||
94 | 4 | private static function getCallableReflectionMethod($input) |
|
111 | } |
||
112 |
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.