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 | abstract class ValueObject |
||
12 | { |
||
13 | /** |
||
14 | * Construct object optionally with a set of properties. |
||
15 | * |
||
16 | * Readonly properties values must be set using $properties as they are not writable anymore |
||
17 | * after object has been created. |
||
18 | * |
||
19 | * @param array $properties |
||
20 | * |
||
21 | * @throws \Marek\Toggable\API\Exception\PropertyNotFoundException |
||
22 | */ |
||
23 | 387 | public function __construct(array $properties = array()) |
|
33 | |||
34 | |||
35 | /** |
||
36 | * @param $property |
||
37 | * |
||
38 | * @return mixed |
||
39 | * |
||
40 | * @throws \Marek\Toggable\API\Exception\PropertyNotFoundException |
||
41 | * @throws \Marek\Toggable\API\Exception\PropertyReadOnlyException |
||
42 | */ |
||
43 | 48 | View Code Duplication | public function __get($property) |
52 | |||
53 | /** |
||
54 | * @param $property |
||
55 | * @param $value |
||
56 | * |
||
57 | * @return mixed |
||
58 | * |
||
59 | * @throws \Marek\Toggable\API\Exception\PropertyNotFoundException |
||
60 | * @throws \Marek\Toggable\API\Exception\PropertyReadOnlyException |
||
61 | */ |
||
62 | 13 | View Code Duplication | public function __set($property, $value) |
74 | |||
75 | /** |
||
76 | * Magic isset function handling isset() to non public properties |
||
77 | * |
||
78 | * @param string $property |
||
79 | * |
||
80 | * @return boolean |
||
81 | */ |
||
82 | 14 | public function __isset($property) |
|
86 | } |
||
87 |
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.