Conditions | 13 |
Paths | 12 |
Total Lines | 56 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
53 | public function execute($queue) |
||
54 | { |
||
55 | // Let's save ourselves some trouble and just clear all the caches for this element class |
||
56 | Craft::$app->getTemplateCaches()->deleteCachesByElementType(Asset::class); |
||
57 | |||
58 | // Now find the affected element IDs |
||
59 | /** @var ElementQuery $query */ |
||
60 | $query = Asset::find(); |
||
61 | if (!empty($this->criteria)) { |
||
62 | Craft::configure($query, $this->criteria); |
||
63 | } |
||
64 | $query |
||
65 | ->offset(null) |
||
66 | ->limit(null) |
||
67 | ->orderBy(null); |
||
68 | |||
69 | if (Craft::$app instanceof ConsoleApplication) { |
||
70 | echo $this->description.PHP_EOL; |
||
71 | } |
||
72 | |||
73 | $totalElements = $query->count(); |
||
74 | $currentElement = 0; |
||
75 | |||
76 | /** @var ElementInterface $element */ |
||
77 | foreach ($query->each() as $element) { |
||
78 | // Find each OptimizedImages field and process it |
||
79 | $layout = $element->getFieldLayout(); |
||
80 | if ($layout !== null) { |
||
81 | $fields = $layout->getFields(); |
||
82 | /** @var $field Field */ |
||
83 | foreach ($fields as $field) { |
||
84 | if ($field instanceof OptimizedImagesField && $element instanceof Asset) { |
||
85 | if ($this->fieldId === null || $field->id == $this->fieldId) { |
||
86 | if (Craft::$app instanceof ConsoleApplication) { |
||
87 | echo $currentElement . '/' . $totalElements |
||
88 | . ' - processing asset: ' . $element->title |
||
89 | . ' from field: ' . $field->name . PHP_EOL; |
||
90 | } |
||
91 | try { |
||
92 | ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($field, $element); |
||
93 | } catch (Exception $e) { |
||
94 | Craft::error($e->getMessage(), __METHOD__); |
||
95 | if (Craft::$app instanceof ConsoleApplication) { |
||
96 | echo '[error]: ' |
||
97 | . $e->getMessage() |
||
98 | . ' while processing ' |
||
99 | . $currentElement . '/' . $totalElements |
||
100 | . ' - processing asset: ' . $element->title |
||
101 | . ' from field: ' . $field->name . PHP_EOL; |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | $this->setProgress($queue, $currentElement++ / $totalElements); |
||
109 | } |
||
125 |