Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ObjectStateService 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 ObjectStateService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class ObjectStateService implements ObjectStateServiceInterface |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var \eZ\Publish\API\Repository\Repository |
||
| 44 | */ |
||
| 45 | protected $repository; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler |
||
| 49 | */ |
||
| 50 | protected $objectStateHandler; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $settings; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 59 | * |
||
| 60 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 61 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler $objectStateHandler |
||
| 62 | * @param array $settings |
||
| 63 | */ |
||
| 64 | public function __construct(RepositoryInterface $repository, Handler $objectStateHandler, array $settings = array()) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Creates a new object state group. |
||
| 76 | * |
||
| 77 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state group |
||
| 78 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists |
||
| 79 | * |
||
| 80 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct $objectStateGroupCreateStruct |
||
| 81 | * |
||
| 82 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
| 83 | */ |
||
| 84 | public function createObjectStateGroup(ObjectStateGroupCreateStruct $objectStateGroupCreateStruct) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Loads a object state group. |
||
| 121 | * |
||
| 122 | * @param mixed $objectStateGroupId |
||
| 123 | * |
||
| 124 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the group was not found |
||
| 125 | * |
||
| 126 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
| 127 | */ |
||
| 128 | public function loadObjectStateGroup($objectStateGroupId) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Loads all object state groups. |
||
| 137 | * |
||
| 138 | * @param int $offset |
||
| 139 | * @param int $limit |
||
| 140 | * |
||
| 141 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup[] |
||
| 142 | */ |
||
| 143 | public function loadObjectStateGroups($offset = 0, $limit = -1) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * This method returns the ordered list of object states of a group. |
||
| 157 | * |
||
| 158 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 159 | * |
||
| 160 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState[] |
||
| 161 | */ |
||
| 162 | public function loadObjectStates(APIObjectStateGroup $objectStateGroup) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Updates an object state group. |
||
| 176 | * |
||
| 177 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state group |
||
| 178 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists |
||
| 179 | * |
||
| 180 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 181 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct |
||
| 182 | * |
||
| 183 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function updateObjectStateGroup(APIObjectStateGroup $objectStateGroup, ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Deletes a object state group including all states and links to content. |
||
| 232 | * |
||
| 233 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state group |
||
| 234 | * |
||
| 235 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 236 | */ |
||
| 237 | public function deleteObjectStateGroup(APIObjectStateGroup $objectStateGroup) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Creates a new object state in the given group. |
||
| 257 | * |
||
| 258 | * Note: in current kernel: If it is the first state all content objects will |
||
| 259 | * set to this state. |
||
| 260 | * |
||
| 261 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create an object state |
||
| 262 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group |
||
| 263 | * |
||
| 264 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 265 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct $objectStateCreateStruct |
||
| 266 | * |
||
| 267 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
| 268 | */ |
||
| 269 | public function createObjectState(APIObjectStateGroup $objectStateGroup, ObjectStateCreateStruct $objectStateCreateStruct) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Loads an object state. |
||
| 318 | * |
||
| 319 | * @param mixed $stateId |
||
| 320 | * |
||
| 321 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the state was not found |
||
| 322 | * |
||
| 323 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
| 324 | */ |
||
| 325 | public function loadObjectState($stateId) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Updates an object state. |
||
| 334 | * |
||
| 335 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state |
||
| 336 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state with provided identifier already exists in the same group |
||
| 337 | * |
||
| 338 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 339 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct $objectStateUpdateStruct |
||
| 340 | * |
||
| 341 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
| 342 | */ |
||
| 343 | View Code Duplication | public function updateObjectState(APIObjectState $objectState, ObjectStateUpdateStruct $objectStateUpdateStruct) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Changes the priority of the state. |
||
| 394 | * |
||
| 395 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change priority on an object state |
||
| 396 | * |
||
| 397 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 398 | * @param int $priority |
||
| 399 | */ |
||
| 400 | public function setPriorityOfObjectState(APIObjectState $objectState, $priority) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Deletes a object state. The state of the content objects is reset to the |
||
| 427 | * first object state in the group. |
||
| 428 | * |
||
| 429 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete an object state |
||
| 430 | * |
||
| 431 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 432 | */ |
||
| 433 | public function deleteObjectState(APIObjectState $objectState) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Sets the object-state of a state group to $state for the given content. |
||
| 453 | * |
||
| 454 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state does not belong to the given group |
||
| 455 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to change the object state |
||
| 456 | * |
||
| 457 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 458 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 459 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 460 | */ |
||
| 461 | public function setContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup, APIObjectState $objectState) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Gets the object-state of object identified by $contentId. |
||
| 489 | * |
||
| 490 | * The $state is the id of the state within one group. |
||
| 491 | * |
||
| 492 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 493 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 494 | * |
||
| 495 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
| 496 | */ |
||
| 497 | public function getContentState(ContentInfo $contentInfo, APIObjectStateGroup $objectStateGroup) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Returns the number of objects which are in this state. |
||
| 509 | * |
||
| 510 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 511 | * |
||
| 512 | * @return int |
||
| 513 | */ |
||
| 514 | public function getContentCount(APIObjectState $objectState) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Instantiates a new Object State Group Create Struct and sets $identified in it. |
||
| 523 | * |
||
| 524 | * @param string $identifier |
||
| 525 | * |
||
| 526 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupCreateStruct |
||
| 527 | */ |
||
| 528 | public function newObjectStateGroupCreateStruct($identifier) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Instantiates a new Object State Group Update Struct. |
||
| 538 | * |
||
| 539 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct |
||
| 540 | */ |
||
| 541 | public function newObjectStateGroupUpdateStruct() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Instantiates a new Object State Create Struct and sets $identifier in it. |
||
| 548 | * |
||
| 549 | * @param string $identifier |
||
| 550 | * |
||
| 551 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateCreateStruct |
||
| 552 | */ |
||
| 553 | public function newObjectStateCreateStruct($identifier) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Instantiates a new Object State Update Struct. |
||
| 563 | * |
||
| 564 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateUpdateStruct |
||
| 565 | */ |
||
| 566 | public function newObjectStateUpdateStruct() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Converts the object state SPI value object to API value object. |
||
| 573 | * |
||
| 574 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState $spiObjectState |
||
| 575 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 576 | * |
||
| 577 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectState |
||
| 578 | */ |
||
| 579 | protected function buildDomainObjectStateObject(SPIObjectState $spiObjectState, APIObjectStateGroup $objectStateGroup = null) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Converts the object state group SPI value object to API value object. |
||
| 599 | * |
||
| 600 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState\Group $spiObjectStateGroup |
||
| 601 | * |
||
| 602 | * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup |
||
| 603 | */ |
||
| 604 | protected function buildDomainObjectStateGroupObject(SPIObjectStateGroup $spiObjectStateGroup) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Validates input for creating object states/groups and builds the InputStruct object. |
||
| 620 | * |
||
| 621 | * @param string $identifier |
||
| 622 | * @param string $defaultLanguageCode |
||
| 623 | * @param string[] $names |
||
| 624 | * @param string[] $descriptions |
||
| 625 | * |
||
| 626 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
| 627 | */ |
||
| 628 | protected function buildCreateInputStruct($identifier, $defaultLanguageCode, $names, $descriptions) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Validates input for updating object states and builds the InputStruct object. |
||
| 683 | * |
||
| 684 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 685 | * @param string $identifier |
||
| 686 | * @param string $defaultLanguageCode |
||
| 687 | * @param string[] $names |
||
| 688 | * @param string[] $descriptions |
||
| 689 | * |
||
| 690 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
| 691 | */ |
||
| 692 | View Code Duplication | protected function buildObjectStateUpdateInputStruct(APIObjectState $objectState, $identifier, $defaultLanguageCode, $names, $descriptions) |
|
| 748 | |||
| 749 | /** |
||
| 750 | * Validates input for updating object state groups and builds the InputStruct object. |
||
| 751 | * |
||
| 752 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup |
||
| 753 | * @param string $identifier |
||
| 754 | * @param string $defaultLanguageCode |
||
| 755 | * @param string[] $names |
||
| 756 | * @param string[] $descriptions |
||
| 757 | * |
||
| 758 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct |
||
| 759 | */ |
||
| 760 | View Code Duplication | protected function buildObjectStateGroupUpdateInputStruct(APIObjectStateGroup $objectStateGroup, $identifier, $defaultLanguageCode, $names, $descriptions) |
|
| 816 | } |
||
| 817 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.