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 |
||
| 26 | class RelationNode extends BaseNode |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var EE_Base_Class |
||
| 30 | */ |
||
| 31 | protected $main_model_obj; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | protected $count; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var EEM_Base |
||
| 40 | */ |
||
| 41 | protected $related_model; |
||
| 42 | |||
| 43 | |||
| 44 | protected $model_obj_nodes; |
||
| 45 | |||
| 46 | public function __construct($main_model_obj, $related_model) |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Here is where most of the work happens. We've counted how many related model objects exist, here we identify |
||
| 56 | * them (ie, learn their IDs). But its recursive, so we'll also find their related dependent model objects etc. |
||
| 57 | * @since $VID:$ |
||
| 58 | * @param int $model_objects_to_identify |
||
| 59 | * @return int |
||
| 60 | * @throws EE_Error |
||
| 61 | * @throws InvalidArgumentException |
||
| 62 | * @throws InvalidDataTypeException |
||
| 63 | * @throws InvalidInterfaceException |
||
| 64 | * @throws ReflectionException |
||
| 65 | */ |
||
| 66 | protected function work($model_objects_to_identify) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Checks if all the identified child nodes are complete or not. |
||
| 105 | * @since $VID:$ |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | protected function allChildrenComplete() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Visits the provided nodes and keeps track of how much work was done, making sure to not go over budget. |
||
| 120 | * @since $VID:$ |
||
| 121 | * @param ModelObjNode[] $model_obj_nodes |
||
| 122 | * @param $work_budget |
||
| 123 | * @return int |
||
| 124 | */ |
||
| 125 | protected function visitAlreadyDiscoveredNodes($model_obj_nodes, $work_budget) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Whether this item has already been initialized |
||
| 142 | */ |
||
| 143 | protected function isDiscovered() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @since $VID:$ |
||
| 150 | * @return boolean |
||
| 151 | */ |
||
| 152 | public function isComplete() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Discovers how many related model objects exist. |
||
| 166 | * @since $VID:$ |
||
| 167 | * @return mixed|void |
||
| 168 | * @throws EE_Error |
||
| 169 | * @throws InvalidArgumentException |
||
| 170 | * @throws InvalidDataTypeException |
||
| 171 | * @throws InvalidInterfaceException |
||
| 172 | * @throws ReflectionException |
||
| 173 | */ |
||
| 174 | protected function discover() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @since $VID:$ |
||
| 181 | * @return array |
||
| 182 | * @throws EE_Error |
||
| 183 | * @throws InvalidDataTypeException |
||
| 184 | * @throws InvalidInterfaceException |
||
| 185 | * @throws InvalidArgumentException |
||
| 186 | * @throws ReflectionException |
||
| 187 | */ |
||
| 188 | protected function whereQueryParams() |
||
| 196 | /** |
||
| 197 | * @since $VID:$ |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function toArray() |
||
| 212 | } |
||
| 213 | // End of file RelationNode.php |
||
| 215 |
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.