Complex classes like ExtraPropertyAnnotator 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 ExtraPropertyAnnotator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ExtraPropertyAnnotator { |
||
33 | |||
34 | /** |
||
35 | * @var SemanticData |
||
36 | */ |
||
37 | protected $semanticData = null; |
||
38 | |||
39 | /** |
||
40 | * @var AppFactory |
||
41 | */ |
||
42 | private $appFactory = null; |
||
43 | |||
44 | |||
45 | protected $configuration = null; |
||
46 | private $dbConnection = null; |
||
47 | private $page = null; |
||
48 | |||
49 | /** |
||
50 | * @since 1.0 |
||
51 | * |
||
52 | * @param SemanticData $semanticData |
||
53 | * @param Factory $factory |
||
|
|||
54 | * @param array $configuration |
||
55 | */ |
||
56 | 9 | public function __construct( SemanticData $semanticData, AppFactory $appFactory, array $configuration ) { |
|
61 | |||
62 | /** |
||
63 | * @since 1.0 |
||
64 | * |
||
65 | * @return boolean |
||
66 | * @throws RuntimeException |
||
67 | * |
||
68 | * @return boolean |
||
69 | */ |
||
70 | 8 | public function addAnnotation() { |
|
85 | |||
86 | /** |
||
87 | * @since 1.0 |
||
88 | * |
||
89 | * @return SemanticData |
||
90 | */ |
||
91 | 6 | public function getSemanticData() { |
|
94 | |||
95 | /** |
||
96 | * @since 1.0 |
||
97 | * |
||
98 | * @return WikiPage |
||
99 | */ |
||
100 | 6 | public function getWikiPage() { |
|
108 | |||
109 | 6 | protected function addPropertyValues() { |
|
139 | |||
140 | 6 | protected function hasRegisteredPropertyId( $propertyId, $cachedProperties ) { |
|
144 | |||
145 | 6 | protected function createDataItemById( $externalId, $property ) { |
|
146 | |||
147 | 6 | $dataItem = null; |
|
148 | |||
149 | // _REVID was incorrect in the original SESP because getId returns the |
||
150 | // page id not the revision Id |
||
151 | |||
152 | switch ( $externalId ) { |
||
153 | 6 | case '_CUSER' : |
|
154 | 1 | $dataItem = $this->makeFirstAuthorDataItem(); |
|
155 | 1 | break; |
|
156 | 5 | case '_VIEWS' : |
|
157 | $dataItem = $this->makeNumberOfPageViewsDataItem(); |
||
158 | break; |
||
159 | 5 | case '_USERREG' : |
|
160 | 2 | $dataItem = $this->makeUserRegistrationDataItem(); |
|
161 | 2 | break; |
|
162 | 3 | case '_USEREDITCNT' : |
|
163 | $dataItem = $this->makeUserEditCountDataItem(); |
||
164 | break; |
||
165 | 3 | case '_PAGEID' : |
|
166 | $dataItem = $this->makePageIdDataItem(); |
||
167 | break; |
||
168 | 3 | case '_PAGELGTH' : |
|
169 | 2 | $dataItem = $this->makePageLengthDataItem(); |
|
170 | 2 | break; |
|
171 | 1 | case '_REVID' : |
|
172 | 1 | $dataItem = $this->makeRevisionIdDataItem(); |
|
173 | 1 | break; |
|
174 | case '_NREV' : |
||
175 | $dataItem = $this->makeNumberOfRevisionsDataItem(); |
||
176 | break; |
||
177 | case '_NTREV' : |
||
178 | $dataItem = $this->makeNumberOfTalkPageRevisionsDataItem(); |
||
179 | break; |
||
180 | case '_EUSER' : |
||
181 | $this->addPropertyValuesForPageContributors( $property ); |
||
182 | break; |
||
183 | case '_SUBP' : |
||
184 | $this->addPropertyValuesForSubPages( $property ); |
||
185 | break; |
||
186 | case '_MEDIATYPE' : |
||
187 | case '_MIMETYPE' : |
||
188 | $this->addPropertyValuesForMIMEAndMediaType(); |
||
189 | break; |
||
190 | case '_EXIFDATA' : |
||
191 | $this->addPropertyValuesForExifData(); |
||
192 | break; |
||
193 | case '_SHORTURL' : |
||
194 | $this->addPropertyValuesForShortUrl(); |
||
195 | 6 | break; |
|
196 | } |
||
197 | |||
198 | 2 | return $dataItem; |
|
199 | 2 | } |
|
200 | |||
201 | private function isUserPage() { |
||
202 | return $this->getWikiPage()->getTitle()->inNamespace( NS_USER ); |
||
203 | } |
||
204 | |||
205 | private function isFilePage() { |
||
206 | 1 | return $this->getWikiPage()->getTitle()->inNamespace( NS_FILE ); |
|
207 | 1 | } |
|
208 | |||
209 | 1 | private function makeFirstAuthorDataItem() { |
|
210 | 1 | $creator = $this->getWikiPage()->getCreator(); |
|
211 | |||
212 | if ( $creator ) { |
||
213 | return DIWikiPage::newFromTitle( $creator->getUserPage() ); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | private function makeNumberOfPageViewsDataItem() { |
||
218 | if ( $this->configuration['wgDisableCounters'] ) { |
||
219 | return null; |
||
220 | } |
||
221 | |||
222 | $count = $this->getPageViewCount(); |
||
223 | |||
224 | if ( !is_numeric( $count ) ) { |
||
225 | return null; |
||
226 | } |
||
227 | |||
228 | return new DINumber( $count ); |
||
229 | } |
||
230 | |||
231 | private function getPageViewCount() { |
||
232 | if ( class_exists( '\HitCounters\HitCounters' ) ) { |
||
233 | return \HitCounters\HitCounters::getCount( $this->getWikiPage()->getTitle() ); |
||
234 | } |
||
235 | |||
236 | if ( method_exists( $this->getWikiPage(), 'getCount' ) ) { |
||
237 | return $this->getWikiPage()->getCount(); |
||
238 | } |
||
239 | |||
240 | return null; |
||
241 | } |
||
242 | |||
243 | private function addPropertyValuesForPageContributors( DIProperty $property ) { |
||
263 | |||
264 | private function makePageIdDataItem() { |
||
265 | $pageID = $this->getWikiPage()->getId(); |
||
266 | |||
267 | if ( is_integer( $pageID ) && $pageID > 0 ) { |
||
268 | return new DINumber( $pageID ); |
||
269 | 2 | } |
|
271 | |||
272 | 2 | private function makePageLengthItem() { |
|
279 | 1 | ||
280 | 1 | private function makeRevisionIdDataItem() { |
|
287 | 1 | ||
288 | private function getPageRevisionsForId( $pageId ) { |
||
300 | |||
301 | private function makeNumberOfRevisionsDataItem() { |
||
310 | |||
311 | private function makeNumberOfTalkPageRevisionsDataItem() { |
||
320 | |||
321 | private function addPropertyValuesForMIMEAndMediaType(){ |
||
342 | |||
343 | private function addPropertyValuesForSubPages( DIProperty $property ) { |
||
355 | |||
356 | private function addPropertyValuesForExifData() { |
||
361 | |||
362 | 2 | private function addPropertyValuesForShortUrl() { |
|
370 | 1 | ||
371 | 1 | private function makeUserRegistrationDataItem() { |
|
394 | |||
395 | private function makeUserEditCountDataItem() { |
||
409 | |||
410 | } |
||
411 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.