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 |
||
8 | class GenericResolver |
||
9 | { |
||
10 | /** |
||
11 | * @var string $prefix |
||
12 | */ |
||
13 | private $prefix = ''; |
||
14 | |||
15 | /** |
||
16 | * @var string $suffix |
||
17 | */ |
||
18 | private $suffix = ''; |
||
19 | |||
20 | /** |
||
21 | * @var array $capitals |
||
22 | */ |
||
23 | private $capitals; |
||
24 | |||
25 | /** |
||
26 | * @var array $replacements |
||
27 | */ |
||
28 | private $replacements; |
||
29 | |||
30 | /** |
||
31 | * @param array $data Optional class dependencies. Will use default values if none are provided. |
||
32 | */ |
||
33 | public function __construct(array $data = null) |
||
62 | |||
63 | /** |
||
64 | * Resolver needs to be callable |
||
65 | * |
||
66 | * @param string $type The "type" of object to resolve (the object ident). |
||
67 | * @return string The resolved class name (FQN). |
||
68 | */ |
||
69 | public function __invoke($type) |
||
73 | |||
74 | /** |
||
75 | * Resolve the class name from the requested type. |
||
76 | * |
||
77 | * @param string $type The "type" of object to resolve (the object ident). |
||
78 | * @throws InvalidArgumentException If the type parameter is not a string. |
||
79 | * @return string The resolved class name (FQN). |
||
80 | */ |
||
81 | public function resolve($type) |
||
112 | } |
||
113 |
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.