Complex classes like LinkRenderer 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 LinkRenderer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class LinkRenderer { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Whether to force the pretty article path |
||
| 45 | * |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | private $forceArticlePath = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * A PROTO_* constant or false |
||
| 52 | * |
||
| 53 | * @var string|bool|int |
||
| 54 | */ |
||
| 55 | private $expandUrls = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | private $stubThreshold = 0; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var TitleFormatter |
||
| 64 | */ |
||
| 65 | private $titleFormatter; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var LinkCache |
||
| 69 | */ |
||
| 70 | private $linkCache; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Whether to run the legacy Linker hooks |
||
| 74 | * |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | private $runLegacyBeginHook = true; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param TitleFormatter $titleFormatter |
||
| 81 | * @param LinkCache $linkCache |
||
| 82 | */ |
||
| 83 | public function __construct( TitleFormatter $titleFormatter, LinkCache $linkCache ) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param bool $force |
||
| 90 | */ |
||
| 91 | public function setForceArticlePath( $force ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function getForceArticlePath() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string|bool|int $expand A PROTO_* constant or false |
||
| 104 | */ |
||
| 105 | public function setExpandURLs( $expand ) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return string|bool|int a PROTO_* constant or false |
||
| 111 | */ |
||
| 112 | public function getExpandURLs() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param int $threshold |
||
| 118 | */ |
||
| 119 | public function setStubThreshold( $threshold ) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function getStubThreshold() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param bool $run |
||
| 132 | */ |
||
| 133 | public function setRunLegacyBeginHook( $run ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param LinkTarget $target |
||
| 139 | * @param string|HtmlArmor|null $text |
||
| 140 | * @param array $extraAttribs |
||
| 141 | * @param array $query |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function makeLink( |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Get the options in the legacy format |
||
| 157 | * |
||
| 158 | * @param bool $isKnown Whether the link is known or broken |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | private function getLegacyOptions( $isKnown ) { |
||
| 176 | |||
| 177 | private function runBeginHook( LinkTarget $target, &$text, &$extraAttribs, &$query, $isKnown ) { |
||
| 188 | |||
| 189 | private function runLegacyBeginHook( LinkTarget $target, &$text, &$extraAttribs, &$query, |
||
| 235 | |||
| 236 | /** |
||
| 237 | * If you have already looked up the proper CSS classes using LinkRenderer::getLinkClasses() |
||
| 238 | * or some other method, use this to avoid looking it up again. |
||
| 239 | * |
||
| 240 | * @param LinkTarget $target |
||
| 241 | * @param string|HtmlArmor|null $text |
||
| 242 | * @param string $classes CSS classes to add |
||
| 243 | * @param array $extraAttribs |
||
| 244 | * @param array $query |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function makePreloadedLink( |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param LinkTarget $target |
||
| 276 | * @param string|HtmlArmor|null $text |
||
| 277 | * @param array $extraAttribs |
||
| 278 | * @param array $query |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function makeKnownLink( |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param LinkTarget $target |
||
| 304 | * @param string|HtmlArmor|null $text |
||
| 305 | * @param array $extraAttribs |
||
| 306 | * @param array $query |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function makeBrokenLink( |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Builds the final <a> element |
||
| 353 | * |
||
| 354 | * @param LinkTarget $target |
||
| 355 | * @param string|HtmlArmor $text |
||
| 356 | * @param array $attribs |
||
| 357 | * @param bool $isKnown |
||
| 358 | * @return null|string |
||
| 359 | */ |
||
| 360 | private function buildAElement( LinkTarget $target, $text, array $attribs, $isKnown ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param LinkTarget $target |
||
| 387 | * @return string non-escaped text |
||
| 388 | */ |
||
| 389 | private function getLinkText( LinkTarget $target ) { |
||
| 399 | |||
| 400 | private function getLinkURL( LinkTarget $target, array $query = [] ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Normalizes the provided target |
||
| 423 | * |
||
| 424 | * @todo move the code from Linker actually here |
||
| 425 | * @param LinkTarget $target |
||
| 426 | * @return LinkTarget |
||
| 427 | */ |
||
| 428 | private function normalizeTarget( LinkTarget $target ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Merges two sets of attributes |
||
| 434 | * |
||
| 435 | * @param array $defaults |
||
| 436 | * @param array $attribs |
||
| 437 | * |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | private function mergeAttribs( $defaults, $attribs ) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Return the CSS classes of a known link |
||
| 459 | * |
||
| 460 | * @param LinkTarget $target |
||
| 461 | * @return string CSS class |
||
| 462 | */ |
||
| 463 | public function getLinkClasses( LinkTarget $target ) { |
||
| 483 | } |
||
| 484 |