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 |
||
5 | View Code Duplication | class Rule |
|
|
|||
6 | { |
||
7 | const TYPE_OBJECT = 'object'; |
||
8 | |||
9 | const TYPE_SCALAR = 'scalar'; |
||
10 | |||
11 | const TYPE_CUSTOM = 'custom-validator'; |
||
12 | |||
13 | private static $avalableRuleTypes = [ |
||
14 | self::TYPE_OBJECT, |
||
15 | self::TYPE_SCALAR, |
||
16 | self::TYPE_CUSTOM, |
||
17 | ]; |
||
18 | |||
19 | private $rule; |
||
20 | |||
21 | private function __construct(array $rule) |
||
25 | |||
26 | public static function fromArray(array $rule) |
||
36 | |||
37 | public function ensureRuleNameIsValid() |
||
47 | |||
48 | public function asArray() : array |
||
52 | |||
53 | public function isValid() |
||
57 | |||
58 | public function is($type) |
||
62 | |||
63 | public function isNot($type) |
||
67 | |||
68 | public function isCustom() |
||
72 | |||
73 | public function isNotCustom() |
||
77 | |||
78 | public function isNotMail() |
||
82 | |||
83 | public function getRuleType() |
||
87 | |||
88 | public function isObject() |
||
92 | |||
93 | public function getObjectType() |
||
97 | |||
98 | public function getValue() |
||
102 | |||
103 | public function getExpectedType() |
||
113 | |||
114 | public function isValueNotAnObject() |
||
118 | } |
||
119 |
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.