Complex classes like ProviderService 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 ProviderService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class ProviderService implements IProviderService { |
||
54 | |||
55 | |||
56 | /** @var AppManager */ |
||
57 | private $appManager; |
||
58 | |||
59 | /** @var ConfigService */ |
||
60 | private $configService; |
||
61 | |||
62 | /** @var MiscService */ |
||
63 | private $miscService; |
||
64 | |||
65 | /** @var ProviderWrapper[] */ |
||
66 | private $providers = []; |
||
67 | |||
68 | /** @var bool */ |
||
69 | private $providersLoaded = false; |
||
70 | |||
71 | |||
72 | /** |
||
73 | * ProviderService constructor. |
||
74 | * |
||
75 | * @param AppManager $appManager |
||
76 | * @param ConfigService $configService |
||
77 | * @param MiscService $miscService |
||
78 | */ |
||
79 | public function __construct( |
||
86 | |||
87 | |||
88 | /** |
||
89 | * Load all FullTextSearchProviders set in any info.xml file |
||
90 | * |
||
91 | * @throws Exception |
||
92 | */ |
||
93 | private function loadProviders() { |
||
94 | if ($this->providersLoaded) { |
||
95 | return; |
||
96 | } |
||
97 | |||
98 | try { |
||
99 | $apps = $this->appManager->getInstalledApps(); |
||
100 | foreach ($apps as $appId) { |
||
101 | $this->loadProvidersFromApp($appId); |
||
102 | } |
||
103 | } catch (Exception $e) { |
||
104 | $this->miscService->log($e->getMessage()); |
||
105 | } |
||
106 | |||
107 | $this->providersLoaded = true; |
||
108 | } |
||
109 | |||
110 | |||
111 | /** |
||
112 | * @param string $appId |
||
113 | * @param string $providerId |
||
114 | * |
||
115 | * @throws ProviderIsNotCompatibleException |
||
116 | * @throws ProviderIsNotUniqueException |
||
117 | * @throws QueryException |
||
118 | */ |
||
119 | public function loadProvider(string $appId, string $providerId) { |
||
138 | |||
139 | |||
140 | /** |
||
141 | * @return ProviderWrapper[] |
||
142 | * @throws Exception |
||
143 | */ |
||
144 | public function getProviders(): array { |
||
149 | |||
150 | /** |
||
151 | * @return IFullTextSearchProvider[] |
||
152 | * @throws Exception |
||
153 | */ |
||
154 | public function getConfiguredProviders(): array { |
||
167 | |||
168 | |||
169 | /** |
||
170 | * @param array $providerList |
||
171 | * |
||
172 | * @return IFullTextSearchProvider[] |
||
173 | * @throws Exception |
||
174 | * @throws ProviderDoesNotExistException |
||
175 | */ |
||
176 | public function getFilteredProviders(array $providerList): array { |
||
194 | |||
195 | |||
196 | /** |
||
197 | * @param string $providerId |
||
198 | * |
||
199 | * @return ProviderWrapper |
||
200 | * @throws Exception |
||
201 | * @throws ProviderDoesNotExistException |
||
202 | */ |
||
203 | public function getProvider(string $providerId): ProviderWrapper { |
||
215 | |||
216 | |||
217 | /** |
||
218 | * @param string $providerId |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function isProviderIndexed(string $providerId): bool { |
||
238 | |||
239 | |||
240 | /** |
||
241 | * @param IFullTextSearchProvider $provider |
||
242 | * @param bool $boolean |
||
243 | */ |
||
244 | public function setProviderAsIndexed(IFullTextSearchProvider $provider, bool $boolean) { |
||
249 | |||
250 | |||
251 | /** |
||
252 | * |
||
253 | */ |
||
254 | public function setProvidersAsNotIndexed() { |
||
257 | |||
258 | |||
259 | /** |
||
260 | * @param string $appId |
||
261 | * |
||
262 | * @throws ProviderIsNotCompatibleException |
||
263 | * @throws ProviderIsNotUniqueException |
||
264 | * @throws QueryException |
||
265 | */ |
||
266 | private function loadProvidersFromApp(string $appId) { |
||
281 | |||
282 | |||
283 | /** |
||
284 | * @param string $appId |
||
285 | * @param array $providers |
||
286 | * |
||
287 | * @throws ProviderIsNotCompatibleException |
||
288 | * @throws ProviderIsNotUniqueException |
||
289 | * @throws QueryException |
||
290 | */ |
||
291 | private function loadProvidersFromList(string $appId, array $providers) { |
||
311 | |||
312 | |||
313 | /** |
||
314 | * @param IFullTextSearchProvider $provider |
||
315 | * |
||
316 | * @throws ProviderIsNotUniqueException |
||
317 | * @throws Exception |
||
318 | */ |
||
319 | private function providerIdMustBeUnique(IFullTextSearchProvider $provider) { |
||
329 | |||
330 | |||
331 | /** |
||
332 | * @param IFullTextSearchProvider[] $providers |
||
333 | * |
||
334 | * @return array |
||
335 | */ |
||
336 | public function serialize(array $providers): array { |
||
347 | |||
348 | |||
349 | /** |
||
350 | * |
||
351 | */ |
||
352 | public function addJavascriptAPI() { |
||
361 | |||
362 | |||
363 | } |
||
364 |
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.