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 |
||
24 | class ArticleFactory implements ArticleFactoryInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var FactoryInterface |
||
28 | */ |
||
29 | private $baseFactory; |
||
30 | |||
31 | /** |
||
32 | * @var RouteProviderInterface |
||
33 | */ |
||
34 | private $routeProvider; |
||
35 | |||
36 | /** |
||
37 | * @var ArticleProviderInterface |
||
38 | */ |
||
39 | private $articleProvider; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $contentRelativePath; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $allowedTypes = [ |
||
50 | ItemInterface::TYPE_PICTURE, |
||
51 | ItemInterface::TYPE_FILE, |
||
52 | ItemInterface::TYPE_TEXT, |
||
53 | ItemInterface::TYPE_COMPOSITE, |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * ArticleFactory constructor. |
||
58 | * |
||
59 | * @param FactoryInterface $baseFactory |
||
60 | * @param RouteProviderInterface $routeProvider |
||
61 | * @param ArticleProviderInterface $articleProvider |
||
62 | * @param string $contentRelativePath |
||
63 | */ |
||
64 | 13 | public function __construct( |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 13 | public function create() |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 13 | public function createFromPackage(PackageInterface $package) |
|
108 | |||
109 | 13 | View Code Duplication | private function populateLead(PackageInterface $package) |
117 | |||
118 | View Code Duplication | private function populateBody(PackageInterface $package) |
|
126 | |||
127 | 11 | private function ensureTypeIsAllowed($type) |
|
137 | } |
||
138 |
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.