| Total Complexity | 57 |
| Total Lines | 279 |
| Duplicated Lines | 0 % |
| Changes | 11 | ||
| 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 | * Register the given JavaScript file as required. |
||
| 82 | * |
||
| 83 | * @param string $file Either relative to docroot or in the form "vendor/package:resource" |
||
| 84 | * @param array $options List of options. Available options include: |
||
| 85 | * - 'provides' : List of scripts files included in this file |
||
| 86 | * - 'async' : Boolean value to set async attribute to script tag |
||
| 87 | * - 'defer' : Boolean value to set defer attribute to script tag (true by default) |
||
| 88 | * - 'type' : Override script type= value. |
||
| 89 | * - 'integrity' : SubResource Integrity hash |
||
| 90 | * - 'crossorigin' : Cross-origin policy for the resource |
||
| 91 | * - 'cookie-consent' : Type of cookie for conditionnal loading : strictly-necessary,functionality,tracking,targeting |
||
| 92 | */ |
||
| 93 | public function javascript($file, $options = array()) |
||
| 94 | { |
||
| 95 | if (!is_array($options)) { |
||
| 96 | $options = []; |
||
| 97 | } |
||
| 98 | if (self::config()->enable_js_modules) { |
||
| 99 | if (empty($options['type']) && self::config()->enable_js_modules) { |
||
| 100 | $options['type'] = 'module'; |
||
| 101 | } |
||
| 102 | // Modules are deferred by default |
||
| 103 | if (isset($options['defer']) && $options['type'] == "module") { |
||
| 104 | unset($options['defer']); |
||
| 105 | } |
||
| 106 | } else { |
||
| 107 | // We want to defer by default, but we can disable it if needed |
||
| 108 | if (!isset($options['defer'])) { |
||
| 109 | $options['defer'] = true; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | if (isset($options['cookie-consent'])) { |
||
| 113 | if (!in_array($options['cookie-consent'], self::listCookieTypes())) { |
||
| 114 | throw new InvalidArgumentException("The cookie-consent value is invalid, it must be one of: strictly-necessary,functionality,tracking,targeting"); |
||
| 115 | } |
||
| 116 | // switch to text plain for conditional loading |
||
| 117 | $options['type'] = 'text/plain'; |
||
| 118 | } |
||
| 119 | parent::javascript($file, $options); |
||
| 120 | if (isset($options['cookie-consent'])) { |
||
| 121 | $this->javascript[$file]['cookie-consent'] = $options['cookie-consent']; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $name |
||
| 127 | * @param string|array $type Pass the type or an array of options |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public function themedJavascript($name, $type = null) |
||
| 131 | { |
||
| 132 | $path = ThemeResourceLoader::inst()->findThemedJavascript($name, SSViewer::get_themes()); |
||
| 133 | if ($path) { |
||
| 134 | $options = []; |
||
| 135 | if ($type) { |
||
| 136 | if (is_string($type)) { |
||
| 137 | $options['type'] = $type; |
||
| 138 | } elseif (is_array($type)) { |
||
| 139 | $options = $type; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | $this->javascript($path, $options); |
||
| 143 | } else { |
||
| 144 | throw new InvalidArgumentException( |
||
| 145 | "The javascript file doesn't exist. Please check if the file $name.js exists in any " |
||
| 146 | . "context or search for themedJavascript references calling this file in your templates." |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get all css files |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getCSS() |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Update the given HTML content with the appropriate include tags for the registered |
||
| 174 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
| 175 | * including a head and body tag. |
||
| 176 | * |
||
| 177 | * @param string $content HTML content that has already been parsed from the $templateFile through {@link SSViewer} |
||
| 178 | * @return string HTML content augmented with the requirements tags |
||
| 179 | */ |
||
| 180 | public function includeInHTML($content) |
||
| 302 |
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.