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 |
||
| 49 | class DeviceGroup extends Model |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * Indicates if the model should be timestamped. |
||
| 53 | * |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | public $timestamps = false; |
||
| 57 | /** |
||
| 58 | * The table associated with the model. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $table = 'device_groups'; |
||
| 63 | /** |
||
| 64 | * The primary key column name. |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $primaryKey = 'id'; |
||
| 69 | /** |
||
| 70 | * Virtual attributes |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $appends = ['patternSql', 'deviceCount']; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The attributes that can be mass assigned. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $fillable = ['name', 'desc', 'pattern', 'params']; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The attributes that should be casted to native types. |
||
| 85 | * |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $casts = ['params' => 'array']; |
||
| 89 | |||
| 90 | // ---- Helper Functions ---- |
||
| 91 | |||
| 92 | |||
| 93 | public function updateRelations() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get an array of the device ids from this group by re-querying the database with |
||
| 108 | * either the specified pattern or the saved pattern of this group |
||
| 109 | * |
||
| 110 | * @param string $statement Optional, will use the pattern from this group if not specified |
||
| 111 | * @param array $params array of paremeters |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function getDeviceIdsRaw($statement = null, $params = null) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Process Macros |
||
| 160 | * |
||
| 161 | * @param string $pattern Rule to process |
||
| 162 | * @param int $x Recursion-Anchor, do not pass |
||
| 163 | * @return string|boolean |
||
| 164 | */ |
||
| 165 | public static function applyGroupMacros($pattern, $x = 1) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Extract an array of tables in a pattern |
||
| 190 | * |
||
| 191 | * @param string $pattern |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | private function getTablesFromPattern($pattern) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Relationship to App\Models\Device |
||
| 205 | * |
||
| 206 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
| 207 | */ |
||
| 208 | public function devices() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Returns an sql formatted string |
||
| 215 | * Mostly, this is for ingestion by JQuery-QueryBuilder |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function getPatternSqlAttribute() |
||
| 232 | |||
| 233 | |||
| 234 | // ---- Accessors/Mutators ---- |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Fetch the device counts for groups |
||
| 238 | * Use DeviceGroups::with('deviceCountRelation') to eager load |
||
| 239 | * |
||
| 240 | * @return int |
||
| 241 | */ |
||
| 242 | public function getDeviceCountAttribute() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Custom mutator for params attribute |
||
| 257 | * Allows already encoded json to pass through |
||
| 258 | * |
||
| 259 | * @param array|string $params |
||
| 260 | */ |
||
| 261 | public function setParamsAttribute($params) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Check if the stored pattern is v1 |
||
| 272 | * Convert it to v2 for display |
||
| 273 | * Currently, it will only be updated in the database if the user saves the rule in the ui |
||
| 274 | * |
||
| 275 | * @param $pattern |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getPatternAttribute($pattern) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Convert a v1 device group pattern to sql that can be ingested by jQuery-QueryBuilder |
||
| 290 | * |
||
| 291 | * @param $pattern |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | private function convertV1Pattern($pattern) |
||
| 335 | |||
| 336 | // ---- Define Relationships ---- |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Convert sql regex to like, many common uses can be converted |
||
| 340 | * Should only be used to convert v1 patterns |
||
| 341 | * |
||
| 342 | * @param $pattern |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | private function convertRegexToLike($pattern) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Relationship allows us to eager load device counts |
||
| 375 | * DeviceGroups::with('deviceCountRelation') |
||
| 376 | * |
||
| 377 | * @return mixed |
||
| 378 | */ |
||
| 379 | public function deviceCountRelation() |
||
| 383 | } |
||
| 384 |
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.