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 | 10 | 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 | 9 | public function addAnnotation() { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @since 1.0 |
||
| 88 | * |
||
| 89 | * @return SemanticData |
||
| 90 | */ |
||
| 91 | 7 | public function getSemanticData() { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @since 1.0 |
||
| 97 | * |
||
| 98 | * @return WikiPage |
||
| 99 | */ |
||
| 100 | 7 | public function getWikiPage() { |
|
| 108 | |||
| 109 | 7 | protected function addPropertyValues() { |
|
| 139 | |||
| 140 | 7 | protected function hasRegisteredPropertyId( $propertyId, $cachedProperties ) { |
|
| 144 | |||
| 145 | 7 | protected function createDataItemById( $externalId, $property ) { |
|
| 146 | |||
| 147 | 7 | $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 | 7 | case '_CUSER' : |
|
| 154 | 1 | $dataItem = $this->makeFirstAuthorDataItem(); |
|
| 155 | 1 | break; |
|
| 156 | 6 | case '_VIEWS' : |
|
| 157 | $dataItem = $this->makeNumberOfPageViewsDataItem(); |
||
| 158 | break; |
||
| 159 | 6 | case '_USERREG' : |
|
| 160 | 2 | $dataItem = $this->makeUserRegistrationDataItem(); |
|
| 161 | 2 | break; |
|
| 162 | 4 | case '_USEREDITCNT' : |
|
| 163 | $dataItem = $this->makeUserEditCountDataItem(); |
||
| 164 | break; |
||
| 165 | 4 | case '_PAGEID' : |
|
| 166 | $dataItem = $this->makePageIdDataItem(); |
||
| 167 | break; |
||
| 168 | 4 | case '_PAGELGTH' : |
|
| 169 | 1 | $dataItem = $this->makePageLengthDataItem(); |
|
| 170 | 1 | break; |
|
| 171 | 3 | case '_REVID' : |
|
| 172 | 2 | $dataItem = $this->makeRevisionIdDataItem(); |
|
| 173 | 2 | break; |
|
| 174 | 1 | case '_NREV' : |
|
| 175 | 1 | $dataItem = $this->makeNumberOfRevisionsDataItem(); |
|
| 176 | 1 | 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 | break; |
||
| 196 | } |
||
| 197 | |||
| 198 | 7 | return $dataItem; |
|
| 199 | } |
||
| 200 | |||
| 201 | 2 | private function isUserPage() { |
|
| 204 | |||
| 205 | private function isFilePage() { |
||
| 208 | |||
| 209 | 1 | private function makeFirstAuthorDataItem() { |
|
| 216 | |||
| 217 | private function makeNumberOfPageViewsDataItem() { |
||
| 230 | |||
| 231 | private function getPageViewCount() { |
||
| 242 | |||
| 243 | private function addPropertyValuesForPageContributors( DIProperty $property ) { |
||
| 263 | |||
| 264 | private function makePageIdDataItem() { |
||
| 271 | |||
| 272 | 1 | private function makePageLengthDataItem() { |
|
| 273 | 1 | $pageLen = $this->getWikiPage()->getTitle()->getLength(); |
|
| 274 | |||
| 275 | 1 | if ( is_integer( $pageLen ) && $pageLen > 0 ) { |
|
| 276 | 1 | return new DINumber( $pageLen ); |
|
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | 2 | private function makeRevisionIdDataItem() { |
|
| 287 | |||
| 288 | 1 | private function getPageRevisionsForId( $pageId ) { |
|
| 300 | |||
| 301 | 1 | 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 | private function addPropertyValuesForShortUrl() { |
||
| 370 | |||
| 371 | 2 | 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
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.