Complex classes like ModifyEntity 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 ModifyEntity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | abstract class ModifyEntity extends ApiBase { |
||
| 39 | |||
| 40 | use FederatedPropertyApiValidatorTrait; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var StringNormalizer |
||
| 44 | */ |
||
| 45 | protected $stringNormalizer; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var SiteLinkTargetProvider |
||
| 49 | */ |
||
| 50 | protected $siteLinkTargetProvider; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var EntityTitleStoreLookup |
||
| 54 | */ |
||
| 55 | private $titleLookup; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string[] |
||
| 59 | */ |
||
| 60 | protected $siteLinkGroups; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string[] |
||
| 64 | */ |
||
| 65 | protected $badgeItems; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var ApiErrorReporter |
||
| 69 | */ |
||
| 70 | protected $errorReporter; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var EntityPermissionChecker |
||
| 74 | */ |
||
| 75 | private $permissionChecker; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var EntityRevisionLookup |
||
| 79 | */ |
||
| 80 | private $revisionLookup; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var ResultBuilder |
||
| 84 | */ |
||
| 85 | private $resultBuilder; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var EntitySavingHelper |
||
| 89 | */ |
||
| 90 | private $entitySavingHelper; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string[] |
||
| 94 | */ |
||
| 95 | protected $enabledEntityTypes; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param ApiMain $mainModule |
||
| 99 | * @param string $moduleName |
||
| 100 | * @param bool $federatedPropertiesEnabled |
||
| 101 | * @param string $modulePrefix |
||
| 102 | * |
||
| 103 | * @see ApiBase::__construct |
||
| 104 | */ |
||
| 105 | public function __construct( ApiMain $mainModule, string $moduleName, bool $federatedPropertiesEnabled, string $modulePrefix = '' ) { |
||
| 135 | |||
| 136 | public function setServices( SiteLinkTargetProvider $siteLinkTargetProvider ): void { |
||
| 139 | |||
| 140 | protected function getTitleLookup(): EntityTitleStoreLookup { |
||
| 143 | |||
| 144 | protected function getResultBuilder(): ResultBuilder { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Create a new Summary instance suitable for representing the action performed by this module. |
||
| 150 | * |
||
| 151 | * @param array $params |
||
| 152 | * |
||
| 153 | * @return Summary |
||
| 154 | */ |
||
| 155 | protected function createSummary( array $params ): Summary { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Actually modify the entity. |
||
| 163 | * |
||
| 164 | * @param EntityDocument $entity |
||
| 165 | * @param ChangeOp $changeOp |
||
| 166 | * @param array $preparedParameters |
||
| 167 | * |
||
| 168 | * @return Summary|null a summary of the modification, or null to indicate failure. |
||
| 169 | */ |
||
| 170 | abstract protected function modifyEntity( |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Applies the given ChangeOp to the given Entity. |
||
| 178 | * Any ChangeOpException is converted into an ApiUsageException with the code 'modification-failed'. |
||
| 179 | * |
||
| 180 | * @param ChangeOp $changeOp |
||
| 181 | * @param EntityDocument $entity |
||
| 182 | * @param Summary|null $summary The summary object to update with information about the change. |
||
| 183 | * |
||
| 184 | * @return ChangeOpResult |
||
| 185 | */ |
||
| 186 | protected function applyChangeOp( ChangeOp $changeOp, EntityDocument $entity, Summary $summary = null ): ChangeOpResult { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param array $params |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | protected function prepareParameters( array $params ): array { |
||
| 241 | |||
| 242 | protected function validateEntitySpecificParameters( |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Make sure the required parameters are provided and that they are valid. |
||
| 251 | * |
||
| 252 | * @param array $params |
||
| 253 | */ |
||
| 254 | protected function validateParameters( array $params ): void { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @inheritDoc |
||
| 287 | */ |
||
| 288 | public function execute(): void { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param array $preparedParameters |
||
| 342 | * @param EntityDocument $entity |
||
| 343 | * |
||
| 344 | * @return ChangeOp |
||
| 345 | */ |
||
| 346 | abstract protected function getChangeOp( array $preparedParameters, EntityDocument $entity ): ChangeOp; |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Try to find the entity or fail and create it, or die in the process. |
||
| 350 | * |
||
| 351 | * @param EntityId|null $entityId |
||
| 352 | * |
||
| 353 | * @return EntityDocument |
||
| 354 | * @throws ApiUsageException |
||
| 355 | */ |
||
| 356 | private function loadEntityFromSavingHelper( ?EntityId $entityId ): EntityDocument { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Check the rights for the user accessing the module. |
||
| 378 | * |
||
| 379 | * @param EntityDocument $entity the entity to check |
||
| 380 | * @param User $user User doing the action |
||
| 381 | * @param ChangeOp $changeOp |
||
| 382 | * |
||
| 383 | * @return Status the check's result |
||
| 384 | */ |
||
| 385 | private function checkPermissions( EntityDocument $entity, User $user, ChangeOp $changeOp ): Status { |
||
| 395 | |||
| 396 | private function addToOutput( EntityDocument $entity, Status $status, int $oldRevId ): void { |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @inheritDoc |
||
| 414 | */ |
||
| 415 | protected function getAllowedParams(): array { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get allowed params for the identification of the entity |
||
| 426 | * Lookup through an id is common for all entities |
||
| 427 | * |
||
| 428 | * @return array[] |
||
| 429 | */ |
||
| 430 | private function getAllowedParamsForId(): array { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Get allowed params for the identification by a sitelink pair |
||
| 443 | * Lookup through the sitelink object is not used in every subclasses |
||
| 444 | * |
||
| 445 | * @return array[] |
||
| 446 | */ |
||
| 447 | private function getAllowedParamsForSiteLink(): array { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get allowed params for the entity in general |
||
| 462 | * |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | private function getAllowedParamsForEntity(): array { |
||
| 481 | |||
| 482 | } |
||
| 483 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: