Complex classes like Javascript 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Javascript, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | trait Javascript |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * The static String.fromCharCode() method returns a string created by using the specified sequence of Unicode values. |
||
| 11 | * |
||
| 12 | * @return JString |
||
| 13 | */ |
||
| 14 | public static function fromCharCode() |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The static String.fromCodePoint() method returns a string created by using the specified sequence of code points. |
||
| 23 | * |
||
| 24 | * @return $this |
||
| 25 | */ |
||
| 26 | public static function fromCodePoint() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The charAt() method returns the specified character from a string. |
||
| 33 | * |
||
| 34 | * @param int $index |
||
| 35 | * @return static |
||
| 36 | */ |
||
| 37 | 2 | public function charAt($index = 0) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * The charCodeAt() method returns an int between 0 and 65535 |
||
| 44 | * representing the UTF-16 code unit at the given index |
||
| 45 | * (the UTF-16 code unit matches the Unicode code point for code points representable in a single UTF-16 code unit, |
||
| 46 | * but might also be the first code unit of a surrogate pair for code points not representable in a single UTF-16 code unit, |
||
| 47 | * e.g. Unicode code points > 0x10000). If you want the entire code point value, use codePointAt(). |
||
| 48 | * |
||
| 49 | * @param int $index |
||
| 50 | * @return int |
||
| 51 | */ |
||
| 52 | 1 | public function charCodeAt($index = 0) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * The codePointAt() method returns a non-negative int that is the Unicode code point value. |
||
| 59 | * |
||
| 60 | * @param int $index |
||
| 61 | * @return int |
||
| 62 | */ |
||
| 63 | public function codePointAt($index = 0) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The concat() method combines the text of one or more strings and returns a new string. |
||
| 69 | * |
||
| 70 | * @return JString |
||
| 71 | */ |
||
| 72 | 1 | public function concat() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * The endsWith() method determines whether a string ends with the characters of another string, |
||
| 79 | * returning true or false as appropriate. |
||
| 80 | * |
||
| 81 | * @param string $searchString |
||
| 82 | * @param int $position |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | 1 | public function endsWith($searchString, $position = null) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * The includes() method determines whether one string may be found within another string, |
||
| 97 | * returning true or false as appropriate. |
||
| 98 | * |
||
| 99 | * @param string $searchString |
||
| 100 | * @param int $position |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | 1 | public function includes($searchString, $position = 0) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, |
||
| 114 | * starting the search at fromIndex. Returns -1 if the value is not found. |
||
| 115 | * |
||
| 116 | * @param string $searchValue |
||
| 117 | * @param int $fromIndex |
||
| 118 | * @return int |
||
| 119 | */ |
||
| 120 | 2 | public function indexOf($searchValue, $fromIndex = 0) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, |
||
| 133 | * searching backwards from fromIndex. Returns -1 if the value is not found. |
||
| 134 | * |
||
| 135 | * @param string $searchValue |
||
| 136 | * @param int $fromIndex |
||
| 137 | * @return int |
||
| 138 | */ |
||
| 139 | 2 | public function lastIndexOf($searchValue, $fromIndex = null) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * The localeCompare() method returns a number indicating |
||
| 157 | * whether a reference string comes before or after or is the same as the given string in sort order. |
||
| 158 | * |
||
| 159 | * @return int |
||
| 160 | */ |
||
| 161 | public function localeCompare() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * The match() method retrieves the matches when matching a string against a regular expression. |
||
| 167 | * |
||
| 168 | * @param string $regexp |
||
| 169 | * @param int $flag |
||
| 170 | * @param int $offset |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | 1 | public function match($regexp, $flag = PREG_PATTERN_ORDER, $offset = 0) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * The padEnd() method pads the current string with a given string (possibly repeated) |
||
| 184 | * so that the resulting string reaches a given length. The padding is applied from the end (right) of the current string. |
||
| 185 | * |
||
| 186 | * @param int $targetLength |
||
| 187 | * @param string $padString |
||
| 188 | * @return static |
||
| 189 | */ |
||
| 190 | public function padEnd($targetLength, $padString = ' ') |
||
| 194 | |||
| 195 | /** |
||
| 196 | * The padStart() method pads the current string with a given string (eventually repeated) |
||
| 197 | * so that the resulting string reaches a given length. The pad is applied from the start (left) of the current string. |
||
| 198 | * |
||
| 199 | * @param int $targetLength |
||
| 200 | * @param string $padString |
||
| 201 | * @return static |
||
| 202 | */ |
||
| 203 | public function padStart($targetLength, $padString = ' ') |
||
| 207 | |||
| 208 | /** |
||
| 209 | * The repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together. |
||
| 210 | * |
||
| 211 | * @param int $count |
||
| 212 | * @return static |
||
| 213 | */ |
||
| 214 | 1 | public function repeat($count) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. |
||
| 221 | * |
||
| 222 | * @param mixed $regexp |
||
| 223 | * @param mixed $replacement |
||
| 224 | * @return static |
||
| 225 | */ |
||
| 226 | 1 | public function replace($regexp, $replacement = null) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * The search() method executes a search for a match between a regular expression and this String object. |
||
| 241 | * |
||
| 242 | * @param string $regexp |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | 1 | public function search($regexp) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * The slice() method extracts a section of a string and returns a new string. |
||
| 256 | * |
||
| 257 | * @param int $startSlice |
||
| 258 | * @param int $endSlice |
||
| 259 | * @return static |
||
| 260 | */ |
||
| 261 | 2 | public function slice($startSlice, $endSlice = null) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * The split() method splits a String object into an array of strings by separating the string into substrings. |
||
| 278 | * |
||
| 279 | * @param string|null $separator |
||
| 280 | * @return Recca0120\Lodash\Arr |
||
| 281 | */ |
||
| 282 | 1 | public function split($separator = null) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * The startsWith() method determines whether a string begins with the characters of another string, returning true or false as appropriate. |
||
| 293 | * |
||
| 294 | * @param string $searchString |
||
| 295 | * @param int $position |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | 1 | public function startsWith($searchString, $position = 0) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * The substr() method returns the characters in a string beginning at the specified location through the specified number of characters. |
||
| 305 | * |
||
| 306 | * @return static |
||
| 307 | */ |
||
| 308 | 6 | public function substr() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * The substring() method returns a subset of a string between one index and another, or through the end of the string. |
||
| 318 | * |
||
| 319 | * @param int $indexStart |
||
| 320 | * @param int $indexEnd |
||
| 321 | * @return static |
||
| 322 | */ |
||
| 323 | 1 | public function substring($indexStart, $indexEnd = null) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * The toLocaleLowerCase() method returns the calling string value converted to lower case, according to any locale-specific case mappings. |
||
| 337 | * |
||
| 338 | * @return static |
||
| 339 | */ |
||
| 340 | public function toLocaleLowerCase() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * The toLocaleUpperCase() method returns the calling string value converted to upper case, according to any locale-specific case mappings. |
||
| 347 | * |
||
| 348 | * @return static |
||
| 349 | */ |
||
| 350 | public function toLocaleUpperCase() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * The toLowerCase() method returns the calling string value converted to lower case. |
||
| 357 | * |
||
| 358 | * @return static |
||
| 359 | */ |
||
| 360 | 1 | public function toLowerCase() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * The toString() method returns a string representing the specified object. |
||
| 367 | * |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | public function toString() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * The toUpperCase() method returns the calling string value converted to upper case. |
||
| 377 | * |
||
| 378 | * @return static |
||
| 379 | */ |
||
| 380 | 1 | public function toUpperCase() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.). |
||
| 387 | * |
||
| 388 | * @param string $characterMask |
||
| 389 | * @return static |
||
| 390 | */ |
||
| 391 | 1 | public function trim($characterMask = " \t\n\r\0\x0B") |
|
| 395 | |||
| 396 | /** |
||
| 397 | * The trimLeft() method removes whitespace from the left end of a string. |
||
| 398 | * |
||
| 399 | * @param string $characterMask |
||
| 400 | * @return static |
||
| 401 | */ |
||
| 402 | 1 | public function trimLeft($characterMask = " \t\n\r\0\x0B") |
|
| 406 | |||
| 407 | /** |
||
| 408 | * The trimRight() method removes whitespace from the right end of a string. |
||
| 409 | * |
||
| 410 | * @param string $characterMask |
||
| 411 | * @return static |
||
| 412 | */ |
||
| 413 | 1 | public function trimRight($characterMask = " \t\n\r\0\x0B") |
|
| 417 | |||
| 418 | /** |
||
| 419 | * The valueOf() method returns the primitive value of a String object. |
||
| 420 | * |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 1 | public function valueOf() |
|
| 427 | |||
| 428 | /** |
||
| 429 | * length. |
||
| 430 | * |
||
| 431 | * @return int |
||
| 432 | */ |
||
| 433 | 7 | public function length() |
|
| 437 | } |
||
| 438 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.