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:
Complex classes like DBSelector often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DBSelector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class DBSelector extends \Asymptix\core\BasicObject { |
||
| 16 | protected $fieldsList = [ |
||
| 17 | 'conditions' => "", |
||
| 18 | 'order' => "", |
||
| 19 | 'offset' => 0, |
||
| 20 | 'count' => "all", |
||
| 21 | 'field' => "" |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Inform DBSelector to select only unique records with DISTINCT. |
||
| 26 | * |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | public $unique = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Class name of the selector objects. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $className = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Selector object. |
||
| 40 | * |
||
| 41 | * @var DBObject |
||
| 42 | */ |
||
| 43 | private $dbObject = null; |
||
| 44 | |||
| 45 | public function __construct($className) { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Validate if classname of the selector equal to the select method |
||
| 55 | * classname. |
||
| 56 | * |
||
| 57 | * @param string $className |
||
| 58 | */ |
||
| 59 | private function validateClassName($className) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Reset fields to the initial values. |
||
| 73 | */ |
||
| 74 | public function reset() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Wrapper setter method for set SQL query conditions. |
||
| 86 | * |
||
| 87 | * @param mixed $conditions |
||
| 88 | */ |
||
| 89 | public function setConditions($conditions) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Selects DBObject from the database. |
||
| 98 | * |
||
| 99 | * @param bool $debug Debug flag. |
||
| 100 | * @return DBObject |
||
| 101 | */ |
||
| 102 | public function selectDBObject($debug = false) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Selects DBObject by some field value. |
||
| 126 | * |
||
| 127 | * @param string $fieldName Name of the field. |
||
| 128 | * @param mixed $fieldValue Field value. |
||
| 129 | * @param bool $debug Debug mode flag. |
||
| 130 | * |
||
| 131 | * @return DBObject |
||
| 132 | */ |
||
| 133 | public function selectDBObjectByField($fieldName, $fieldValue, $debug = false) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Selects DBObject by ID. |
||
| 163 | * |
||
| 164 | * @param mixed $objectId Id of the DB record (primary index). |
||
| 165 | * @param bool $debug Debug mode flag. |
||
| 166 | * |
||
| 167 | * @return DBObject |
||
| 168 | */ |
||
| 169 | public function selectDBObjectById($objectId = null, $debug = false) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Selects DBObjects by some predefined condition. |
||
| 179 | * |
||
| 180 | * @param bool $debug Debug mode flag. |
||
| 181 | * |
||
| 182 | * @return array<DBObject> |
||
| 183 | */ |
||
| 184 | public function selectDBObjects($debug = false) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Selects DBObjects by some field value. |
||
| 214 | * |
||
| 215 | * @param string $fieldName Name of the field. |
||
| 216 | * @param mixed $fieldValue Field value. |
||
| 217 | * @param bool $debug Debug mode flag. |
||
| 218 | * |
||
| 219 | * @return array<DBObject> |
||
| 220 | */ |
||
| 221 | public function selectDBObjectsByField($fieldName, $fieldValue, $debug = false) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Count number of records by some predefined condition. |
||
| 252 | * |
||
| 253 | * @return int Number of records. |
||
| 254 | */ |
||
| 255 | View Code Duplication | public function count() { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Selects max value of some field by some predefined condition. |
||
| 267 | * |
||
| 268 | * @return int Number of records. |
||
| 269 | */ |
||
| 270 | View Code Duplication | public function max() { |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Selects min value of some field by some predefined condition. |
||
| 282 | * |
||
| 283 | * @return int Number of records. |
||
| 284 | */ |
||
| 285 | View Code Duplication | public function min() { |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Returns SQL ORDER string for current selector. |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | private function getQueryOrderSQL() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns SQL LIMIT string for current selector. |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | private function getQueryLimitSQL() { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Magic methods for readable method names. |
||
| 327 | * |
||
| 328 | * @param string $methodName Name of the method. |
||
| 329 | * @param array $methodParams Method parameters. |
||
| 330 | * |
||
| 331 | * @return mixed |
||
| 332 | * @throws DBSelectorException |
||
| 333 | */ |
||
| 334 | public function __call($methodName, $methodParams) { |
||
| 418 | } |
||
| 419 | |||
| 424 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.