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:
| 1 | <?php |
||
| 10 | class ObjectStateManager extends RepositoryExecutor implements MigrationGeneratorInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected $supportedStepTypes = array('object_state'); |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var ObjectStateMatcher |
||
| 19 | */ |
||
| 20 | protected $objectStateMatcher; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var ObjectStateGroupMatcher |
||
| 24 | */ |
||
| 25 | protected $objectStateGroupMatcher; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param ObjectStateMatcher $objectStateMatcher |
||
| 29 | * @param ObjectStateGroupMatcher $objectStateGroupMatcher |
||
| 30 | */ |
||
| 31 | public function __construct(ObjectStateMatcher $objectStateMatcher, ObjectStateGroupMatcher $objectStateGroupMatcher) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Handle the create step of object state migrations. |
||
| 39 | * |
||
| 40 | * @throws \Exception |
||
| 41 | */ |
||
| 42 | protected function create() |
||
| 43 | { |
||
| 44 | foreach (array('object_state_group', 'names', 'identifier') as $key) { |
||
| 45 | if (!isset($this->dsl[$key])) { |
||
| 46 | throw new \Exception("The '$key' key is missing in a object state creation definition"); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | if (!count($this->dsl['names'])) { |
||
| 51 | throw new \Exception('No object state names have been defined. Need to specify at least one to create the state.'); |
||
| 52 | } |
||
| 53 | |||
| 54 | $objectStateService = $this->repository->getObjectStateService(); |
||
| 55 | |||
| 56 | $objectStateGroupId = $this->dsl['object_state_group']; |
||
| 57 | $objectStateGroupId = $this->referenceResolver->resolveReference($objectStateGroupId); |
||
| 58 | $objectStateGroup = $this->objectStateGroupMatcher->matchOneByKey($objectStateGroupId); |
||
| 59 | |||
| 60 | $objectStateCreateStruct = $objectStateService->newObjectStateCreateStruct($this->dsl['identifier']); |
||
| 61 | $objectStateCreateStruct->defaultLanguageCode = self::DEFAULT_LANGUAGE_CODE; |
||
| 62 | |||
| 63 | foreach ($this->dsl['names'] as $languageCode => $name) { |
||
| 64 | $objectStateCreateStruct->names[$languageCode] = $name; |
||
| 65 | } |
||
| 66 | if (isset($this->dsl['descriptions'])) { |
||
| 67 | foreach ($this->dsl['descriptions'] as $languageCode => $description) { |
||
| 68 | $objectStateCreateStruct->descriptions[$languageCode] = $description; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | $objectState = $objectStateService->createObjectState($objectStateGroup, $objectStateCreateStruct); |
||
| 73 | |||
| 74 | $this->setReferences($objectState); |
||
| 75 | |||
| 76 | return $objectState; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Handle the update step of object state migrations. |
||
| 81 | * |
||
| 82 | * @throws \Exception |
||
| 83 | */ |
||
| 84 | View Code Duplication | protected function update() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Handle the deletion step of object state migrations. |
||
| 124 | */ |
||
| 125 | protected function delete() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string $action |
||
| 140 | * @return ObjectStateCollection |
||
| 141 | * @throws \Exception |
||
| 142 | */ |
||
| 143 | View Code Duplication | private function matchObjectStates($action) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $objectState |
||
| 168 | */ |
||
| 169 | View Code Duplication | protected function setReferences($objectState) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param array $matchCondition |
||
| 193 | * @param string $mode |
||
| 194 | * @throws \Exception |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public function generateMigration(array $matchCondition, $mode) |
||
| 272 | } |
||
| 273 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.