Complex classes like EntityTrait 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 EntityTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | trait EntityTrait |
||
27 | { |
||
28 | use GUIDTrait, |
||
29 | IDTrait, |
||
30 | IPTrait, |
||
31 | TimestampTrait; |
||
32 | |||
33 | private $entityLocalRules = []; |
||
34 | private $entityLocalBehaviors = []; |
||
35 | |||
36 | /** |
||
37 | * @var string cache key and tag prefix. the prefix is usually set to full |
||
38 | * qualified class name. |
||
39 | */ |
||
40 | public $cachePrefix = ''; |
||
41 | public static $eventNewRecordCreated = 'newRecordCreated'; |
||
42 | public static $cacheKeyEntityRules = 'entity_rules'; |
||
43 | public static $cacheTagEntityRules = 'tag_entity_rules'; |
||
44 | public static $cacheKeyEntityBehaviors = 'entity_behaviors'; |
||
45 | public static $cacheTagEntityBehaviors = 'tag_entity_behaviors'; |
||
46 | |||
47 | /** |
||
48 | * @var string cache component id. |
||
49 | */ |
||
50 | public $cacheId = 'cache'; |
||
51 | |||
52 | /** |
||
53 | * @var boolean Determines to skip initialization. |
||
54 | */ |
||
55 | public $skipInit = false; |
||
56 | |||
57 | /** |
||
58 | * @var string the name of query class or sub-class. |
||
59 | */ |
||
60 | public $queryClass; |
||
61 | |||
62 | /** |
||
63 | * @return \static New self without any initializations. |
||
64 | */ |
||
65 | 55 | public static function buildNoInitModel() |
|
69 | |||
70 | /** |
||
71 | * Populate and return the entity rules. |
||
72 | * You should call this function in your extended class and merge the result |
||
73 | * with your rules, instead of overriding it, unless you know the |
||
74 | * consequences. |
||
75 | * @return type |
||
76 | */ |
||
77 | 53 | public function rules() |
|
81 | |||
82 | /** |
||
83 | * Populate and return the entity behaviors. |
||
84 | * You should call this function in your extended class and merge the result |
||
85 | * with your behaviors, instead of overriding it, unless you know the |
||
86 | * consequences. |
||
87 | * @return type |
||
88 | */ |
||
89 | 55 | public function behaviors() |
|
93 | |||
94 | /** |
||
95 | * Get cache component. If cache component is not configured, Yii::$app->cache |
||
96 | * will be given. |
||
97 | * @return \yii\caching\Cache cache component. |
||
98 | */ |
||
99 | 55 | protected function getCache() |
|
104 | |||
105 | /** |
||
106 | * Get entity rules cache key. |
||
107 | * @return string cache key. |
||
108 | */ |
||
109 | 53 | public function getEntityRulesCacheKey() |
|
113 | |||
114 | /** |
||
115 | * Get entity rules cache tag. |
||
116 | * @return string cache tag. |
||
117 | */ |
||
118 | 17 | public function getEntityRulesCacheTag() |
|
122 | |||
123 | /** |
||
124 | * Get entity rules. |
||
125 | * @return array rules. |
||
126 | */ |
||
127 | 53 | public function getEntityRules() |
|
139 | |||
140 | /** |
||
141 | * Set entity rules. |
||
142 | * @param array $rules |
||
143 | */ |
||
144 | 17 | protected function setEntityRules($rules = []) |
|
155 | |||
156 | /** |
||
157 | * Get entity behaviors cache key. |
||
158 | * @return string cache key. |
||
159 | */ |
||
160 | 55 | public function getEntityBehaviorsCacheKey() |
|
164 | |||
165 | /** |
||
166 | * Get entity behaviors cache tag. |
||
167 | * @return string cache tag. |
||
168 | */ |
||
169 | 17 | public function getEntityBehaviorsCacheTag() |
|
173 | |||
174 | /** |
||
175 | * Get the entity behaviors. |
||
176 | * @return array |
||
177 | */ |
||
178 | 55 | public function getEntityBehaviors() |
|
189 | |||
190 | /** |
||
191 | * Set the entity behaviors. |
||
192 | * @param array $behaviors |
||
193 | */ |
||
194 | 17 | protected function setEntityBehaviors($behaviors) |
|
204 | |||
205 | /** |
||
206 | * Reset cache key. |
||
207 | * @param string $cacheKey |
||
208 | * @param mixed $value |
||
209 | * @return boolean whether the value is successfully stored into cache. if |
||
210 | * cache component was not configured, then return false directly. |
||
211 | */ |
||
212 | public function resetCacheKey($cacheKey, $value = false) |
||
220 | |||
221 | /** |
||
222 | * Attach events associated with entity model. |
||
223 | */ |
||
224 | 55 | protected function initEntityEvents() |
|
235 | |||
236 | /** |
||
237 | * Initialize the cache prefix. |
||
238 | * @param \yii\base\Event $event |
||
239 | */ |
||
240 | 55 | public function onInitCache($event) |
|
250 | |||
251 | /** |
||
252 | * Record warnings. |
||
253 | */ |
||
254 | protected function recordWarnings() |
||
260 | |||
261 | /** |
||
262 | * Get guid or id. if neither disabled, return null. |
||
263 | * @return string |
||
264 | */ |
||
265 | 8 | public function __toString() |
|
275 | |||
276 | /** |
||
277 | * @inheritdoc |
||
278 | * ------------- |
||
279 | * if enable `$idAttribute` and $row[$idAttribute] set, the `idPreassigned` |
||
280 | * will be assigned to true. |
||
281 | */ |
||
282 | 42 | public static function instantiate($row) |
|
292 | |||
293 | /** |
||
294 | * unset entity attributes. |
||
295 | * @return array result. |
||
296 | */ |
||
297 | public function unsetSelfFields() |
||
301 | |||
302 | /** |
||
303 | * unset fields of array. |
||
304 | * @param array $array |
||
305 | * @param array $fields |
||
306 | * @return array |
||
307 | */ |
||
308 | public static function unsetFields($array, $fields = null) |
||
320 | |||
321 | /** |
||
322 | * Get enabled fields. |
||
323 | * @return string[] |
||
324 | */ |
||
325 | 16 | public function enabledFields() |
|
334 | } |
||
335 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.