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 |
||
23 | final class FactoryParser |
||
24 | { |
||
25 | /** |
||
26 | * @var ResolverInterface |
||
27 | */ |
||
28 | private $serviceResolver; |
||
29 | |||
30 | 44 | public function __construct(ResolverInterface $serviceResolver) |
|
34 | |||
35 | /** |
||
36 | * Parses a factory service definition and return the factory object. |
||
37 | * |
||
38 | * @param ServiceInterface $service |
||
39 | * @param mixed $factory |
||
40 | * @param string $fileName file name |
||
41 | * |
||
42 | * @return ServiceInterface |
||
43 | * @throws InvalidArgumentException |
||
44 | */ |
||
45 | 2 | public function parse(ServiceInterface $service, $factory, $fileName) |
|
51 | |||
52 | /** |
||
53 | * @param ServiceInterface $service |
||
54 | * @param mixed $factory |
||
55 | * @param string $fileName file name |
||
56 | * |
||
57 | * @throws InvalidArgumentException |
||
58 | */ |
||
59 | 2 | private function checkFactory(ServiceInterface $service, $factory, $fileName) |
|
83 | |||
84 | /** |
||
85 | * @param ServiceInterface $service |
||
86 | * @param mixed $factoryClass |
||
87 | * @param mixed $factoryMethod |
||
88 | * @param string $fileName file name |
||
89 | * |
||
90 | * @return FactoryInterface |
||
91 | * @throws InvalidArgumentException |
||
92 | */ |
||
93 | 2 | private function parseFactory(ServiceInterface $service, $factoryClass, $factoryMethod, $fileName) |
|
120 | } |
||
121 |
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.