| Total Complexity | 53 |
| Total Lines | 262 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ProvisionsAssetsTrait 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 ProvisionsAssetsTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | trait ProvisionsAssetsTrait |
||
| 20 | { |
||
| 21 | use AbstractModuleTraitTrait; |
||
| 22 | use MustBeInitiatedTrait; |
||
| 23 | |||
| 24 | protected ScriptLoaderInterface $scriptLoader; |
||
| 25 | |||
| 26 | protected ScriptPrinterInterface $scriptPrinter; |
||
| 27 | |||
| 28 | protected StyleLoaderInterface $styleLoader; |
||
| 29 | |||
| 30 | protected StylePrinterInterface $stylePrinter; |
||
| 31 | |||
| 32 | protected ?ScriptCollectionInterface $scripts = null; |
||
| 33 | |||
| 34 | protected ?StyleCollectionInterface $styles = null; |
||
| 35 | |||
| 36 | protected ?InlineScriptCollectionInterface $inlineScripts = null; |
||
| 37 | |||
| 38 | protected ?InlineStyleCollectionInterface $inlineStyles = null; |
||
| 39 | |||
| 40 | protected ?ScriptLocalizationCollectionInterface $scriptLocalizations = null; |
||
| 41 | |||
| 42 | protected function getScriptLoader(): ScriptLoaderInterface |
||
| 43 | { |
||
| 44 | return $this->scriptLoader; |
||
| 45 | } |
||
| 46 | |||
| 47 | protected function getScriptPrinter(): ScriptPrinterInterface |
||
| 50 | } |
||
| 51 | |||
| 52 | protected function getStyleLoader(): StyleLoaderInterface |
||
| 53 | { |
||
| 54 | return $this->styleLoader; |
||
| 55 | } |
||
| 56 | |||
| 57 | protected function getStylePrinter(): StylePrinterInterface |
||
| 58 | { |
||
| 59 | return $this->stylePrinter; |
||
| 60 | } |
||
| 61 | |||
| 62 | protected function getScripts(): ?ScriptCollectionInterface |
||
| 63 | { |
||
| 64 | return $this->scripts; |
||
| 65 | } |
||
| 66 | |||
| 67 | protected function getStyles(): ?StyleCollectionInterface |
||
| 68 | { |
||
| 69 | return $this->styles; |
||
| 70 | } |
||
| 71 | |||
| 72 | protected function getInlineScripts(): ?InlineScriptCollectionInterface |
||
| 73 | { |
||
| 74 | return $this->inlineScripts; |
||
| 75 | } |
||
| 76 | |||
| 77 | protected function getInlineStyles(): ?InlineStyleCollectionInterface |
||
| 78 | { |
||
| 79 | return $this->inlineStyles; |
||
| 80 | } |
||
| 81 | |||
| 82 | protected function getScriptLocalizations(): ?ScriptLocalizationCollectionInterface |
||
| 83 | { |
||
| 84 | return $this->scriptLocalizations; |
||
| 85 | } |
||
| 86 | |||
| 87 | protected function init() |
||
| 88 | { |
||
| 89 | $this->scripts = $this->scripts(); |
||
|
|
|||
| 90 | $this->inlineScripts = $this->inlineScripts(); |
||
| 91 | $this->scriptLocalizations = $this->scriptLocalizations(); |
||
| 92 | |||
| 93 | $this->styles = $this->styles(); |
||
| 94 | $this->inlineStyles = $this->inlineStyles(); |
||
| 95 | |||
| 96 | $this->scriptLoader = $this->scriptLoader(); |
||
| 97 | $this->styleLoader = $this->styleLoader(); |
||
| 98 | |||
| 99 | $this->scriptPrinter = $this->scriptPrinter(); |
||
| 100 | $this->stylePrinter = $this->stylePrinter(); |
||
| 101 | |||
| 102 | $this->isInitiated = true; |
||
| 103 | } |
||
| 104 | |||
| 105 | protected function hasScripts(): bool |
||
| 106 | { |
||
| 107 | return ($scripts = $this->getScripts()) |
||
| 108 | && !empty($scripts->getScripts()); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function hasStyles(): bool |
||
| 112 | { |
||
| 113 | return ($styles = $this->getStyles()) |
||
| 114 | && !empty($styles->getStyles()); |
||
| 115 | } |
||
| 116 | |||
| 117 | protected function hasInlineScripts(): bool |
||
| 118 | { |
||
| 119 | return ($inlineScripts = $this->getInlineScripts()) |
||
| 120 | && !empty($inlineScripts->getScripts()); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function hasInlineStyles(): bool |
||
| 124 | { |
||
| 125 | return ($inlineStyles = $this->getInlineStyles()) |
||
| 126 | && !empty($inlineStyles->getStyles()); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function hasScriptLocalizations(): bool |
||
| 133 | } |
||
| 134 | |||
| 135 | protected function provisionAssets(?string $hookSuffix = null): void |
||
| 136 | { |
||
| 137 | $this->maybeInit(); |
||
| 138 | |||
| 139 | $request = $this->getServerRequest(); |
||
| 140 | |||
| 141 | $scriptLoader = $this->getScriptLoader(); |
||
| 142 | $styleLoader = $this->getStyleLoader(); |
||
| 143 | |||
| 144 | if ($hookSuffix) { |
||
| 145 | $request = $request->withAttribute('hook_suffix', $hookSuffix); |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($this->hasScripts()) { |
||
| 149 | $scriptLoader->load($this->getScripts(), $request); |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($this->hasStyles()) { |
||
| 153 | $styleLoader->load($this->getStyles(), $request); |
||
| 154 | } |
||
| 155 | |||
| 156 | if ($this->hasInlineScripts()) { |
||
| 157 | $scriptLoader->support($this->getInlineScripts(), $request); |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($this->hasInlineStyles()) { |
||
| 161 | $styleLoader->support($this->getInlineStyles(), $request); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($this->hasScriptLocalizations()) { |
||
| 165 | $scriptLoader->localize($this->getScriptLocalizations(), $request); |
||
| 166 | } |
||
| 167 | |||
| 168 | $scriptLoader->activate(...$this->activateScripts()); |
||
| 169 | $scriptLoader->deactivate(...$this->deactivateScripts()); |
||
| 170 | |||
| 171 | $styleLoader->activate(...$this->activateStyles()); |
||
| 172 | $styleLoader->deactivate(...$this->deactivateStyles()); |
||
| 173 | } |
||
| 174 | |||
| 175 | protected function filterScriptLoaderTag(string $tag, string $handle, string $src): string |
||
| 176 | { |
||
| 177 | if (!$this->isInitiated() || !$this->hasScripts() || !$this->getScripts()->hasScript($handle)) { |
||
| 178 | return $tag; |
||
| 179 | } |
||
| 180 | |||
| 181 | return $this->getScriptPrinter() |
||
| 182 | ->merge($tag, $this->getScripts()->getScript($handle)); |
||
| 183 | } |
||
| 184 | |||
| 185 | protected function filterStyleLoaderTag(string $tag, string $handle, string $href, string $media): string |
||
| 186 | { |
||
| 187 | if (!$this->isInitiated() || !$this->hasStyles() || !$this->getStyles()->hasStyle($handle)) { |
||
| 188 | return $tag; |
||
| 189 | } |
||
| 190 | |||
| 191 | return $this->getStylePrinter() |
||
| 192 | ->merge($tag, $this->getStyles()->getStyle($handle)); |
||
| 193 | } |
||
| 194 | |||
| 195 | protected function asset(?string $asset = null): string |
||
| 196 | { |
||
| 197 | return $this->extension->url( |
||
| 198 | $this->configCascade($this->assetsConfigCascade()) . $asset |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | |||
| 202 | protected function assetsConfigCascade(): array |
||
| 203 | { |
||
| 204 | return [ |
||
| 205 | 'view.assets.path', 'view.assets', 'theme.assets', |
||
| 206 | ]; |
||
| 207 | } |
||
| 208 | |||
| 209 | protected function version(?string $version = null): string |
||
| 210 | { |
||
| 211 | if ($this->extension->isInDev()) { |
||
| 212 | return (string) time(); |
||
| 213 | } |
||
| 214 | |||
| 215 | return $version ?: $this->extension->getVersion(); |
||
| 216 | } |
||
| 217 | |||
| 218 | protected function scriptLoader(): ScriptLoaderInterface |
||
| 219 | { |
||
| 220 | return new ScriptLoader(); |
||
| 221 | } |
||
| 222 | |||
| 223 | protected function scriptPrinter(): ScriptPrinterInterface |
||
| 224 | { |
||
| 225 | return new ScriptPrinter(); |
||
| 226 | } |
||
| 227 | |||
| 228 | protected function styleLoader(): StyleLoaderInterface |
||
| 231 | } |
||
| 232 | |||
| 233 | protected function stylePrinter(): StylePrinterInterface |
||
| 234 | { |
||
| 235 | return new StylePrinter(); |
||
| 236 | } |
||
| 237 | |||
| 238 | protected function scripts(): ?ScriptCollectionInterface |
||
| 239 | { |
||
| 240 | return null; |
||
| 241 | } |
||
| 242 | |||
| 243 | protected function styles(): ?StyleCollectionInterface |
||
| 244 | { |
||
| 245 | return null; |
||
| 246 | } |
||
| 247 | |||
| 248 | protected function inlineScripts(): ?InlineScriptCollectionInterface |
||
| 249 | { |
||
| 250 | return null; |
||
| 251 | } |
||
| 252 | |||
| 253 | protected function inlineStyles(): ?InlineStyleCollectionInterface |
||
| 256 | } |
||
| 257 | |||
| 258 | protected function scriptLocalizations(): ?ScriptLocalizationCollectionInterface |
||
| 259 | { |
||
| 260 | return null; |
||
| 261 | } |
||
| 262 | |||
| 263 | protected function activateScripts(): array |
||
| 264 | { |
||
| 265 | return []; |
||
| 266 | } |
||
| 267 | |||
| 268 | protected function deactivateScripts(): array |
||
| 271 | } |
||
| 272 | |||
| 273 | protected function activateStyles(): array |
||
| 274 | { |
||
| 275 | return []; |
||
| 276 | } |
||
| 277 | |||
| 278 | protected function deactivateStyles(): array |
||
| 279 | { |
||
| 281 | } |
||
| 282 | } |
||
| 283 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.