Complex classes like DeviceGroup 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 DeviceGroup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class DeviceGroup extends Model |
||
| 53 | { |
||
| 54 | /** |
||
| 55 | * Indicates if the model should be timestamped. |
||
| 56 | * |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | public $timestamps = false; |
||
| 60 | /** |
||
| 61 | * The table associated with the model. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $table = 'device_groups'; |
||
| 66 | /** |
||
| 67 | * The primary key column name. |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $primaryKey = 'id'; |
||
| 72 | /** |
||
| 73 | * Virtual attributes |
||
| 74 | * |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $appends = ['patternSql', 'deviceCount']; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The attributes that can be mass assigned. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $fillable = ['name', 'desc', 'pattern', 'params']; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The attributes that should be casted to native types. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $casts = ['params' => 'array']; |
||
| 92 | |||
| 93 | // ---- Helper Functions ---- |
||
| 94 | |||
| 95 | |||
| 96 | 1 | public function updateRelations() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Get an array of the device ids from this group by re-querying the database with |
||
| 111 | * either the specified pattern or the saved pattern of this group |
||
| 112 | * |
||
| 113 | * @param string $statement Optional, will use the pattern from this group if not specified |
||
| 114 | * @param array $params array of paremeters |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | 1 | public function getDeviceIdsRaw($statement = null, $params = null) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Process Macros |
||
| 163 | * |
||
| 164 | * @param string $pattern Rule to process |
||
| 165 | * @param int $x Recursion-Anchor, do not pass |
||
| 166 | * @return string|boolean |
||
| 167 | */ |
||
| 168 | 2 | public static function applyGroupMacros($pattern, $x = 1) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Extract an array of tables in a pattern |
||
| 193 | * |
||
| 194 | * @param string $pattern |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | 1 | private function getTablesFromPattern($pattern) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Convert a v1 device group pattern to sql that can be ingested by jQuery-QueryBuilder |
||
| 208 | * |
||
| 209 | * @param $pattern |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | 1 | private function convertV1Pattern($pattern) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Convert sql regex to like, many common uses can be converted |
||
| 255 | * Should only be used to convert v1 patterns |
||
| 256 | * |
||
| 257 | * @param $pattern |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 1 | private function convertRegexToLike($pattern) |
|
| 287 | |||
| 288 | // ---- Accessors/Mutators ---- |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns an sql formatted string |
||
| 292 | * Mostly, this is for ingestion by JQuery-QueryBuilder |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | public function getPatternSqlAttribute() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Fetch the device counts for groups |
||
| 312 | * Use DeviceGroups::with('deviceCountRelation') to eager load |
||
| 313 | * |
||
| 314 | * @return int |
||
| 315 | */ |
||
| 316 | public function getDeviceCountAttribute() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Custom mutator for params attribute |
||
| 331 | * Allows already encoded json to pass through |
||
| 332 | * |
||
| 333 | * @param array|string $params |
||
| 334 | */ |
||
| 335 | 1 | public function setParamsAttribute($params) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Check if the stored pattern is v1 |
||
| 346 | * Convert it to v2 for display |
||
| 347 | * Currently, it will only be updated in the database if the user saves the rule in the ui |
||
| 348 | * |
||
| 349 | * @param $pattern |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | 2 | public function getPatternAttribute($pattern) |
|
| 361 | |||
| 362 | |||
| 363 | // ---- Define Relationships ---- |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Relationship allows us to eager load device counts |
||
| 367 | * DeviceGroups::with('deviceCountRelation') |
||
| 368 | * |
||
| 369 | * @return mixed |
||
| 370 | */ |
||
| 371 | public function deviceCountRelation() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Relationship to App\Models\Device |
||
| 379 | * |
||
| 380 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 381 | */ |
||
| 382 | 1 | public function devices() |
|
| 386 | } |
||
| 387 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.