Complex classes like ECKey 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 ECKey, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ECKey |
||
29 | { |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $values = []; |
||
34 | |||
35 | /** |
||
36 | * ECKey constructor. |
||
37 | * |
||
38 | * @param array $data |
||
39 | */ |
||
40 | private function __construct(array $data) |
||
44 | |||
45 | /** |
||
46 | * @param string $pem |
||
47 | * |
||
48 | * @return ECKey |
||
49 | */ |
||
50 | public static function createFromPEM(string $pem): self |
||
56 | |||
57 | /** |
||
58 | * @param string $data |
||
59 | * |
||
60 | * @throws \Exception |
||
61 | * |
||
62 | * @return array |
||
63 | */ |
||
64 | private static function loadPEM(string $data): array |
||
85 | |||
86 | /** |
||
87 | * @param ASNObject[] $children |
||
88 | * |
||
89 | * @return array |
||
90 | */ |
||
91 | private static function loadPKCS8(array $children): array |
||
101 | |||
102 | /** |
||
103 | * @param ASNObject[] $children |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | private static function loadPublicPEM(array $children): array |
||
140 | |||
141 | /** |
||
142 | * @param string $oid |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | private static function getCurve(string $oid): string |
||
156 | |||
157 | /** |
||
158 | * @return array |
||
159 | */ |
||
160 | private static function getSupportedCurves(): array |
||
168 | |||
169 | /** |
||
170 | * @param ASNObject $children |
||
171 | */ |
||
172 | private static function verifyVersion(ASNObject $children) |
||
178 | |||
179 | /** |
||
180 | * @param ASNObject $children |
||
181 | * @param string|null $x |
||
182 | * @param string|null $y |
||
183 | */ |
||
184 | private static function getXAndY(ASNObject $children, ?string &$x, ?string &$y) |
||
203 | |||
204 | /** |
||
205 | * @param ASNObject $children |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | private static function getD(ASNObject $children): string |
||
217 | |||
218 | /** |
||
219 | * @param array $children |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | private static function loadPrivatePEM(array $children): array |
||
248 | |||
249 | /** |
||
250 | * @param ASNObject[] $children |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | private static function isPKCS8(array $children): bool |
||
269 | |||
270 | /** |
||
271 | * @param ECKey $private |
||
272 | * |
||
273 | * @return ECKey |
||
274 | */ |
||
275 | public static function toPublic(self $private): self |
||
284 | |||
285 | /** |
||
286 | * @return array |
||
287 | */ |
||
288 | public function toArray() |
||
292 | |||
293 | /** |
||
294 | * @param array $jwk |
||
295 | */ |
||
296 | private function loadJWK(array $jwk) |
||
315 | } |
||
316 |
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.