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 ) { |
|
| 57 | 9 | $this->semanticData = $semanticData; |
|
| 58 | 9 | $this->appFactory = $appFactory; |
|
| 59 | 9 | $this->configuration = $configuration; |
|
| 60 | 9 | } |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @since 1.0 |
||
| 64 | * |
||
| 65 | * @return boolean |
||
| 66 | * @throws RuntimeException |
||
| 67 | * |
||
| 68 | * @return boolean |
||
| 69 | */ |
||
| 70 | 8 | public function addAnnotation() { |
|
| 71 | |||
| 72 | 8 | $subject = $this->semanticData->getSubject(); |
|
| 73 | |||
| 74 | 8 | if ( $subject === null || $subject->getTitle() === null || $subject->getTitle()->isSpecialPage() ) { |
|
| 75 | 1 | return false; |
|
| 76 | } |
||
| 77 | |||
| 78 | 7 | if ( isset( $this->configuration['sespSpecialProperties'] ) && |
|
| 79 | 7 | is_array( $this->configuration['sespSpecialProperties'] ) ) { |
|
| 80 | 6 | return $this->addPropertyValues(); |
|
| 81 | } |
||
| 82 | |||
| 83 | 1 | throw new RuntimeException( "Expected a 'sespSpecialProperties' configuration array" ); |
|
| 84 | } |
||
| 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() { |
|
| 101 | |||
| 102 | 6 | if ( $this->page === null ) { |
|
| 103 | 6 | $this->page = $this->appFactory->newWikiPage( $this->semanticData->getSubject()->getTitle() ); //$this->loadRegisteredObject( 'WikiPage' ); |
|
| 104 | 6 | } |
|
| 105 | |||
| 106 | 6 | return $this->page; |
|
| 107 | } |
||
| 108 | |||
| 109 | 6 | protected function addPropertyValues() { |
|
| 110 | |||
| 111 | 6 | $cachedProperties = array(); |
|
| 112 | |||
| 113 | 6 | foreach ( $this->configuration['sespSpecialProperties'] as $externalId ) { |
|
| 114 | |||
| 115 | 6 | $propertyId = PropertyRegistry::getInstance()->getPropertyId( $externalId ); |
|
| 116 | |||
| 117 | 6 | if ( $this->hasRegisteredPropertyId( $propertyId, $cachedProperties ) ) { |
|
| 118 | continue; |
||
| 119 | } |
||
| 120 | |||
| 121 | 6 | $propertyDI = new DIProperty( $propertyId ); |
|
| 122 | |||
| 123 | 6 | if ( $this->getSemanticData()->getPropertyValues( $propertyDI ) !== array() ) { |
|
| 124 | $cachedProperties[ $propertyId ] = true; |
||
| 125 | continue; |
||
| 126 | } |
||
| 127 | |||
| 128 | 6 | $dataItem = $this->createDataItemById( $externalId, $propertyDI ); |
|
| 129 | |||
| 130 | 6 | if ( $dataItem instanceof DataItem ) { |
|
| 131 | 4 | $cachedProperties[ $propertyId ] = true; |
|
| 132 | 4 | $this->getSemanticData()->addPropertyObjectValue( $propertyDI, $dataItem ); |
|
| 133 | 4 | } |
|
| 134 | |||
| 135 | 6 | } |
|
| 136 | |||
| 137 | 6 | return true; |
|
| 138 | } |
||
| 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 '_REVID' : |
|
| 169 | 2 | $dataItem = $this->makeRevisionIdDataItem(); |
|
| 170 | 2 | break; |
|
| 171 | 1 | case '_NREV' : |
|
| 172 | 1 | $dataItem = $this->makeNumberOfRevisionsDataItem(); |
|
| 173 | 1 | break; |
|
| 174 | case '_NTREV' : |
||
| 175 | $dataItem = $this->makeNumberOfTalkPageRevisionsDataItem(); |
||
| 176 | break; |
||
| 177 | case '_EUSER' : |
||
| 178 | $this->addPropertyValuesForPageContributors( $property ); |
||
| 179 | break; |
||
| 180 | case '_SUBP' : |
||
| 181 | $this->addPropertyValuesForSubPages( $property ); |
||
| 182 | break; |
||
| 183 | case '_MEDIATYPE' : |
||
| 184 | case '_MIMETYPE' : |
||
| 185 | $this->addPropertyValuesForMIMEAndMediaType(); |
||
| 186 | break; |
||
| 187 | case '_EXIFDATA' : |
||
| 188 | $this->addPropertyValuesForExifData(); |
||
| 189 | break; |
||
| 190 | case '_SHORTURL' : |
||
| 191 | $this->addPropertyValuesForShortUrl(); |
||
| 192 | break; |
||
| 193 | } |
||
| 194 | |||
| 195 | 6 | return $dataItem; |
|
| 196 | } |
||
| 197 | |||
| 198 | 2 | private function isUserPage() { |
|
| 201 | |||
| 202 | private function isFilePage() { |
||
| 205 | |||
| 206 | 1 | private function makeFirstAuthorDataItem() { |
|
| 207 | 1 | $creator = $this->getWikiPage()->getCreator(); |
|
| 208 | |||
| 209 | 1 | if ( $creator ) { |
|
| 210 | 1 | return DIWikiPage::newFromTitle( $creator->getUserPage() ); |
|
| 213 | |||
| 214 | private function makeNumberOfPageViewsDataItem() { |
||
| 227 | |||
| 228 | private function getPageViewCount() { |
||
| 239 | |||
| 240 | private function addPropertyValuesForPageContributors( DIProperty $property ) { |
||
| 260 | |||
| 261 | private function makePageIdDataItem() { |
||
| 262 | $pageID = $this->getWikiPage()->getId(); |
||
| 263 | |||
| 264 | if ( is_integer( $pageID ) && $pageID > 0 ) { |
||
| 265 | return new DINumber( $pageID ); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | 2 | private function makeRevisionIdDataItem() { |
|
| 270 | 2 | $revID = $this->getWikiPage()->getLatest(); |
|
| 271 | |||
| 272 | 2 | if ( is_integer( $revID ) && $revID > 0 ) { |
|
| 273 | 1 | return new DINumber( $revID ); |
|
| 274 | } |
||
| 275 | 1 | } |
|
| 276 | |||
| 277 | 1 | private function getPageRevisionsForId( $pageId ) { |
|
| 289 | |||
| 290 | 1 | private function makeNumberOfRevisionsDataItem() { |
|
| 291 | 1 | $numberOfPageRevisions = $this->getPageRevisionsForId( |
|
| 299 | |||
| 300 | private function makeNumberOfTalkPageRevisionsDataItem() { |
||
| 309 | |||
| 310 | private function addPropertyValuesForMIMEAndMediaType(){ |
||
| 331 | |||
| 332 | private function addPropertyValuesForSubPages( DIProperty $property ) { |
||
| 344 | |||
| 345 | private function addPropertyValuesForExifData() { |
||
| 350 | |||
| 351 | private function addPropertyValuesForShortUrl() { |
||
| 359 | |||
| 360 | 2 | private function makeUserRegistrationDataItem() { |
|
| 383 | |||
| 384 | private function makeUserEditCountDataItem() { |
||
| 398 | |||
| 399 | } |
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.