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 ProfileValidate 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 ProfileValidate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ProfileValidate extends Model implements ModelInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * Validate fields with rules. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | public $rules = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Attributes. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | public $attributes = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Attribute labels. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | public $attributeLabels = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Set manage for roles. |
||
| 56 | * |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | public $rbacManage = false; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Rewrite rules, labels, attributes by custom. |
||
| 63 | * |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | public $customRewrite = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Current profile (user) model. |
||
| 70 | * |
||
| 71 | * @var IdentityInterface|ActiveRecordInterface |
||
| 72 | */ |
||
| 73 | private $profileModel; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Auth manager. |
||
| 77 | * |
||
| 78 | * @var ManagerInterface |
||
| 79 | */ |
||
| 80 | private $authManager; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Scenarios constants. |
||
| 84 | */ |
||
| 85 | const SCENARIO_CREATE = 'create'; |
||
| 86 | const SCENARIO_UPDATE = 'update'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Initialize. |
||
| 90 | */ |
||
| 91 | public function init() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @inheritdoc |
||
| 100 | */ |
||
| 101 | public function rules() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Scenarios. |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public function scenarios() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * List if attributes. |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | public function attributes() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * List if attribute labels. |
||
| 235 | * |
||
| 236 | * @inheritdoc |
||
| 237 | */ |
||
| 238 | public function attributeLabels() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get field value. |
||
| 262 | * |
||
| 263 | * @param string $name field name. |
||
| 264 | * |
||
| 265 | * @return mixed |
||
| 266 | */ |
||
| 267 | public function __get($name) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Set field value. |
||
| 287 | * |
||
| 288 | * @param string $name name of field. |
||
| 289 | * @param mixed $value value to be stored in field. |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | public function __set($name, $value) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns profile (user) model. |
||
| 300 | * |
||
| 301 | * @return mixed |
||
| 302 | */ |
||
| 303 | public function getProfileModel() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set profile (user) model. |
||
| 310 | * |
||
| 311 | * @param $model. |
||
| 312 | * |
||
| 313 | * @throws InvalidConfigException |
||
| 314 | * |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | public function setProfileModel($model): void |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Set auth manager. |
||
| 326 | * |
||
| 327 | * @param ManagerInterface $authManager |
||
| 328 | */ |
||
| 329 | public function setAuthManager(ManagerInterface $authManager): void |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Validate roles data format. |
||
| 336 | * |
||
| 337 | * @param $attribute |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | public function validateRoles($attribute): bool |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Save user data. |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | public function save(): bool |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Returns current model id. |
||
| 388 | * |
||
| 389 | * @return int |
||
| 390 | */ |
||
| 391 | public function getId(): int |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Check profileModel for instances. |
||
| 398 | * |
||
| 399 | * @throws InvalidConfigException |
||
| 400 | * |
||
| 401 | * @param $model |
||
| 402 | * |
||
| 403 | * @return void |
||
| 404 | */ |
||
| 405 | public static function checkProfileModel($model) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Assign roles. |
||
| 422 | * |
||
| 423 | * @return void |
||
| 424 | */ |
||
| 425 | private function assignRoles(): void |
||
| 443 | } |
||
| 444 |
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.