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 |
||
29 | abstract class ExplicitGetterSetter extends ExplicitGetter |
||
30 | { |
||
31 | |||
32 | |||
33 | // <editor-fold desc="// = = = = P R O T E C T E D F I E L D S = = = = = = = = = = = = = = = = = = = = = = ="> |
||
34 | |||
35 | /** |
||
36 | * The names of all set* methods (without the leading set) that should not be mapped as dynamic properties |
||
37 | * |
||
38 | * @type array |
||
39 | */ |
||
40 | protected $ignoreSetProperties = []; |
||
41 | |||
42 | // </editor-fold> |
||
43 | |||
44 | |||
45 | // <editor-fold desc="// = = = = P U B L I C M E T H O D S = = = = = = = = = = = = = = = = = = = = = = = = ="> |
||
46 | |||
47 | /** |
||
48 | * The magic set method to let you access all setter methods by dynamic properties. |
||
49 | * |
||
50 | * @param string $name The name of the dynamic property. |
||
51 | * @param mixed $value The value to set. |
||
52 | * @throws \LogicException Is thrown if the property does not exist or if writing is requested but not allowed. |
||
53 | */ |
||
54 | 4 | public function __set( string $name, $value ) |
|
65 | |||
66 | /** |
||
67 | * Magic isset implementation. |
||
68 | * |
||
69 | * @param string $name The name of the required property. |
||
70 | * @return boolean |
||
71 | */ |
||
72 | 3 | public function __isset( string $name ) |
|
83 | |||
84 | /** |
||
85 | * Returns, if a property with the defined name exists for write access. |
||
86 | * |
||
87 | * @param string $name The name of the property. |
||
88 | * @param string $setterName Returns the name of the associated set method, if method returns TRUE. |
||
89 | * @return boolean |
||
90 | */ |
||
91 | 4 | View Code Duplication | public function hasWritableProperty( string $name, &$setterName ) : bool |
104 | |||
105 | // </editor-fold> |
||
106 | |||
107 | |||
108 | } |
||
109 | |||
110 |
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.