Complex classes like SubsidiaryTrait 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 SubsidiaryTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | Trait SubsidiaryTrait |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var string[] Subsidiary map. |
||
| 25 | * Array key represents class alias, |
||
| 26 | * array value represents the full qualified class name corresponds to the alias. |
||
| 27 | * |
||
| 28 | * For example: |
||
| 29 | * ```php |
||
| 30 | * public $subsidiaryMap = [ |
||
| 31 | * 'Profile' => 'app\models\user\Profile', |
||
| 32 | * ]; |
||
| 33 | * ``` |
||
| 34 | * or: |
||
| 35 | * ```php |
||
| 36 | * public $subsidiaryMap = [ |
||
| 37 | * 'Profile' => [ |
||
| 38 | * 'class' => 'app\models\user\Profile', |
||
| 39 | * 'max' => 1, |
||
| 40 | * ] |
||
| 41 | * ]; |
||
| 42 | * |
||
| 43 | * If you want to create subsidiary model and the class is not found, the array elements will be taken. |
||
| 44 | */ |
||
| 45 | public $subsidiaryMap = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Add subsidiary. |
||
| 49 | * @param string $name |
||
| 50 | * @param string|array $config |
||
| 51 | * @return boolean |
||
| 52 | * @throws InvalidConfigException |
||
| 53 | */ |
||
| 54 | public function addSubsidiary($name, $config) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * |
||
| 77 | * @param string $name |
||
| 78 | * @return boolean |
||
| 79 | */ |
||
| 80 | public function removeSubsidiary($name) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * |
||
| 91 | * @param string $name |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function getSubsidiaryClass($name) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * |
||
| 105 | * @param string $name |
||
| 106 | * @param integer $limit |
||
| 107 | * @return boolean |
||
| 108 | */ |
||
| 109 | public function changeSubsidiaryLimit($name, $limit) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * |
||
| 119 | * @param string $name |
||
| 120 | * @return boolean |
||
| 121 | */ |
||
| 122 | public function removeSubsidiaryLimit($name) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * |
||
| 136 | * @param string $name |
||
| 137 | * @return boolean |
||
| 138 | * @throws InvalidConfigException |
||
| 139 | */ |
||
| 140 | public function getSubsidiaryLimit($name) |
||
| 153 | |||
| 154 | public function __call($name, $arguments) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Find existed, or create new model. |
||
| 166 | * If model to be found doesn't exist, and $config is null, the parameter |
||
| 167 | * `$condition` will be regarded as properties of new model. |
||
| 168 | * If you want to know whether the returned model is new model, please check |
||
| 169 | * the return value of `getIsNewRecord()` method. |
||
| 170 | * @param string $className Full qualified class name. |
||
| 171 | * @param array $condition Search condition, or properties if not found and |
||
| 172 | * $config is null. |
||
| 173 | * @param array $config new model's configuration array. If you specify this |
||
| 174 | * parameter, the $condition will be skipped when created one. |
||
| 175 | * @return [[$className]] the existed model, or new model created by specified |
||
|
|
|||
| 176 | * condition or configuration. |
||
| 177 | */ |
||
| 178 | 1 | public function findOneOrCreate($className, $condition = [], $config = null) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Create new entity model associated with current user. The model to be created |
||
| 196 | * must be extended from [[BaseBlameableModel]], [[BaseMongoBlameableModel]], |
||
| 197 | * [[BaseRedisBlameableModel]], or any other classes used [[BlameableTrait]]. |
||
| 198 | * if $config does not specify `userClass` property, self will be assigned to. |
||
| 199 | * @param string $className Full qualified class name. |
||
| 200 | * @param array $config name-value pairs that will be used to initialize |
||
| 201 | * the object properties. |
||
| 202 | * @param boolean $loadDefault Determines whether loading default values |
||
| 203 | * after entity model created. |
||
| 204 | * Notice! The [[\yii\mongodb\ActiveRecord]] and [[\yii\redis\ActiveRecord]] |
||
| 205 | * does not support loading default value. If you want to assign properties |
||
| 206 | * with default values, please define the `default` rule(s) for properties in |
||
| 207 | * `rules()` method and return them by yourself if you don't specified them in config param. |
||
| 208 | * @param boolean $skipIfSet whether existing value should be preserved. |
||
| 209 | * This will only set defaults for attributes that are `null`. |
||
| 210 | * @return [[$className]] new model created with specified configuration. |
||
| 211 | */ |
||
| 212 | 54 | public function create($className, $config = [], $loadDefault = true, $skipIfSet = true) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * |
||
| 230 | * @param string $name |
||
| 231 | * @param array $config |
||
| 232 | * @return type |
||
| 233 | * @todo 区分字符串和类的实例两种情况。 |
||
| 234 | */ |
||
| 235 | public function createSubsidiary($name, $config) |
||
| 250 | } |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.