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 |
||
17 | View Code Duplication | class TransporterFactory |
|
|
|||
18 | { |
||
19 | /** |
||
20 | * @var ContainerInterface |
||
21 | */ |
||
22 | protected $container; |
||
23 | |||
24 | /** |
||
25 | * @var string[] |
||
26 | */ |
||
27 | protected $transporters = array(); |
||
28 | |||
29 | /** |
||
30 | * @var string[] |
||
31 | */ |
||
32 | protected $configurations = array(); |
||
33 | |||
34 | /** |
||
35 | * @param ContainerInterface $container |
||
36 | */ |
||
37 | 3 | public function __construct(ContainerInterface $container) |
|
41 | |||
42 | /** |
||
43 | * @param string $serviceId |
||
44 | * @param string $alias |
||
45 | * @param string|bool $configuration |
||
46 | */ |
||
47 | 3 | public function addTransporter($serviceId, $alias, $configuration = false) |
|
52 | |||
53 | /** |
||
54 | * @return string[] |
||
55 | */ |
||
56 | 1 | public function getTransporters() |
|
60 | |||
61 | /** |
||
62 | * Returns transporter |
||
63 | * |
||
64 | * @param string $alias name of transporter |
||
65 | * @param array $options transporter settings |
||
66 | * @throws \InvalidArgumentException |
||
67 | * @return AbstractTransporter |
||
68 | */ |
||
69 | public function get($alias, array $options = array()) |
||
82 | |||
83 | /** |
||
84 | * Returns transporter configuration |
||
85 | * |
||
86 | * @param string $alias transporter name |
||
87 | * @return \Symfony\Component\Config\Definition\ConfigurationInterface|boolean |
||
88 | */ |
||
89 | 1 | public function configuration($alias) |
|
99 | } |
||
100 |
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.