Complex classes like SpecialSetSiteLink 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 SpecialSetSiteLink, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class SpecialSetSiteLink extends SpecialModifyEntity { |
||
32 | |||
33 | /** |
||
34 | * @var SiteLookup |
||
35 | */ |
||
36 | private $siteLookup; |
||
37 | |||
38 | /** |
||
39 | * @var SiteLinkTargetProvider |
||
40 | */ |
||
41 | private $siteLinkTargetProvider; |
||
42 | |||
43 | /** |
||
44 | * @var string[] |
||
45 | */ |
||
46 | private $siteLinkGroups; |
||
47 | |||
48 | /** |
||
49 | * @var string[] |
||
50 | */ |
||
51 | private $badgeItems; |
||
52 | |||
53 | /** |
||
54 | * @var LanguageFallbackLabelDescriptionLookupFactory |
||
55 | */ |
||
56 | private $labelDescriptionLookupFactory; |
||
57 | |||
58 | /** |
||
59 | * @var SiteLinkChangeOpFactory |
||
60 | */ |
||
61 | private $siteLinkChangeOpFactory; |
||
62 | |||
63 | /** |
||
64 | * The site of the site link. |
||
65 | * |
||
66 | * @var string|null |
||
67 | */ |
||
68 | private $site; |
||
69 | |||
70 | /** |
||
71 | * The page of the site link. |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | private $page; |
||
76 | |||
77 | /** |
||
78 | * The badges of the site link. |
||
79 | * |
||
80 | * @var string[] |
||
81 | */ |
||
82 | private $badges; |
||
83 | |||
84 | /** |
||
85 | * @param SpecialPageCopyrightView $copyrightView |
||
86 | * @param SummaryFormatter $summaryFormatter |
||
87 | * @param EntityTitleLookup $entityTitleLookup |
||
88 | * @param MediawikiEditEntityFactory $editEntityFactory |
||
89 | * @param SiteLookup $siteLookup |
||
90 | * @param SiteLinkTargetProvider $siteLinkTargetProvider |
||
91 | * @param string[] $siteLinkGroups |
||
92 | * @param string[] $badgeItems |
||
93 | * @param LanguageFallbackLabelDescriptionLookupFactory $labelDescriptionLookupFactory |
||
94 | * @param SiteLinkChangeOpFactory $siteLinkChangeOpFactory |
||
95 | */ |
||
96 | public function __construct( |
||
123 | |||
124 | public static function factory(): self { |
||
125 | $wikibaseRepo = WikibaseRepo::getDefaultInstance(); |
||
126 | $siteLookup = $wikibaseRepo->getSiteLookup(); |
||
127 | $settings = $wikibaseRepo->getSettings(); |
||
128 | |||
129 | $siteLinkChangeOpFactory = $wikibaseRepo->getChangeOpFactoryProvider()->getSiteLinkChangeOpFactory(); |
||
130 | $siteLinkTargetProvider = new SiteLinkTargetProvider( |
||
131 | $siteLookup, |
||
132 | $settings->getSetting( 'specialSiteLinkGroups' ) |
||
133 | ); |
||
134 | |||
135 | $copyrightView = new SpecialPageCopyrightView( |
||
136 | new CopyrightMessageBuilder(), |
||
137 | $settings->getSetting( 'dataRightsUrl' ), |
||
138 | $settings->getSetting( 'dataRightsText' ) |
||
139 | ); |
||
140 | |||
141 | $labelDescriptionLookupFactory = $wikibaseRepo->getLanguageFallbackLabelDescriptionLookupFactory(); |
||
142 | return new self( |
||
143 | $copyrightView, |
||
144 | $wikibaseRepo->getSummaryFormatter(), |
||
145 | $wikibaseRepo->getEntityTitleLookup(), |
||
146 | $wikibaseRepo->newEditEntityFactory(), |
||
147 | $siteLookup, |
||
148 | $siteLinkTargetProvider, |
||
149 | $settings->getSetting( 'siteLinkGroups' ), |
||
150 | $settings->getSetting( 'badgeItems' ), |
||
151 | $labelDescriptionLookupFactory, |
||
152 | $siteLinkChangeOpFactory |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | public function doesWrites() { |
||
159 | |||
160 | /** |
||
161 | * @see SpecialModifyEntity::processArguments() |
||
162 | * |
||
163 | * @param string|null $subPage |
||
164 | */ |
||
165 | protected function processArguments( $subPage ) { |
||
209 | |||
210 | /** |
||
211 | * @see SpecialModifyEntity::validateInput() |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | protected function validateInput() { |
||
230 | |||
231 | /** |
||
232 | * @see SpecialModifyEntity::modifyEntity() |
||
233 | * |
||
234 | * @param EntityDocument $entity |
||
235 | * |
||
236 | * @return Summary|bool The summary or false |
||
237 | */ |
||
238 | protected function modifyEntity( EntityDocument $entity ) { |
||
253 | |||
254 | /** |
||
255 | * Checks if the site id is valid. |
||
256 | * |
||
257 | * @param string $siteId the site id |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | private function isValidSiteId( $siteId ) { |
||
265 | |||
266 | /** |
||
267 | * @see SpecialModifyEntity::getForm() |
||
268 | * |
||
269 | * @param EntityDocument|null $entity |
||
270 | * |
||
271 | * @return HTMLForm |
||
272 | */ |
||
273 | protected function getForm( EntityDocument $entity = null ) { |
||
349 | |||
350 | /** |
||
351 | * Returns an array for generating a checkbox for each badge. |
||
352 | * |
||
353 | * @return array |
||
354 | */ |
||
355 | private function getMultiSelectForBadges() { |
||
392 | |||
393 | /** |
||
394 | * Returning the site page of the entity. |
||
395 | * |
||
396 | * @param Item|null $item |
||
397 | * @param string $siteId |
||
398 | * |
||
399 | * @throws OutOfBoundsException |
||
400 | * @return string |
||
401 | */ |
||
402 | private function getSiteLink( ?Item $item, $siteId ) { |
||
409 | |||
410 | /** |
||
411 | * Returning the badges of the entity. |
||
412 | * |
||
413 | * @param Item|null $item |
||
414 | * @param string $siteId |
||
415 | * |
||
416 | * @throws OutOfBoundsException |
||
417 | * @return string[] |
||
418 | */ |
||
419 | private function getBadges( ?Item $item, $siteId ) { |
||
431 | |||
432 | /** |
||
433 | * Validates badges from params and turns them into an array of ItemIds. |
||
434 | * |
||
435 | * @param string[] $badges |
||
436 | * @param Status $status |
||
437 | * |
||
438 | * @return ItemId[]|boolean |
||
439 | */ |
||
440 | private function parseBadges( array $badges, Status $status ) { |
||
468 | |||
469 | /** |
||
470 | * Setting the sitepage of the entity. |
||
471 | * |
||
472 | * @param EntityDocument $item |
||
473 | * @param string $siteId |
||
474 | * @param string $pageName |
||
475 | * @param string[] $badgeIds |
||
476 | * @param Summary|null &$summary The summary for this edit will be saved here. |
||
477 | * |
||
478 | * @throws InvalidArgumentException |
||
479 | * @return Status |
||
480 | */ |
||
481 | private function setSiteLink( EntityDocument $item, $siteId, $pageName, array $badgeIds, Summary &$summary = null ) { |
||
522 | |||
523 | } |
||
524 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..