Complex classes like OCSEndPoint 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 OCSEndPoint, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class OCSEndPoint { |
||
40 | |||
41 | /** @var string */ |
||
42 | protected $filter; |
||
43 | |||
44 | /** @var int */ |
||
45 | protected $since; |
||
46 | |||
47 | /** @var int */ |
||
48 | protected $limit; |
||
49 | |||
50 | /** @var string */ |
||
51 | protected $sort; |
||
52 | |||
53 | /** @var string */ |
||
54 | protected $objectType; |
||
55 | |||
56 | /** @var int */ |
||
57 | protected $objectId; |
||
58 | |||
59 | /** @var string */ |
||
60 | protected $user; |
||
61 | |||
62 | /** @var bool */ |
||
63 | protected $loadPreviews; |
||
64 | |||
65 | /** @var Data */ |
||
66 | protected $data; |
||
67 | |||
68 | /** @var GroupHelper */ |
||
69 | protected $helper; |
||
70 | |||
71 | /** @var UserSettings */ |
||
72 | protected $settings; |
||
73 | |||
74 | /** @var IRequest */ |
||
75 | protected $request; |
||
76 | |||
77 | /** @var IURLGenerator */ |
||
78 | protected $urlGenerator; |
||
79 | |||
80 | /** @var IUserSession */ |
||
81 | protected $userSession; |
||
82 | |||
83 | /** @var IPreview */ |
||
84 | protected $preview; |
||
85 | |||
86 | /** @var IMimeTypeDetector */ |
||
87 | protected $mimeTypeDetector; |
||
88 | |||
89 | /** @var View */ |
||
90 | protected $view; |
||
91 | |||
92 | /** @var ViewInfoCache */ |
||
93 | protected $infoCache; |
||
94 | |||
95 | /** |
||
96 | * OCSEndPoint constructor. |
||
97 | * |
||
98 | * @param Data $data |
||
99 | * @param GroupHelper $helper |
||
100 | * @param UserSettings $settings |
||
101 | * @param IRequest $request |
||
102 | * @param IURLGenerator $urlGenerator |
||
103 | * @param IUserSession $userSession |
||
104 | * @param IPreview $preview |
||
105 | * @param IMimeTypeDetector $mimeTypeDetector |
||
106 | * @param View $view |
||
107 | * @param ViewInfoCache $infoCache |
||
108 | */ |
||
109 | 61 | public function __construct(Data $data, |
|
130 | |||
131 | /** |
||
132 | * @param array $parameters |
||
133 | * @throws InvalidFilterException when the filter is invalid |
||
134 | * @throws \OutOfBoundsException when no user is given |
||
135 | */ |
||
136 | 23 | protected function readParameters(array $parameters) { |
|
137 | 23 | $this->filter = isset($parameters['filter']) && \is_string($parameters['filter']) ? (string) $parameters['filter'] : 'all'; |
|
138 | 23 | if ($this->filter !== $this->data->validateFilter($this->filter)) { |
|
139 | 2 | throw new InvalidFilterException(); |
|
140 | } |
||
141 | 21 | $this->since = (int) $this->request->getParam('since', 0); |
|
142 | 21 | $this->limit = (int) $this->request->getParam('limit', 50); |
|
143 | 21 | $this->loadPreviews = $this->request->getParam('previews', 'false') === 'true'; |
|
144 | 21 | $this->objectType = (string) $this->request->getParam('object_type', ''); |
|
145 | 21 | $this->objectId = (int) $this->request->getParam('object_id', 0); |
|
146 | 21 | $this->sort = (string) $this->request->getParam('sort', ''); |
|
147 | 21 | $this->sort = \in_array($this->sort, ['asc', 'desc']) ? $this->sort : 'desc'; |
|
148 | |||
149 | 21 | if ($this->objectType !== '' && $this->objectId === 0 || $this->objectType === '' && $this->objectId !== 0) { |
|
150 | // Only allowed together |
||
151 | 2 | $this->objectType = ''; |
|
152 | 2 | $this->objectId = 0; |
|
153 | } |
||
154 | |||
155 | 21 | $user = $this->userSession->getUser(); |
|
156 | 21 | if ($user instanceof IUser) { |
|
|
|||
157 | 20 | $this->user = $user->getUID(); |
|
158 | } else { |
||
159 | // No user logged in |
||
160 | 1 | throw new \OutOfBoundsException(); |
|
161 | } |
||
162 | 20 | } |
|
163 | |||
164 | /** |
||
165 | * @param array $parameters |
||
166 | * @return \OC_OCS_Result |
||
167 | */ |
||
168 | 3 | public function getDefault(array $parameters) { |
|
169 | 3 | return $this->get(\array_merge($parameters, [ |
|
170 | 3 | 'filter' => 'all', |
|
171 | ])); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param array $parameters |
||
176 | * @return \OC_OCS_Result |
||
177 | */ |
||
178 | 3 | public function getFilter(array $parameters) { |
|
181 | |||
182 | /** |
||
183 | * @param array $parameters |
||
184 | * @return \OC_OCS_Result |
||
185 | */ |
||
186 | 9 | protected function get(array $parameters) { |
|
248 | |||
249 | /** |
||
250 | * @param array $headers |
||
251 | * @param bool $hasMoreActivities |
||
252 | * @return array |
||
253 | */ |
||
254 | 4 | protected function generateHeaders(array $headers, $hasMoreActivities) { |
|
280 | |||
281 | /** |
||
282 | * @param string $owner |
||
283 | * @param int $fileId |
||
284 | * @param string $filePath |
||
285 | * @return array |
||
286 | */ |
||
287 | 7 | protected function getPreview($owner, $fileId, $filePath) { |
|
337 | |||
338 | /** |
||
339 | * @param string $filePath |
||
340 | * @param array $info |
||
341 | * @return array |
||
342 | */ |
||
343 | 3 | protected function getPreviewFromPath($filePath, $info) { |
|
353 | |||
354 | /** |
||
355 | * @param string $mimeType |
||
356 | * @return string |
||
357 | */ |
||
358 | 3 | protected function getPreviewPathFromMimeType($mimeType) { |
|
366 | |||
367 | /** |
||
368 | * @param string $path |
||
369 | * @param bool $isDir |
||
370 | * @param string $view |
||
371 | * @return string |
||
372 | */ |
||
373 | 6 | protected function getPreviewLink($path, $isDir, $view) { |
|
386 | } |
||
387 |
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.