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 | // ---- Accessors/Mutators ---- | ||
| 234 | |||
| 235 | /** | ||
| 236 | * Fetch the device counts for groups | ||
| 237 |      * Use DeviceGroups::with('deviceCountRelation') to eager load | ||
| 238 | * | ||
| 239 | * @return int | ||
| 240 | */ | ||
| 241 | public function getDeviceCountAttribute() | ||
| 253 | |||
| 254 | /** | ||
| 255 | * Custom mutator for params attribute | ||
| 256 | * Allows already encoded json to pass through | ||
| 257 | * | ||
| 258 | * @param array|string $params | ||
| 259 | */ | ||
| 260 | public function setParamsAttribute($params) | ||
| 268 | |||
| 269 | /** | ||
| 270 | * Check if the stored pattern is v1 | ||
| 271 | * Convert it to v2 for display | ||
| 272 | * Currently, it will only be updated in the database if the user saves the rule in the ui | ||
| 273 | * | ||
| 274 | * @param $pattern | ||
| 275 | * @return string | ||
| 276 | */ | ||
| 277 | public function getPatternAttribute($pattern) | ||
| 286 | |||
| 287 | /** | ||
| 288 | * Convert a v1 device group pattern to sql that can be ingested by jQuery-QueryBuilder | ||
| 289 | * | ||
| 290 | * @param $pattern | ||
| 291 | * @return array | ||
| 292 | */ | ||
| 293 | private function convertV1Pattern($pattern) | ||
| 334 | |||
| 335 | // ---- Define Relationships ---- | ||
| 336 | |||
| 337 | /** | ||
| 338 | * Convert sql regex to like, many common uses can be converted | ||
| 339 | * Should only be used to convert v1 patterns | ||
| 340 | * | ||
| 341 | * @param $pattern | ||
| 342 | * @return string | ||
| 343 | */ | ||
| 344 | private function convertRegexToLike($pattern) | ||
| 371 | |||
| 372 | /** | ||
| 373 | * Relationship allows us to eager load device counts | ||
| 374 |      * DeviceGroups::with('deviceCountRelation') | ||
| 375 | * | ||
| 376 | * @return mixed | ||
| 377 | */ | ||
| 378 | public function deviceCountRelation() | ||
| 382 | } | ||
| 383 | 
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.