Complex classes like Record 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 Record, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | abstract class Record |
||
| 16 | { |
||
| 17 | use NameWorksTrait; |
||
| 18 | |||
| 19 | protected $_name = null; |
||
| 20 | protected $_manager = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $managerName = null; |
||
| 26 | |||
| 27 | protected $_dbData = []; |
||
| 28 | protected $_helpers = []; |
||
| 29 | |||
| 30 | |||
| 31 | protected $_data; |
||
| 32 | |||
| 33 | public function &__get($name) |
||
| 41 | |||
| 42 | public function __set($name, $value) |
||
| 46 | |||
| 47 | public function __isset($name) |
||
| 51 | |||
| 52 | public function __unset($name) |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Overloads Ucfirst() helper |
||
| 60 | * |
||
| 61 | * @param string $name |
||
| 62 | * @param array $arguments |
||
| 63 | * @return \Nip\Helpers\AbstractHelper|null |
||
| 64 | */ |
||
| 65 | public function __call($name, $arguments) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param string $name |
||
| 77 | * @return \Nip\Helpers\AbstractHelper |
||
| 78 | */ |
||
| 79 | public function getHelper($name) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return mixed |
||
| 86 | */ |
||
| 87 | public function getName() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param mixed $name |
||
| 97 | */ |
||
| 98 | public function setName($name) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param bool|array $data |
||
| 105 | */ |
||
| 106 | public function writeDBData($data = false) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | public function getDBData() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return mixed |
||
| 123 | */ |
||
| 124 | public function getPrimaryKey() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return \Nip\Records\RecordManager |
||
| 133 | */ |
||
| 134 | public function getManager() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param RecordManager $manager |
||
| 145 | */ |
||
| 146 | public function setManager($manager) |
||
| 150 | |||
| 151 | protected function initManager() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getManagerName() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $managerName |
||
| 172 | */ |
||
| 173 | public function setManagerName($managerName) |
||
| 177 | |||
| 178 | protected function initManagerName() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | protected function inflectManagerName() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $class |
||
| 193 | * @return RecordManager |
||
| 194 | * @throws Exception |
||
| 195 | */ |
||
| 196 | protected function getManagerInstance($class) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public function insert() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return bool|\Nip\Database\Result |
||
| 220 | */ |
||
| 221 | public function update() |
||
| 226 | |||
| 227 | public function save() |
||
| 231 | |||
| 232 | public function saveRecord() |
||
| 236 | |||
| 237 | public function delete() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function isInDB() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return bool|false|Record |
||
| 253 | */ |
||
| 254 | public function exists() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function toJSON() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return mixed |
||
| 269 | */ |
||
| 270 | public function toArray() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @return mixed |
||
| 278 | */ |
||
| 279 | public function toApiArray() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return Record |
||
| 287 | */ |
||
| 288 | public function getCloneWithRelations() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return Record |
||
| 298 | */ |
||
| 299 | public function getClone() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param self $record |
||
| 311 | */ |
||
| 312 | public function updateDataFromRecord($record) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param bool|array $data |
||
| 322 | */ |
||
| 323 | public function writeData($data = false) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Clone the relations records from a sibling |
||
| 332 | * @param self $from |
||
| 333 | * @return \Nip\Records\Traits\Relations\HasRelationsRecordTrait |
||
| 334 | */ |
||
| 335 | public function cloneRelations($from) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return \Nip\Request |
||
| 342 | */ |
||
| 343 | protected function getRequest() |
||
| 347 | } |
||
| 348 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.