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 |
||
11 | final class ObjectEnsurance extends ClassEnsurance |
||
12 | { |
||
13 | /** |
||
14 | * @var object |
||
15 | */ |
||
16 | private $object; |
||
17 | |||
18 | /** |
||
19 | * ObjectEnsurance constructor. |
||
20 | * |
||
21 | * @param $object |
||
22 | * |
||
23 | * @throws EnsuranceException |
||
24 | */ |
||
25 | public function __construct($object) |
||
35 | |||
36 | /** |
||
37 | * @param string $class |
||
38 | * |
||
39 | * @return ObjectEnsurance |
||
40 | */ |
||
41 | public function isSome(string $class): ObjectEnsurance |
||
47 | |||
48 | /** |
||
49 | * @param string $class |
||
50 | * |
||
51 | * @return ObjectEnsurance |
||
52 | */ |
||
53 | public function isNotSome(string $class): ObjectEnsurance |
||
59 | |||
60 | /** |
||
61 | * @param $class |
||
62 | * |
||
63 | * @return ObjectEnsurance |
||
64 | */ |
||
65 | View Code Duplication | public function isInstanceOf($class): ObjectEnsurance |
|
72 | |||
73 | /** |
||
74 | * @param $class |
||
75 | * |
||
76 | * @return ObjectEnsurance |
||
77 | */ |
||
78 | View Code Duplication | public function isNotInstanceOf($class): ObjectEnsurance |
|
85 | } |
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.