| Total Complexity | 46 |
| Total Lines | 239 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TJavaScript 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 TJavaScript, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class TJavaScript |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Renders a list of javascript files |
||
| 28 | * @param array $files URLs to the javascript files |
||
| 29 | * @return string rendering result |
||
| 30 | */ |
||
| 31 | public static function renderScriptFiles($files) |
||
| 32 | { |
||
| 33 | $str = ''; |
||
| 34 | foreach ($files as $file) { |
||
| 35 | $str .= self::renderScriptFile($file); |
||
| 36 | } |
||
| 37 | return $str; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Renders a javascript file |
||
| 42 | * @param \Prado\Web\Javascripts\TJavaScriptAsset|string $asset URL to the javascript file or TJavaScriptAsset |
||
| 43 | * @return string rendering result |
||
| 44 | */ |
||
| 45 | public static function renderScriptFile($asset) |
||
| 46 | { |
||
| 47 | if (is_object($asset) && ($asset instanceof TJavaScriptAsset)) { |
||
| 48 | return $asset->__toString() . "\n"; |
||
| 49 | } |
||
| 50 | return '<script src="' . THttpUtility::htmlEncode($asset) . "\"></script>\n"; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Renders a list of javascript blocks |
||
| 55 | * @param array $scripts javascript blocks |
||
| 56 | * @return string rendering result |
||
| 57 | */ |
||
| 58 | public static function renderScriptBlocks($scripts) |
||
| 59 | { |
||
| 60 | if (count($scripts)) { |
||
| 61 | return "<script>\n/*<![CDATA[*/\n" . implode("\n", $scripts) . "\n/*]]>*/\n</script>\n"; |
||
| 62 | } else { |
||
| 63 | return ''; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Renders a list of javascript code |
||
| 69 | * @param array $scripts javascript blocks |
||
| 70 | * @return string rendering result |
||
| 71 | */ |
||
| 72 | public static function renderScriptBlocksCallback($scripts) |
||
| 73 | { |
||
| 74 | if (count($scripts)) { |
||
| 75 | return implode("\n", $scripts) . "\n"; |
||
| 76 | } else { |
||
| 77 | return ''; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Renders javascript block |
||
| 83 | * @param string $script javascript block |
||
| 84 | * @return string rendering result |
||
| 85 | */ |
||
| 86 | public static function renderScriptBlock($script) |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Quotes a javascript string. |
||
| 93 | * After processing, the string is safely enclosed within a pair of |
||
| 94 | * quotation marks and can serve as a javascript string. |
||
| 95 | * @param string $js string to be quoted |
||
| 96 | * @return string the quoted string |
||
| 97 | */ |
||
| 98 | public static function quoteString($js) |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param mixed $js |
||
| 105 | * @return TJavaScriptLiteral Marks a string as a javascript function. Once marke, the string is considered as a |
||
| 106 | * raw javascript function that is not supposed to be encoded by {@link encode} |
||
| 107 | */ |
||
| 108 | public static function quoteJsLiteral($js) |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param mixed $js |
||
| 119 | * @return bool true if the parameter is marked as a javascript function, i.e. if it's considered as a |
||
| 120 | * raw javascript function that is not supposed to be encoded by {@link encode} |
||
| 121 | */ |
||
| 122 | public static function isJsLiteral($js) |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Encodes a PHP variable into javascript representation. |
||
| 129 | * |
||
| 130 | * Example: |
||
| 131 | * <code> |
||
| 132 | * $options['onLoading'] = "doit"; |
||
| 133 | * $options['onComplete'] = "more"; |
||
| 134 | * echo TJavaScript::encode($options); |
||
| 135 | * //expects the following javascript code |
||
| 136 | * // {'onLoading':'doit','onComplete':'more'} |
||
| 137 | * </code> |
||
| 138 | * |
||
| 139 | * For higher complexity data structures use {@link jsonEncode} and {@link jsonDecode} |
||
| 140 | * to serialize and unserialize. |
||
| 141 | * |
||
| 142 | * @param mixed $value PHP variable to be encoded |
||
| 143 | * @param bool $toMap whether the output is a map or a list. |
||
| 144 | * @since 3.1.5 |
||
| 145 | * @param bool $encodeEmptyStrings wether to encode empty strings too. Default to false for BC. |
||
| 146 | * @return string the encoded string |
||
| 147 | */ |
||
| 148 | public static function encode($value, $toMap = true, $encodeEmptyStrings = false) |
||
| 206 | } |
||
| 207 | } |
||
| 208 | /** |
||
| 209 | * Encodes a PHP variable into javascript string. |
||
| 210 | * This method invokes json_encode to perform the encoding. |
||
| 211 | * @param mixed $value variable to be encoded |
||
| 212 | * @param mixed $options |
||
| 213 | * @return string encoded string |
||
| 214 | */ |
||
| 215 | public static function jsonEncode($value, $options = 0) |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Encodes an string or the content of an array to UTF8 |
||
| 227 | * @param array|mixed|string $value |
||
| 228 | * @param string $sourceEncoding |
||
| 229 | */ |
||
| 230 | private static function convertToUtf8(&$value, $sourceEncoding) |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Decodes a javascript string into PHP variable. |
||
| 243 | * This method invokes json_decode to perform the decoding. |
||
| 244 | * @param string $value string to be decoded |
||
| 245 | * @param bool $assoc whether to convert returned objects to associative arrays |
||
| 246 | * @param int $depth recursion depth |
||
| 247 | * @return mixed decoded variable |
||
| 248 | */ |
||
| 249 | public static function jsonDecode($value, $assoc = false, $depth = 512) |
||
| 250 | { |
||
| 251 | return json_decode($value, $assoc, $depth, JSON_THROW_ON_ERROR); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Minimize the size of a javascript script. |
||
| 256 | * This method is based on Douglas Crockford's JSMin. |
||
| 257 | * @param string $code code that you want to minimzie |
||
| 258 | * @return string minimized version of the code |
||
| 259 | */ |
||
| 260 | public static function JSMin($code) |
||
| 263 | } |
||
| 264 | } |
||
| 265 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.