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 | class ControllerFactory |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var \PDO PDO |
||
22 | */ |
||
23 | protected $pdo; |
||
24 | |||
25 | /** |
||
26 | * @var Configuration |
||
27 | */ |
||
28 | protected $configuration; |
||
29 | |||
30 | /** |
||
31 | * @var Request |
||
32 | */ |
||
33 | protected $request; |
||
34 | |||
35 | /** |
||
36 | * @var Entity|null User |
||
37 | */ |
||
38 | protected $user; |
||
39 | |||
40 | /** |
||
41 | * ControllerFactory constructor. |
||
42 | * |
||
43 | * @param \PDO $pdo |
||
44 | * @param Configuration $configuration |
||
45 | * @param Request $request |
||
46 | * @param Entity|null $user |
||
47 | */ |
||
48 | 1 | View Code Duplication | public function __construct(\PDO $pdo, Configuration $configuration, Request $request, Entity $user = null) |
55 | |||
56 | /** |
||
57 | * Initialize controller |
||
58 | * |
||
59 | * @param string $controller Controller class name |
||
60 | * |
||
61 | * @return Controller Controller object |
||
62 | * |
||
63 | * @throws ControllerInvalidSuperclassException |
||
64 | * @throws ControllerNonexistentException |
||
65 | */ |
||
66 | 3 | public function create(string $controller): Controller |
|
81 | } |
||
82 |
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.