Complex classes like BaseMongoEntityModel 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 BaseMongoEntityModel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | abstract class BaseMongoEntityModel extends ActiveRecord |
||
32 | { |
||
33 | use EntityTrait; |
||
34 | |||
35 | 73 | public function getGUIDRules() |
|
45 | |||
46 | 9 | public function getGUID() |
|
51 | |||
52 | 74 | public function setGUID($guid) |
|
63 | |||
64 | /** |
||
65 | * Check if the $guid existed in current database table. |
||
66 | * @param string|Binary $guid the GUID to be checked. |
||
67 | * @return boolean Whether the $guid exists or not. |
||
68 | */ |
||
69 | 3 | public static function checkGuidExists($guid) |
|
86 | |||
87 | /** |
||
88 | * Get the rules associated with ip attributes. |
||
89 | * @return array |
||
90 | */ |
||
91 | 73 | public function getIPRules() |
|
115 | |||
116 | /** |
||
117 | * Get the IPv4 address. |
||
118 | * @return string |
||
119 | */ |
||
120 | 3 | protected function getIPv4Address() |
|
124 | |||
125 | /** |
||
126 | * Get the IPv6 address. |
||
127 | * @return string |
||
128 | */ |
||
129 | 3 | protected function getIPv6Address() |
|
133 | |||
134 | 3 | protected function setIPv4Address($ipAddress) |
|
138 | |||
139 | 3 | protected function setIPv6Address($ipAddress) |
|
143 | |||
144 | /** |
||
145 | * Initialize new entity. |
||
146 | */ |
||
147 | 74 | public function init() |
|
157 | |||
158 | /** |
||
159 | * @inheritdoc |
||
160 | * @return BaseMongoEntityQuery the newly created [[BaseMongoEntityQuery]] or its sub-class instance. |
||
161 | */ |
||
162 | 61 | public static function find() |
|
172 | |||
173 | /** |
||
174 | * @inheritdoc |
||
175 | * You can override this method if enabled fields cannot meet you requirements. |
||
176 | * @return array |
||
177 | */ |
||
178 | 16 | public function attributes() |
|
182 | |||
183 | /** |
||
184 | * |
||
185 | * @param array $models |
||
186 | */ |
||
187 | 7 | public static function compositeGUIDs($models) { |
|
209 | } |
||
210 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.