We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PHP_Typography 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 PHP_Typography, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class PHP_Typography { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * A DOM-based HTML5 parser. |
||
| 50 | * |
||
| 51 | * @var \Masterminds\HTML5 |
||
| 52 | */ |
||
| 53 | private $html5_parser; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The hyphenator cache. |
||
| 57 | * |
||
| 58 | * @var Hyphenator\Cache |
||
| 59 | */ |
||
| 60 | protected $hyphenator_cache; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The node fixes registry. |
||
| 64 | * |
||
| 65 | * @var Registry|null; |
||
| 66 | */ |
||
| 67 | private $registry; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Whether the Hyphenator\Cache of the $registry needs to be updated. |
||
| 71 | * |
||
| 72 | * @var bool |
||
| 73 | */ |
||
| 74 | private $update_registry_cache; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Sets up a new PHP_Typography object. |
||
| 78 | * |
||
| 79 | * @param Registry|null $registry Optional. A fix registry instance. Default null, |
||
| 80 | * meaning the default fixes are used. |
||
| 81 | */ |
||
| 82 | public function __construct( Registry $registry = null ) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Modifies $html according to the defined settings. |
||
| 89 | * |
||
| 90 | * @param string $html A HTML fragment. |
||
| 91 | * @param Settings $settings A settings object. |
||
| 92 | * @param bool $is_title Optional. If the HTML fragment is a title. Default false. |
||
| 93 | * |
||
| 94 | * @return string The processed $html. |
||
| 95 | */ |
||
| 96 | public function process( $html, Settings $settings, $is_title = false ) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Modifies $html according to the defined settings, in a way that is appropriate for RSS feeds |
||
| 104 | * (i.e. excluding processes that may not display well with limited character set intelligence). |
||
| 105 | * |
||
| 106 | * @param string $html A HTML fragment. |
||
| 107 | * @param Settings $settings A settings object. |
||
| 108 | * @param bool $is_title Optional. If the HTML fragment is a title. Default false. |
||
| 109 | * |
||
| 110 | * @return string The processed $html. |
||
| 111 | */ |
||
| 112 | public function process_feed( $html, Settings $settings, $is_title = false ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Applies specific fixes to all textnodes of the HTML fragment. |
||
| 120 | * |
||
| 121 | * @param string $html A HTML fragment. |
||
| 122 | * @param callable $fixer A callback that applies typography fixes to a single textnode. |
||
| 123 | * @param Settings $settings A settings object. |
||
| 124 | * @param bool $is_title Optional. If the HTML fragment is a title. Default false. |
||
| 125 | * |
||
| 126 | * @return string The processed $html. |
||
| 127 | */ |
||
| 128 | public function process_textnodes( $html, callable $fixer, Settings $settings, $is_title = false ) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Determines whether two object arrays intersect. The second array is expected |
||
| 176 | * to use the spl_object_hash for its keys. |
||
| 177 | * |
||
| 178 | * @param array $array1 The keys are ignored. |
||
| 179 | * @param array $array2 This array has to be in the form ( $spl_object_hash => $object ). |
||
| 180 | * |
||
| 181 | * @return boolean |
||
| 182 | */ |
||
| 183 | protected static function arrays_intersect( array $array1, array $array2 ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Parse HTML5 fragment while ignoring certain warnings for invalid HTML code (e.g. duplicate IDs). |
||
| 195 | * |
||
| 196 | * @param \Masterminds\HTML5 $parser An intialized parser object. |
||
| 197 | * @param string $html The HTML fragment to parse (not a complete document). |
||
| 198 | * @param Settings $settings The settings to apply. |
||
| 199 | * |
||
| 200 | * @return \DOMDocument|null The encoding has already been set to UTF-8. Returns null if there were parsing errors. |
||
| 201 | */ |
||
| 202 | public function parse_html( \Masterminds\HTML5 $parser, $html, Settings $settings ) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Silently handle certain HTML parsing errors. |
||
| 232 | * |
||
| 233 | * @param int $errno Error number. |
||
| 234 | * @param string $errstr Error message. |
||
| 235 | * @param string $errfile The file in which the error occurred. |
||
| 236 | * @param int $errline The line in which the error occurred. |
||
| 237 | * @param array $errcontext Calling context. |
||
| 238 | * |
||
| 239 | * @return boolean Returns true if the error was handled, false otherwise. |
||
| 240 | */ |
||
| 241 | public function handle_parsing_errors( $errno, $errstr, $errfile, $errline, array $errcontext ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Retrieves an array of nodes that should be skipped during processing. |
||
| 252 | * |
||
| 253 | * @param \DOMXPath $xpath A valid XPath instance for the DOM to be queried. |
||
| 254 | * @param \DOMNode $initial_node The starting node of the XPath query. |
||
| 255 | * @param Settings $settings The settings to apply. |
||
| 256 | * |
||
| 257 | * @return \DOMNode[] An array of \DOMNode (can be empty). |
||
| 258 | */ |
||
| 259 | public function query_tags_to_ignore( \DOMXPath $xpath, \DOMNode $initial_node, Settings $settings ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Replaces the given node with HTML content. Uses the HTML5 parser. |
||
| 286 | * |
||
| 287 | * @param \DOMNode $node The node to replace. |
||
| 288 | * @param string $content The HTML fragment used to replace the node. |
||
| 289 | * |
||
| 290 | * @return \DOMNode|array An array of \DOMNode containing the new nodes or the old \DOMNode if the replacement failed. |
||
| 291 | */ |
||
| 292 | public function replace_node_with_html( \DOMNode $node, $content ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Retrieves the fix registry. |
||
| 325 | * |
||
| 326 | * @return Registry |
||
| 327 | */ |
||
| 328 | public function get_registry() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Retrieves the HTML5 parser instance. |
||
| 341 | * |
||
| 342 | * @return \Masterminds\HTML5 |
||
| 343 | */ |
||
| 344 | public function get_html5_parser() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Retrieves the hyphenator cache. |
||
| 357 | * |
||
| 358 | * @return Hyphenator\Cache |
||
| 359 | */ |
||
| 360 | public function get_hyphenator_cache() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Injects an existing Hyphenator\Cache (to facilitate persistent language caching). |
||
| 370 | * |
||
| 371 | * @param Hyphenator\Cache $cache A hyphenator cache instance. |
||
| 372 | */ |
||
| 373 | public function set_hyphenator_cache( Hyphenator\Cache $cache ) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Retrieves the list of valid language plugins in the given directory. |
||
| 384 | * |
||
| 385 | * @param string $path The path in which to look for language plugin files. |
||
| 386 | * |
||
| 387 | * @return string[] An array in the form ( $language_code => $language_name ). |
||
| 388 | */ |
||
| 389 | private static function get_language_plugin_list( $path ) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Retrieves the list of valid hyphenation languages. |
||
| 421 | * |
||
| 422 | * Note that this method reads all the language files on disc, so you should |
||
| 423 | * cache the results if possible. |
||
| 424 | * |
||
| 425 | * @return string[] An array in the form of ( LANG_CODE => LANGUAGE ). |
||
| 426 | */ |
||
| 427 | public static function get_hyphenation_languages() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Retrieves the list of valid diacritic replacement languages. |
||
| 433 | * |
||
| 434 | * Note that this method reads all the language files on disc, so you should |
||
| 435 | * cache the results if possible. |
||
| 436 | * |
||
| 437 | * @return string[] An array in the form of ( LANG_CODE => LANGUAGE ). |
||
| 438 | */ |
||
| 439 | public static function get_diacritic_languages() { |
||
| 442 | } |
||
| 443 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.