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 |
||
| 39 | class LinkRenderer { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Whether to force the pretty article path |
||
| 43 | * |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | private $forceArticlePath = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * A PROTO_* constant or false |
||
| 50 | * |
||
| 51 | * @var string|bool|int |
||
| 52 | */ |
||
| 53 | private $expandUrls = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | private $stubThreshold = 0; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var TitleFormatter |
||
| 62 | */ |
||
| 63 | private $titleFormatter; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Whether to run the legacy Linker hooks |
||
| 67 | * |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | private $runLegacyBeginHook = true; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param TitleFormatter $titleFormatter |
||
| 74 | */ |
||
| 75 | public function __construct( TitleFormatter $titleFormatter ) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param bool $force |
||
| 81 | */ |
||
| 82 | public function setForceArticlePath( $force ) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | public function getForceArticlePath() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string|bool|int $expand A PROTO_* constant or false |
||
| 95 | */ |
||
| 96 | public function setExpandURLs( $expand ) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return string|bool|int a PROTO_* constant or false |
||
| 102 | */ |
||
| 103 | public function getExpandURLs() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param int $threshold |
||
| 109 | */ |
||
| 110 | public function setStubThreshold( $threshold ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | public function getStubThreshold() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param bool $run |
||
| 123 | */ |
||
| 124 | public function setRunLegacyBeginHook( $run ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param LinkTarget $target |
||
| 130 | * @param string|HtmlArmor|null $text |
||
| 131 | * @param array $extraAttribs |
||
| 132 | * @param array $query |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function makeLink( |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the options in the legacy format |
||
| 148 | * |
||
| 149 | * @param bool $isKnown Whether the link is known or broken |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | private function getLegacyOptions( $isKnown ) { |
||
| 167 | |||
| 168 | private function runBeginHook( LinkTarget $target, &$text, &$extraAttribs, &$query, $isKnown ) { |
||
| 179 | |||
| 180 | private function runLegacyBeginHook( LinkTarget $target, &$text, &$extraAttribs, &$query, |
||
| 226 | |||
| 227 | /** |
||
| 228 | * If you have already looked up the proper CSS classes using Linker::getLinkColour() |
||
| 229 | * or some other method, use this to avoid looking it up again. |
||
| 230 | * |
||
| 231 | * @param LinkTarget $target |
||
| 232 | * @param string|HtmlArmor|null $text |
||
| 233 | * @param string $classes CSS classes to add |
||
| 234 | * @param array $extraAttribs |
||
| 235 | * @param array $query |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function makePreloadedLink( |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param LinkTarget $target |
||
| 267 | * @param string|HtmlArmor|null $text |
||
| 268 | * @param array $extraAttribs |
||
| 269 | * @param array $query |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function makeKnownLink( |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param LinkTarget $target |
||
| 295 | * @param string|HtmlArmor|null $text |
||
| 296 | * @param array $extraAttribs |
||
| 297 | * @param array $query |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function makeBrokenLink( |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Builds the final <a> element |
||
| 344 | * |
||
| 345 | * @param LinkTarget $target |
||
| 346 | * @param string|HtmlArmor $text |
||
| 347 | * @param array $attribs |
||
| 348 | * @param bool $isKnown |
||
| 349 | * @return null|string |
||
| 350 | */ |
||
| 351 | private function buildAElement( LinkTarget $target, $text, array $attribs, $isKnown ) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param LinkTarget $target |
||
| 378 | * @return string non-escaped text |
||
| 379 | */ |
||
| 380 | private function getLinkText( LinkTarget $target ) { |
||
| 390 | |||
| 391 | private function getLinkURL( LinkTarget $target, array $query = [] ) { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Normalizes the provided target |
||
| 414 | * |
||
| 415 | * @todo move the code from Linker actually here |
||
| 416 | * @param LinkTarget $target |
||
| 417 | * @return LinkTarget |
||
| 418 | */ |
||
| 419 | private function normalizeTarget( LinkTarget $target ) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Merges two sets of attributes |
||
| 425 | * |
||
| 426 | * @param array $defaults |
||
| 427 | * @param array $attribs |
||
| 428 | * |
||
| 429 | * @return array |
||
| 430 | */ |
||
| 431 | private function mergeAttribs( $defaults, $attribs ) { |
||
| 447 | |||
| 448 | } |
||
| 449 |