Total Complexity | 64 |
Total Lines | 512 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BlendableLoader 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.
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 BlendableLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class BlendableLoader |
||
30 | { |
||
31 | /** @var \modX */ |
||
|
|||
32 | protected $modx; |
||
33 | |||
34 | /** @var Blender */ |
||
35 | protected $blender; |
||
36 | |||
37 | /** @var \LCI\MODX\Console\Helpers\UserInteractionHandler */ |
||
38 | protected $userInteractionHandler; |
||
39 | |||
40 | /** |
||
41 | * BlendableLoader constructor. |
||
42 | * @param \modX $modx |
||
43 | * @param Blender $blender |
||
44 | * @param \LCI\MODX\Console\Helpers\UserInteractionHandler $userInteractionHandler |
||
45 | */ |
||
46 | public function __construct(Blender $blender, \modX $modx, UserInteractionHandler $userInteractionHandler) |
||
47 | { |
||
48 | $this->modx = $modx; |
||
49 | $this->blender = $blender; |
||
50 | $this->userInteractionHandler = $userInteractionHandler; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Use this method with your IDE to help manually build a Chunk with PHP |
||
55 | * @param string $name |
||
56 | * @return Chunk |
||
57 | */ |
||
58 | public function getBlendableChunk($name) |
||
59 | { |
||
60 | /** @var \LCI\Blend\Blendable\Chunk $chunk */ |
||
61 | $chunk = new Chunk($this->modx, $this->blender, $name); |
||
62 | return $chunk->setSeedsDir($this->blender->getSeedsDir()); |
||
63 | } |
||
64 | /** |
||
65 | * @param array $chunks |
||
66 | * @param string $seeds_dir |
||
67 | */ |
||
68 | public function blendManyChunks($chunks = [], $seeds_dir = '') |
||
69 | { |
||
70 | // will update if element does exist or create new |
||
71 | foreach ($chunks as $seed_key) { |
||
72 | /** @var \LCI\Blend\Blendable\Chunk $blendChunk */ |
||
73 | $blendChunk = new Chunk($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
74 | |||
75 | $this->blendOneFromMany($blendChunk, $seed_key, 'Chunk', $seeds_dir); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param array $chunks |
||
81 | * @param string $seeds_dir |
||
82 | */ |
||
83 | public function revertBlendManyChunks($chunks = [], $seeds_dir = '') |
||
84 | { |
||
85 | // will update if system setting does exist or create new |
||
86 | foreach ($chunks as $seed_key) { |
||
87 | /** @var \LCI\Blend\Blendable\Chunk $blendChunk */ |
||
88 | $blendChunk = new Chunk($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
89 | |||
90 | $this->revertOneFromMany($blendChunk, $seed_key, 'Chunk', $seeds_dir); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Use this method with your IDE to help manually build a Chunk with PHP |
||
96 | * @param string $key |
||
97 | * @return Context |
||
98 | */ |
||
99 | public function getBlendableContext($key) |
||
100 | { |
||
101 | /** @var \LCI\Blend\Blendable\Context $chunk */ |
||
102 | $context = new Context($this->modx, $this->blender, $key); |
||
103 | return $context->setSeedsDir($this->blender->getSeedsDir()); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param array $contexts |
||
108 | * @param string $seeds_dir |
||
109 | */ |
||
110 | public function blendManyContexts($contexts = [], $seeds_dir = '') |
||
111 | { |
||
112 | // will update if element does exist or create new |
||
113 | foreach ($contexts as $seed_key) { |
||
114 | /** @var \LCI\Blend\Blendable\Context $blendContext */ |
||
115 | $blendContext = new Context($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
116 | |||
117 | $this->blendOneFromMany($blendContext, $seed_key, 'Context', $seeds_dir); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param array $contexts |
||
123 | * @param string $seeds_dir |
||
124 | */ |
||
125 | public function revertBlendManyContexts($contexts = [], $seeds_dir = '') |
||
126 | { |
||
127 | // will update if system setting does exist or create new |
||
128 | foreach ($contexts as $seed_key) { |
||
129 | /** @var \LCI\Blend\Blendable\Context $blendContext */ |
||
130 | $blendContext = new Context($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
131 | |||
132 | $this->revertOneFromMany($blendContext, $seed_key, 'Context', $seeds_dir); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param string $name |
||
138 | * @return \LCI\Blend\Blendable\MediaSource |
||
139 | */ |
||
140 | public function getBlendableMediaSource($name) |
||
141 | { |
||
142 | /** @var \LCI\Blend\Blendable\MediaSource $mediaSource */ |
||
143 | $mediaSource = new MediaSource($this->modx, $this->blender, $name); |
||
144 | return $mediaSource |
||
145 | ->setFieldName($name) |
||
146 | ->setSeedsDir($this->blender->getSeedsDir()); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param array $media_sources |
||
151 | * @param string $seeds_dir |
||
152 | */ |
||
153 | public function blendManyMediaSources($media_sources = [], $seeds_dir = '') |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param array $media_sources |
||
166 | * @param string $seeds_dir |
||
167 | */ |
||
168 | public function revertBlendManyMediaSources($media_sources = [], $seeds_dir = '') |
||
169 | { |
||
170 | // will update if system setting does exist or create new |
||
171 | foreach ($media_sources as $seed_key) { |
||
172 | /** @var \LCI\Blend\Blendable\MediaSource $blendMediaSource */ |
||
173 | $blendMediaSource = new MediaSource($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
174 | |||
175 | $this->revertOneFromMany($blendMediaSource, $seed_key, 'MediaSource', $seeds_dir); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Use this method with your IDE to help manually build a Plugin with PHP |
||
181 | * @param string $name |
||
182 | * @return \LCI\Blend\Blendable\Plugin |
||
183 | */ |
||
184 | public function getBlendablePlugin($name) |
||
185 | { |
||
186 | /** @var \LCI\Blend\Blendable\Plugin $plugin */ |
||
187 | $plugin = new Plugin($this->modx, $this->blender, $name); |
||
188 | return $plugin->setSeedsDir($this->blender->getSeedsDir()); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param array $plugins |
||
193 | * @param string $seeds_dir |
||
194 | */ |
||
195 | public function blendManyPlugins($plugins = [], $seeds_dir = '') |
||
196 | { |
||
197 | // will update if element does exist or create new |
||
198 | foreach ($plugins as $seed_key) { |
||
199 | /** @var \LCI\Blend\Blendable\Plugin $blendPlugin */ |
||
200 | $blendPlugin = new Plugin($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
201 | |||
202 | $this->blendOneFromMany($blendPlugin, $seed_key, 'Plugin', $seeds_dir); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param array $plugins |
||
208 | * @param string $seeds_dir |
||
209 | */ |
||
210 | public function revertBlendManyPlugins($plugins = [], $seeds_dir = '') |
||
211 | { |
||
212 | // will update if system setting does exist or create new |
||
213 | foreach ($plugins as $seed_key) { |
||
214 | /** @var \LCI\Blend\Blendable\Plugin $blendPlugin */ |
||
215 | $blendPlugin = new Plugin($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
216 | |||
217 | $this->revertOneFromMany($blendPlugin, $seed_key, 'Plugin', $seeds_dir); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Use this method with your IDE to help manually build a Snippet with PHP |
||
223 | * @param string $name |
||
224 | * @return \LCI\Blend\Blendable\Snippet |
||
225 | */ |
||
226 | public function getBlendableSnippet($name) |
||
227 | { |
||
228 | /** @var Snippet $snippet */ |
||
229 | $snippet = new Snippet($this->modx, $this->blender, $name); |
||
230 | return $snippet->setSeedsDir($this->blender->getSeedsDir()); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param array $snippets |
||
235 | * @param string $seeds_dir |
||
236 | */ |
||
237 | public function blendManySnippets($snippets = [], $seeds_dir = '') |
||
238 | { |
||
239 | // will update if element does exist or create new |
||
240 | foreach ($snippets as $seed_key) { |
||
241 | /** @var \LCI\Blend\Blendable\Snippet $blendSnippet */ |
||
242 | $blendSnippet = new Snippet($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
243 | |||
244 | $this->blendOneFromMany($blendSnippet, $seed_key, 'Snippet', $seeds_dir); |
||
245 | } |
||
246 | } |
||
247 | /** |
||
248 | * @param array $snippets |
||
249 | * @param string $seeds_dir |
||
250 | */ |
||
251 | public function revertBlendManySnippets($snippets = [], $seeds_dir = '') |
||
252 | { |
||
253 | // will update if system setting does exist or create new |
||
254 | foreach ($snippets as $seed_key) { |
||
255 | /** @var Snippet $blendSnippet */ |
||
256 | $blendSnippet = new Snippet($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
257 | |||
258 | $this->revertOneFromMany($blendSnippet, $seed_key, 'Snippet', $seeds_dir); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Use this method with your IDE to manually build a template |
||
264 | * @param string $name |
||
265 | * @return \LCI\Blend\Blendable\Template |
||
266 | */ |
||
267 | public function getBlendableTemplate($name) |
||
268 | { |
||
269 | /** @var \LCI\Blend\Blendable\Template $template */ |
||
270 | $template = new Template($this->modx, $this->blender, $name); |
||
271 | return $template->setSeedsDir($this->blender->getSeedsDir()); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @param array $templates |
||
276 | * @param string $seeds_dir |
||
277 | * @param bool $overwrite |
||
278 | */ |
||
279 | public function blendManyTemplates($templates = [], $seeds_dir = '', $overwrite = false) |
||
280 | { |
||
281 | // will update if template does exist or create new |
||
282 | foreach ($templates as $seed_key) { |
||
283 | |||
284 | /** @var \LCI\Blend\Blendable\Template $blendTemplate */ |
||
285 | $blendTemplate = new Template($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
286 | |||
287 | $this->blendOneFromMany($blendTemplate, $seed_key, 'Template', $seeds_dir, $overwrite); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param array $templates |
||
293 | * @param string $seeds_dir |
||
294 | */ |
||
295 | public function revertBlendManyTemplates($templates = [], $seeds_dir = '') |
||
296 | { |
||
297 | // will update if system setting does exist or create new |
||
298 | foreach ($templates as $seed_key) { |
||
299 | /** @var \LCI\Blend\Blendable\Template $blendTemplate */ |
||
300 | $blendTemplate = new Template($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
301 | |||
302 | $this->revertOneFromMany($blendTemplate, $seed_key, 'Template', $seeds_dir); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Use this method with your IDE to manually build a template variable |
||
308 | * @param string $name |
||
309 | * @return TemplateVariable |
||
310 | */ |
||
311 | public function getBlendableTemplateVariable($name) |
||
312 | { |
||
313 | /** @var \LCI\Blend\Blendable\TemplateVariable $tv */ |
||
314 | $tv = new TemplateVariable($this->modx, $this->blender, $name); |
||
315 | return $tv->setSeedsDir($this->blender->getSeedsDir()); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param string $alias |
||
320 | * @param string $context |
||
321 | * @return \LCI\Blend\Blendable\Resource |
||
322 | */ |
||
323 | public function getBlendableResource($alias, $context = 'web') |
||
324 | { |
||
325 | /** @var \LCI\Blend\Blendable\Resource $resource */ |
||
326 | $resource = new Resource($this->modx, $this->blender, $alias, $context); |
||
327 | return $resource |
||
328 | ->setSeedsDir($this->blender->getSeedsDir()); |
||
329 | } |
||
330 | /** |
||
331 | * @param array $resources |
||
332 | * @param string $seeds_dir |
||
333 | * @param bool $overwrite |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | public function blendManyResources($resources = [], $seeds_dir = '', $overwrite = false) |
||
338 | { |
||
339 | $saved = true; |
||
340 | // will update if resource does exist or create new |
||
341 | foreach ($resources as $context => $seeds) { |
||
342 | |||
343 | foreach ($seeds as $seed_key) { |
||
344 | /** @var \LCI\Blend\Blendable\Resource $blendResource */ |
||
345 | $blendResource = new Resource($this->modx, $this->blender, $this->blender->getAliasFromSeedKey($seed_key), $context); |
||
346 | |||
347 | $this->blendOneFromMany($blendResource, $seed_key, 'Resource', $seeds_dir, $overwrite); |
||
348 | } |
||
349 | |||
350 | } |
||
351 | |||
352 | return $saved; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @param array $resources |
||
357 | * @param string $seeds_dir |
||
358 | * |
||
359 | * @return bool |
||
360 | */ |
||
361 | public function revertBlendManyResources($resources = [], $seeds_dir = '') |
||
362 | { |
||
363 | $saved = true; |
||
364 | // will update if resource does exist or create new |
||
365 | foreach ($resources as $context => $seeds) { |
||
366 | |||
367 | foreach ($seeds as $seed_key) { |
||
368 | /** @var \LCI\Blend\Blendable\Resource $blendResource */ |
||
369 | $blendResource = new Resource($this->modx, $this->blender, $this->blender->getAliasFromSeedKey($seed_key), $context); |
||
370 | |||
371 | if(!$this->revertOneFromMany($blendResource, $seed_key, 'Resource', $seeds_dir)) { |
||
372 | $saved = false; |
||
373 | } |
||
374 | } |
||
375 | |||
376 | } |
||
377 | |||
378 | return $saved; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @param string $key |
||
383 | * @return \LCI\Blend\Blendable\SystemSetting |
||
384 | */ |
||
385 | public function getBlendableSystemSetting($key = '') |
||
386 | { |
||
387 | /** @var \LCI\Blend\Blendable\SystemSetting $systemSetting */ |
||
388 | $systemSetting = new SystemSetting($this->modx, $this->blender, $key); |
||
389 | return $systemSetting->setSeedsDir($this->blender->getSeedsDir()); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * @param array $settings ~ [ ['name' => 'mySystemSetting', 'value' => 'myValue'], ..] |
||
394 | * @param string $seeds_dir |
||
395 | * |
||
396 | * @return bool |
||
397 | */ |
||
398 | public function blendManySystemSettings($settings = [], $seeds_dir = '') |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @param array $settings ~ [ ['name' => 'mySystemSetting', 'value' => 'myValue'], ..] |
||
439 | * @param string $seeds_dir |
||
440 | * |
||
441 | * @return bool |
||
442 | */ |
||
443 | public function revertBlendManySystemSettings($settings = [], $seeds_dir = '') |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @param Blendable $blendable |
||
487 | * @param string $seed_key |
||
488 | * @param string $object_type |
||
489 | * @param string $seeds_dir |
||
490 | * @param bool $overwrite |
||
491 | * |
||
492 | * @return bool |
||
493 | */ |
||
494 | protected function blendOneFromMany($blendable, $seed_key, $object_type, $seeds_dir, $overwrite = false) |
||
495 | { |
||
496 | if (!empty($seeds_dir)) { |
||
497 | $blendable->setSeedsDir($seeds_dir); |
||
498 | } |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * @param Blendable $blendable |
||
521 | * @param string $seed_key |
||
522 | * @param string $object_type |
||
523 | * @param string $seeds_dir |
||
524 | * |
||
525 | * @return bool |
||
526 | */ |
||
527 | protected function revertOneFromMany($blendable, $seed_key, $object_type, $seeds_dir) |
||
541 | } |
||
542 | |||
543 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths