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