Total Complexity | 63 |
Total Lines | 314 |
Duplicated Lines | 0 % |
Changes | 15 | ||
Bugs | 3 | Features | 1 |
Complex classes like DeferBackend 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 DeferBackend, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class DeferBackend extends Requirements_Backend |
||
23 | { |
||
24 | use Configurable; |
||
25 | |||
26 | /** |
||
27 | * @config |
||
28 | * @var boolean |
||
29 | */ |
||
30 | private static $enable_js_modules = false; |
||
31 | |||
32 | // It's better to write to the head with defer |
||
33 | /** |
||
34 | * @var boolean |
||
35 | */ |
||
36 | public $writeJavascriptToBody = false; |
||
37 | |||
38 | /** |
||
39 | * @return DeferBackend |
||
40 | */ |
||
41 | public static function getDeferBackend() |
||
42 | { |
||
43 | $backend = Requirements::backend(); |
||
44 | if (!($backend instanceof DeferBackend)) { |
||
45 | throw new Exception("Requirements backend is currently of class " . get_class($backend)); |
||
46 | } |
||
47 | return $backend; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param Requirements_Backend $oldBackend defaults to current backend |
||
52 | * @return DeferBackend |
||
53 | */ |
||
54 | public static function replaceBackend(Requirements_Backend $oldBackend = null) |
||
55 | { |
||
56 | if ($oldBackend === null) { |
||
57 | $oldBackend = Requirements::backend(); |
||
58 | } |
||
59 | $deferBackend = new self(); |
||
60 | foreach ($oldBackend->getCSS() as $file => $opts) { |
||
61 | $deferBackend->css($file, null, $opts); |
||
62 | } |
||
63 | foreach ($oldBackend->getJavascript() as $file => $opts) { |
||
64 | // Old scripts may get defer=false even if the option is not passed due to no null state |
||
65 | unset($opts['defer']); |
||
66 | $deferBackend->javascript($file, $opts); |
||
67 | } |
||
68 | foreach ($oldBackend->getCustomCSS() as $id => $script) { |
||
69 | $deferBackend->customCSS($script, $id); |
||
70 | } |
||
71 | foreach ($oldBackend->getCustomScripts() as $id => $script) { |
||
72 | $deferBackend->customScript($script, $id); |
||
73 | } |
||
74 | Requirements::set_backend($deferBackend); |
||
75 | return $deferBackend; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return array<string> |
||
80 | */ |
||
81 | public static function listCookieTypes() |
||
82 | { |
||
83 | return ['strictly-necessary', 'functionality', 'tracking', 'targeting']; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Register the given JavaScript file as required. |
||
88 | * |
||
89 | * @param string $file Either relative to docroot or in the form "vendor/package:resource" |
||
90 | * @param array<string,mixed> $options List of options. Available options include: |
||
91 | * - 'provides' : List of scripts files included in this file |
||
92 | * - 'async' : Boolean value to set async attribute to script tag |
||
93 | * - 'defer' : Boolean value to set defer attribute to script tag (true by default) |
||
94 | * - 'type' : Override script type= value. |
||
95 | * - 'integrity' : SubResource Integrity hash |
||
96 | * - 'crossorigin' : Cross-origin policy for the resource |
||
97 | * - 'cookie-consent' : Type of cookie for conditionnal loading : strictly-necessary,functionality,tracking,targeting |
||
98 | * - 'nomodule' : Boolean value to set nomodule attribute to script tag |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public function javascript($file, $options = []) |
||
103 | { |
||
104 | if (!is_array($options)) { |
||
|
|||
105 | $options = []; |
||
106 | } |
||
107 | if (self::config()->enable_js_modules) { |
||
108 | if (empty($options['type']) && self::config()->enable_js_modules) { |
||
109 | $options['type'] = 'module'; |
||
110 | } |
||
111 | // Modules are deferred by default |
||
112 | if (isset($options['defer']) && $options['type'] == "module") { |
||
113 | unset($options['defer']); |
||
114 | } |
||
115 | } else { |
||
116 | // We want to defer by default, but we can disable it if needed |
||
117 | if (!isset($options['defer'])) { |
||
118 | $options['defer'] = true; |
||
119 | } |
||
120 | } |
||
121 | if (isset($options['cookie-consent'])) { |
||
122 | if (!in_array($options['cookie-consent'], self::listCookieTypes())) { |
||
123 | throw new InvalidArgumentException("The cookie-consent value is invalid, it must be one of: strictly-necessary,functionality,tracking,targeting"); |
||
124 | } |
||
125 | // switch to text plain for conditional loading |
||
126 | $options['type'] = 'text/plain'; |
||
127 | } |
||
128 | if (isset($options['nomodule'])) { |
||
129 | // Force type regardless of global setting |
||
130 | $options['type'] = 'application/javascript'; |
||
131 | } |
||
132 | parent::javascript($file, $options); |
||
133 | |||
134 | $resolvedFile = ModuleResourceLoader::singleton()->resolvePath($file); |
||
135 | // Parent call doesn't store all attributes, so we adjust ourselves |
||
136 | if (isset($options['cookie-consent'])) { |
||
137 | $this->javascript[$resolvedFile]['cookie-consent'] = $options['cookie-consent']; |
||
138 | } |
||
139 | if (isset($options['nomodule'])) { |
||
140 | $this->javascript[$resolvedFile]['nomodule'] = $options['nomodule']; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param string $name |
||
146 | * @param mixed $type Pass the type or an array of options |
||
147 | * @return void |
||
148 | */ |
||
149 | public function themedJavascript($name, $type = null) |
||
150 | { |
||
151 | if ($type !== null && (!is_string($type) && !is_array($type))) { |
||
152 | throw new InvalidArgumentException("Type must be a string or an array"); |
||
153 | } |
||
154 | $path = ThemeResourceLoader::inst()->findThemedJavascript($name, SSViewer::get_themes()); |
||
155 | if ($path) { |
||
156 | $options = []; |
||
157 | if ($type) { |
||
158 | if (is_string($type)) { |
||
159 | $options['type'] = $type; |
||
160 | } else { |
||
161 | $options = $type; |
||
162 | } |
||
163 | } |
||
164 | $this->javascript($path, $options); |
||
165 | } else { |
||
166 | throw new InvalidArgumentException( |
||
167 | "The javascript file doesn't exist. Please check if the file $name.js exists in any " |
||
168 | . "context or search for themedJavascript references calling this file in your templates." |
||
169 | ); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Get all css files |
||
175 | * |
||
176 | * @return array<string,mixed> |
||
177 | */ |
||
178 | public function getCSS() |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Update the given HTML content with the appropriate include tags for the registered |
||
196 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
197 | * including a head and body tag. |
||
198 | * |
||
199 | * @param string $content HTML content that has already been parsed from the $templateFile through {@link SSViewer} |
||
200 | * @return string HTML content augmented with the requirements tags |
||
201 | */ |
||
202 | public function includeInHTML($content) |
||
336 | } |
||
337 | } |
||
338 |