Total Complexity | 58 |
Total Lines | 289 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 3 | Features | 0 |
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 |
||
21 | class DeferBackend extends Requirements_Backend |
||
22 | { |
||
23 | use Configurable; |
||
24 | |||
25 | /** |
||
26 | * @config |
||
27 | * @var boolean |
||
28 | */ |
||
29 | private static $enable_js_modules = false; |
||
30 | |||
31 | // It's better to write to the head with defer |
||
32 | public $writeJavascriptToBody = false; |
||
33 | |||
34 | /** |
||
35 | * @return $this |
||
36 | */ |
||
37 | public static function getDeferBackend() |
||
38 | { |
||
39 | $backend = Requirements::backend(); |
||
40 | if (!$backend instanceof self) { |
||
41 | throw new Exception("Requirements backend is currently of class " . get_class($backend)); |
||
42 | } |
||
43 | return $backend; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param Requirements_Backend $oldBackend defaults to current backend |
||
48 | * @return $this |
||
49 | */ |
||
50 | public static function replaceBackend(Requirements_Backend $oldBackend = null) |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | public static function listCookieTypes() |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return array |
||
82 | */ |
||
83 | public static function listScriptTypes() |
||
84 | { |
||
85 | return ['module', 'application/javascript']; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Register the given JavaScript file as required. |
||
90 | * |
||
91 | * @param string $file Either relative to docroot or in the form "vendor/package:resource" |
||
92 | * @param array $options List of options. Available options include: |
||
93 | * - 'provides' : List of scripts files included in this file |
||
94 | * - 'async' : Boolean value to set async attribute to script tag |
||
95 | * - 'defer' : Boolean value to set defer attribute to script tag (true by default) |
||
96 | * - 'type' : Override script type= value. |
||
97 | * - 'integrity' : SubResource Integrity hash |
||
98 | * - 'crossorigin' : Cross-origin policy for the resource |
||
99 | * - 'cookie-consent' : Type of cookie for conditionnal loading : strictly-necessary,functionality,tracking,targeting |
||
100 | */ |
||
101 | public function javascript($file, $options = array()) |
||
102 | { |
||
103 | if (!is_array($options)) { |
||
104 | $options = []; |
||
105 | } |
||
106 | if (self::config()->enable_js_modules) { |
||
107 | if (empty($options['type']) && self::config()->enable_js_modules) { |
||
108 | $options['type'] = 'module'; |
||
109 | } |
||
110 | // Modules are deferred by default |
||
111 | if (isset($options['defer']) && $options['type'] == "module") { |
||
112 | unset($options['defer']); |
||
113 | } |
||
114 | } else { |
||
115 | // We want to defer by default, but we can disable it if needed |
||
116 | if (!isset($options['defer'])) { |
||
117 | $options['defer'] = true; |
||
118 | } |
||
119 | } |
||
120 | if (isset($options['cookie-consent'])) { |
||
121 | if (!in_array($options['cookie-consent'], self::listCookieTypes())) { |
||
122 | throw new InvalidArgumentException("The cookie-consent value is invalid, it must be one of: strictly-necessary,functionality,tracking,targeting"); |
||
123 | } |
||
124 | // switch to text plain for conditional loading |
||
125 | $options['type'] = 'text/plain'; |
||
126 | } |
||
127 | parent::javascript($file, $options); |
||
128 | if (isset($options['cookie-consent'])) { |
||
129 | $this->javascript[$file]['cookie-consent'] = $options['cookie-consent']; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param string $name |
||
135 | * @param string|array $type Pass the type or an array of options |
||
136 | * @return void |
||
137 | */ |
||
138 | public function themedJavascript($name, $type = null) |
||
139 | { |
||
140 | $path = ThemeResourceLoader::inst()->findThemedJavascript($name, SSViewer::get_themes()); |
||
141 | if ($path) { |
||
142 | $options = []; |
||
143 | if ($type) { |
||
144 | if (is_string($type)) { |
||
145 | $options['type'] = $type; |
||
146 | } elseif (is_array($type)) { |
||
147 | $options = $type; |
||
148 | } |
||
149 | } |
||
150 | $this->javascript($path, $options); |
||
151 | } else { |
||
152 | throw new InvalidArgumentException( |
||
153 | "The javascript file doesn't exist. Please check if the file $name.js exists in any " |
||
154 | . "context or search for themedJavascript references calling this file in your templates." |
||
155 | ); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get all css files |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public function getCSS() |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Update the given HTML content with the appropriate include tags for the registered |
||
182 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
183 | * including a head and body tag. |
||
184 | * |
||
185 | * @param string $content HTML content that has already been parsed from the $templateFile through {@link SSViewer} |
||
186 | * @return string HTML content augmented with the requirements tags |
||
187 | */ |
||
188 | public function includeInHTML($content) |
||
310 | } |
||
311 | } |
||
312 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.