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 |
||
| 27 | class RelationNode extends BaseNode |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var EE_Base_Class |
||
| 31 | */ |
||
| 32 | protected $main_model_obj; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | protected $count; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var EEM_Base |
||
| 41 | */ |
||
| 42 | protected $related_model; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var ModelObjNode[] |
||
| 46 | */ |
||
| 47 | protected $model_obj_nodes; |
||
| 48 | |||
| 49 | public function __construct($main_model_obj, $related_model) |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Here is where most of the work happens. We've counted how many related model objects exist, here we identify |
||
| 59 | * them (ie, learn their IDs). But its recursive, so we'll also find their related dependent model objects etc. |
||
| 60 | * @since $VID:$ |
||
| 61 | * @param int $model_objects_to_identify |
||
| 62 | * @return int |
||
| 63 | * @throws EE_Error |
||
| 64 | * @throws InvalidArgumentException |
||
| 65 | * @throws InvalidDataTypeException |
||
| 66 | * @throws InvalidInterfaceException |
||
| 67 | * @throws ReflectionException |
||
| 68 | */ |
||
| 69 | protected function work($model_objects_to_identify) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Checks if all the identified child nodes are complete or not. |
||
| 108 | * @since $VID:$ |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | protected function allChildrenComplete() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Visits the provided nodes and keeps track of how much work was done, making sure to not go over budget. |
||
| 123 | * @since $VID:$ |
||
| 124 | * @param ModelObjNode[] $model_obj_nodes |
||
| 125 | * @param $work_budget |
||
| 126 | * @return int |
||
| 127 | */ |
||
| 128 | protected function visitAlreadyDiscoveredNodes($model_obj_nodes, $work_budget) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Whether this item has already been initialized |
||
| 145 | */ |
||
| 146 | protected function isDiscovered() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @since $VID:$ |
||
| 153 | * @return boolean |
||
| 154 | */ |
||
| 155 | public function isComplete() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Discovers how many related model objects exist. |
||
| 169 | * @since $VID:$ |
||
| 170 | * @return mixed|void |
||
| 171 | * @throws EE_Error |
||
| 172 | * @throws InvalidArgumentException |
||
| 173 | * @throws InvalidDataTypeException |
||
| 174 | * @throws InvalidInterfaceException |
||
| 175 | * @throws ReflectionException |
||
| 176 | */ |
||
| 177 | protected function discover() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @since $VID:$ |
||
| 184 | * @return array |
||
| 185 | * @throws EE_Error |
||
| 186 | * @throws InvalidDataTypeException |
||
| 187 | * @throws InvalidInterfaceException |
||
| 188 | * @throws InvalidArgumentException |
||
| 189 | * @throws ReflectionException |
||
| 190 | */ |
||
| 191 | protected function whereQueryParams() |
||
| 210 | /** |
||
| 211 | * @since $VID:$ |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | public function toArray() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Gets the IDs of all the model objects to delete; indexed first by model object name. |
||
| 229 | * @since $VID:$ |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | public function getIds() |
||
| 248 | } |
||
| 249 | // End of file RelationNode.php |
||
| 251 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.