Complex classes like APIv2 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 APIv2, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class APIv2 extends OCSController { |
||
45 | |||
46 | /** @var string */ |
||
47 | protected $filter; |
||
48 | |||
49 | /** @var int */ |
||
50 | protected $since; |
||
51 | |||
52 | /** @var int */ |
||
53 | protected $limit; |
||
54 | |||
55 | /** @var string */ |
||
56 | protected $sort; |
||
57 | |||
58 | /** @var string */ |
||
59 | protected $objectType; |
||
60 | |||
61 | /** @var int */ |
||
62 | protected $objectId; |
||
63 | |||
64 | /** @var string */ |
||
65 | protected $user; |
||
66 | |||
67 | /** @var bool */ |
||
68 | protected $loadPreviews; |
||
69 | |||
70 | |||
71 | /** @var IManager */ |
||
72 | protected $activityManager; |
||
73 | |||
74 | /** @var Data */ |
||
75 | protected $data; |
||
76 | |||
77 | /** @var GroupHelper */ |
||
78 | protected $helper; |
||
79 | |||
80 | /** @var UserSettings */ |
||
81 | protected $settings; |
||
82 | |||
83 | /** @var IURLGenerator */ |
||
84 | protected $urlGenerator; |
||
85 | |||
86 | /** @var IUserSession */ |
||
87 | protected $userSession; |
||
88 | |||
89 | /** @var IPreview */ |
||
90 | protected $preview; |
||
91 | |||
92 | /** @var IMimeTypeDetector */ |
||
93 | protected $mimeTypeDetector; |
||
94 | |||
95 | /** @var View */ |
||
96 | protected $view; |
||
97 | |||
98 | /** @var ViewInfoCache */ |
||
99 | protected $infoCache; |
||
100 | |||
101 | /** |
||
102 | * OCSEndPoint constructor. |
||
103 | * |
||
104 | * @param string $appName |
||
105 | * @param IRequest $request |
||
106 | * @param IManager $activityManager |
||
107 | * @param Data $data |
||
108 | * @param GroupHelper $helper |
||
109 | * @param UserSettings $settings |
||
110 | * @param IURLGenerator $urlGenerator |
||
111 | * @param IUserSession $userSession |
||
112 | * @param IPreview $preview |
||
113 | * @param IMimeTypeDetector $mimeTypeDetector |
||
114 | * @param View $view |
||
115 | * @param ViewInfoCache $infoCache |
||
116 | */ |
||
117 | 65 | public function __construct($appName, |
|
141 | |||
142 | /** |
||
143 | * @param string $filter |
||
144 | * @param int $since |
||
145 | * @param int $limit |
||
146 | * @param bool $previews |
||
147 | * @param string $objectType |
||
148 | * @param int $objectId |
||
149 | * @param string $sort |
||
150 | * @throws InvalidFilterException when the filter is invalid |
||
151 | * @throws \OutOfBoundsException when no user is given |
||
152 | */ |
||
153 | 22 | protected function validateParameters($filter, $since, $limit, $previews, $objectType, $objectId, $sort) { |
|
179 | |||
180 | /** |
||
181 | * @NoAdminRequired |
||
182 | * |
||
183 | * @param int $since |
||
184 | * @param int $limit |
||
185 | * @param bool $previews |
||
186 | * @param string $object_type |
||
187 | * @param int $object_id |
||
188 | * @param string $sort |
||
189 | * @return DataResponse |
||
190 | */ |
||
191 | 2 | public function getDefault($since = 0, $limit = 50, $previews = false, $object_type = '', $object_id = 0, $sort = 'desc') { |
|
194 | |||
195 | /** |
||
196 | * @NoAdminRequired |
||
197 | * |
||
198 | * @param string $filter |
||
199 | * @param int $since |
||
200 | * @param int $limit |
||
201 | * @param bool $previews |
||
202 | * @param string $object_type |
||
203 | * @param int $object_id |
||
204 | * @param string $sort |
||
205 | * @return DataResponse |
||
206 | */ |
||
207 | 2 | public function getFilter($filter, $since = 0, $limit = 50, $previews = false, $object_type = '', $object_id = 0, $sort = 'desc') { |
|
210 | |||
211 | /** |
||
212 | * @param string $filter |
||
213 | * @param int $since |
||
214 | * @param int $limit |
||
215 | * @param bool $previews |
||
216 | * @param string $filterObjectType |
||
217 | * @param int $filterObjectId |
||
218 | * @param string $sort |
||
219 | * @return DataResponse |
||
220 | */ |
||
221 | 11 | protected function get($filter, $since, $limit, $previews, $filterObjectType, $filterObjectId, $sort) { |
|
288 | |||
289 | /** |
||
290 | * @param array $headers |
||
291 | * @param bool $hasMoreActivities |
||
292 | * @param array $data |
||
293 | * @return array |
||
294 | */ |
||
295 | 6 | protected function generateHeaders(array $headers, $hasMoreActivities, array $data) { |
|
327 | |||
328 | /** |
||
329 | * @param string $owner |
||
330 | * @param int $fileId |
||
331 | * @param string $filePath |
||
332 | * @return array |
||
333 | */ |
||
334 | 7 | protected function getPreview($owner, $fileId, $filePath) { |
|
371 | |||
372 | /** |
||
373 | * @param string $filePath |
||
374 | * @param array $info |
||
375 | * @return array |
||
376 | */ |
||
377 | 3 | protected function getPreviewFromPath($filePath, $info) { |
|
387 | |||
388 | /** |
||
389 | * @param string $mimeType |
||
390 | * @return string |
||
391 | */ |
||
392 | 3 | protected function getPreviewPathFromMimeType($mimeType) { |
|
400 | |||
401 | /** |
||
402 | * @param string $path |
||
403 | * @param bool $isDir |
||
404 | * @param string $view |
||
405 | * @return string |
||
406 | */ |
||
407 | 6 | protected function getPreviewLink($path, $isDir, $view) { |
|
420 | } |
||
421 |
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.