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 |
||
| 45 | class PHP_Typography { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * A DOM-based HTML5 parser. |
||
| 49 | * |
||
| 50 | * @var \Masterminds\HTML5 |
||
| 51 | */ |
||
| 52 | private $html5_parser; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The hyphenator cache. |
||
| 56 | * |
||
| 57 | * @var Hyphenator\Cache |
||
| 58 | */ |
||
| 59 | protected $hyphenator_cache; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The node fixes registry. |
||
| 63 | * |
||
| 64 | * @var Registry; |
||
| 65 | */ |
||
| 66 | private $registry; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Sets up a new PHP_Typography object. |
||
| 70 | * |
||
| 71 | * @param Registry $registry Optional. A fix registry instance. Default null, |
||
|
|
|||
| 72 | * meaning the default fixes are used. |
||
| 73 | */ |
||
| 74 | public function __construct( Registry $registry = null ) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Modifies $html according to the defined settings. |
||
| 80 | * |
||
| 81 | * @param string $html A HTML fragment. |
||
| 82 | * @param Settings $settings A settings object. |
||
| 83 | * @param bool $is_title Optional. If the HTML fragment is a title. Default false. |
||
| 84 | * |
||
| 85 | * @return string The processed $html. |
||
| 86 | */ |
||
| 87 | public function process( $html, Settings $settings, $is_title = false ) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Modifies $html according to the defined settings, in a way that is appropriate for RSS feeds |
||
| 93 | * (i.e. excluding processes that may not display well with limited character set intelligence). |
||
| 94 | * |
||
| 95 | * @param string $html A HTML fragment. |
||
| 96 | * @param Settings $settings A settings object. |
||
| 97 | * @param bool $is_title Optional. If the HTML fragment is a title. Default false. |
||
| 98 | * |
||
| 99 | * @return string The processed $html. |
||
| 100 | */ |
||
| 101 | public function process_feed( $html, Settings $settings, $is_title = false ) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Applies specific fixes to all textnodes of the HTML fragment. |
||
| 107 | * |
||
| 108 | * @param string $html A HTML fragment. |
||
| 109 | * @param callable $fixer A callback that applies typography fixes to a single textnode. |
||
| 110 | * @param Settings $settings A settings object. |
||
| 111 | * @param bool $is_title Optional. If the HTML fragment is a title. Default false. |
||
| 112 | * |
||
| 113 | * @return string The processed $html. |
||
| 114 | */ |
||
| 115 | public function process_textnodes( $html, callable $fixer, Settings $settings, $is_title = false ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Determines whether two object arrays intersect. The second array is expected |
||
| 163 | * to use the spl_object_hash for its keys. |
||
| 164 | * |
||
| 165 | * @param array $array1 The keys are ignored. |
||
| 166 | * @param array $array2 This array has to be in the form ( $spl_object_hash => $object ). |
||
| 167 | * |
||
| 168 | * @return boolean |
||
| 169 | */ |
||
| 170 | protected static function arrays_intersect( array $array1, array $array2 ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Applies standard typography fixes to a textnode. |
||
| 182 | * |
||
| 183 | * @param \DOMText $textnode The node to process. |
||
| 184 | * @param Settings $settings The settings to apply. |
||
| 185 | * @param bool $is_title Optional. Default false. |
||
| 186 | */ |
||
| 187 | View Code Duplication | protected function apply_fixes_to_html_node( \DOMText $textnode, Settings $settings, $is_title = false ) { |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Applies typography fixes specific to RSS feeds to a textnode. |
||
| 197 | * |
||
| 198 | * @param \DOMText $textnode The node to process. |
||
| 199 | * @param Settings $settings The settings to apply. |
||
| 200 | * @param bool $is_title Optional. Default false. |
||
| 201 | */ |
||
| 202 | View Code Duplication | protected function apply_fixes_to_feed_node( \DOMText $textnode, Settings $settings, $is_title = false ) { |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Parse HTML5 fragment while ignoring certain warnings for invalid HTML code (e.g. duplicate IDs). |
||
| 214 | * |
||
| 215 | * @param \Masterminds\HTML5 $parser An intialized parser object. |
||
| 216 | * @param string $html The HTML fragment to parse (not a complete document). |
||
| 217 | * @param Settings $settings The settings to apply. |
||
| 218 | * |
||
| 219 | * @return \DOMDocument|null The encoding has already been set to UTF-8. Returns null if there were parsing errors. |
||
| 220 | */ |
||
| 221 | public function parse_html( \Masterminds\HTML5 $parser, $html, Settings $settings ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Silently handle certain HTML parsing errors. |
||
| 251 | * |
||
| 252 | * @param int $errno Error number. |
||
| 253 | * @param string $errstr Error message. |
||
| 254 | * @param string $errfile The file in which the error occurred. |
||
| 255 | * @param int $errline The line in which the error occurred. |
||
| 256 | * @param array $errcontext Calling context. |
||
| 257 | * |
||
| 258 | * @return boolean Returns true if the error was handled, false otherwise. |
||
| 259 | */ |
||
| 260 | public function handle_parsing_errors( $errno, $errstr, $errfile, $errline, array $errcontext ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Retrieves an array of nodes that should be skipped during processing. |
||
| 271 | * |
||
| 272 | * @param \DOMXPath $xpath A valid XPath instance for the DOM to be queried. |
||
| 273 | * @param \DOMNode $initial_node The starting node of the XPath query. |
||
| 274 | * @param Settings $settings The settings to apply. |
||
| 275 | * |
||
| 276 | * @return \DOMNode[] An array of \DOMNode (can be empty). |
||
| 277 | */ |
||
| 278 | public function query_tags_to_ignore( \DOMXPath $xpath, \DOMNode $initial_node, Settings $settings ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Replaces the given node with HTML content. Uses the HTML5 parser. |
||
| 305 | * |
||
| 306 | * @param \DOMNode $node The node to replace. |
||
| 307 | * @param string $content The HTML fragment used to replace the node. |
||
| 308 | * |
||
| 309 | * @return \DOMNode|array An array of \DOMNode containing the new nodes or the old \DOMNode if the replacement failed. |
||
| 310 | */ |
||
| 311 | public function replace_node_with_html( \DOMNode $node, $content ) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Retrieves the fix registry. |
||
| 344 | * |
||
| 345 | * @return Registry |
||
| 346 | */ |
||
| 347 | public function get_registry() { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Retrieves the HTML5 parser instance. |
||
| 357 | * |
||
| 358 | * @return \Masterminds\HTML5 |
||
| 359 | */ |
||
| 360 | public function get_html5_parser() { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Retrieves the hyphenator cache. |
||
| 373 | * |
||
| 374 | * @return Hyphenator\Cache |
||
| 375 | */ |
||
| 376 | public function get_hyphenator_cache() { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Injects an existing Hyphenator\Cache (to facilitate persistent language caching). |
||
| 386 | * |
||
| 387 | * @param Hyphenator\Cache $cache A hyphenator cache instance. |
||
| 388 | */ |
||
| 389 | public function set_hyphenator_cache( Hyphenator\Cache $cache ) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Retrieves the list of valid language plugins in the given directory. |
||
| 400 | * |
||
| 401 | * @param string $path The path in which to look for language plugin files. |
||
| 402 | * |
||
| 403 | * @return string[] An array in the form ( $language_code => $language_name ). |
||
| 404 | */ |
||
| 405 | private static function get_language_plugin_list( $path ) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Retrieves the list of valid hyphenation languages. |
||
| 437 | * |
||
| 438 | * Note that this method reads all the language files on disc, so you should |
||
| 439 | * cache the results if possible. |
||
| 440 | * |
||
| 441 | * @return string[] An array in the form of ( LANG_CODE => LANGUAGE ). |
||
| 442 | */ |
||
| 443 | public static function get_hyphenation_languages() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Retrieves the list of valid diacritic replacement languages. |
||
| 449 | * |
||
| 450 | * Note that this method reads all the language files on disc, so you should |
||
| 451 | * cache the results if possible. |
||
| 452 | * |
||
| 453 | * @return string[] An array in the form of ( LANG_CODE => LANGUAGE ). |
||
| 454 | */ |
||
| 455 | public static function get_diacritic_languages() { |
||
| 458 | } |
||
| 459 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.