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 |
||
| 16 | abstract class AbstractDaftObject implements DaftObject |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * List of properties that can be defined on an implementation. |
||
| 20 | * |
||
| 21 | * @var string[] |
||
| 22 | */ |
||
| 23 | const PROPERTIES = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * List of nullable properties that can be defined on an implementation. |
||
| 27 | * |
||
| 28 | * @var string[] |
||
| 29 | */ |
||
| 30 | const NULLABLE_PROPERTIES = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Index of checked types. |
||
| 34 | * |
||
| 35 | * @see self::CheckTypeDefinesOwnIdProperties() |
||
| 36 | * |
||
| 37 | * @var bool[] |
||
| 38 | */ |
||
| 39 | private static $checkedTypes = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Does some sanity checking. |
||
| 43 | * |
||
| 44 | * @see DefinesOwnIdPropertiesInterface |
||
| 45 | * @see self::CheckTypeDefinesOwnIdProperties() |
||
| 46 | * |
||
| 47 | * @throws TypeError if static::class was previously determined to be incorrectly implemented |
||
| 48 | */ |
||
| 49 | 40 | public function __construct() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Maps param $property to the getter method. |
||
| 64 | * |
||
| 65 | * @param string $property the property being retrieved |
||
| 66 | * |
||
| 67 | * @throws UndefinedPropertyException if a property is undefined |
||
| 68 | * |
||
| 69 | * @return mixed |
||
| 70 | */ |
||
| 71 | 26 | View Code Duplication | public function __get(string $property) |
| 80 | |||
| 81 | /** |
||
| 82 | * Maps param $property to the getter method. |
||
| 83 | * |
||
| 84 | * @param string $property the property being retrieved |
||
| 85 | * @param mixed $v |
||
| 86 | * |
||
| 87 | * @throws UndefinedPropertyException if a property is undefined |
||
| 88 | * |
||
| 89 | * @return mixed |
||
| 90 | */ |
||
| 91 | 13 | View Code Duplication | public function __set(string $property, $v) |
| 102 | |||
| 103 | /** |
||
| 104 | * required to support unset($foo->bar). |
||
| 105 | * |
||
| 106 | * @param string $property the property being unset |
||
| 107 | * |
||
| 108 | * @see static::NudgePropertyValue() |
||
| 109 | */ |
||
| 110 | 8 | public function __unset(string $property) : void |
|
| 114 | |||
| 115 | /** |
||
| 116 | * List of properties that can be defined on an implementation. |
||
| 117 | * |
||
| 118 | * @return string[] |
||
| 119 | */ |
||
| 120 | 6 | final public static function DaftObjectProperties() : array |
|
| 124 | |||
| 125 | /** |
||
| 126 | * List of nullable properties that can be defined on an implementation. |
||
| 127 | * |
||
| 128 | * @return string[] |
||
| 129 | */ |
||
| 130 | 9 | final public static function DaftObjectNullableProperties() : array |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Nudge the state of a given property, marking it as dirty. |
||
| 137 | * |
||
| 138 | * @param string $property property being nudged |
||
| 139 | * @param mixed $value value to nudge property with |
||
| 140 | * |
||
| 141 | * @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties() |
||
| 142 | * @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties() |
||
| 143 | */ |
||
| 144 | abstract protected function NudgePropertyValue( |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Checks if a type correctly defines it's own id. |
||
| 151 | * |
||
| 152 | * @param DaftObject $object |
||
| 153 | * |
||
| 154 | * @throws TypeError if $object::DaftObjectIdProperties() does not contain at least one property |
||
| 155 | * @throws TypeError if $object::DaftObjectIdProperties() is not string[] |
||
| 156 | * @throws UndefinedPropertyException if an id property is not in $object::DaftObjectIdProperties() |
||
| 157 | */ |
||
| 158 | 33 | final protected static function CheckTypeDefinesOwnIdProperties( |
|
| 214 | } |
||
| 215 |
This checks looks for cases where a variable has been assigned to itself.
This assignement can be removed without consequences.