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 |
||
| 31 | class SectionService extends SectionServiceDecorator implements SectionServiceInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
||
| 35 | */ |
||
| 36 | protected $eventDispatcher; |
||
| 37 | |||
| 38 | public function __construct( |
||
| 46 | |||
| 47 | public function createSection(SectionCreateStruct $sectionCreateStruct) |
||
| 48 | { |
||
| 49 | $eventData = [$sectionCreateStruct]; |
||
| 50 | |||
| 51 | $beforeEvent = new BeforeCreateSectionEvent(...$eventData); |
||
| 52 | if ($this->eventDispatcher->dispatch(SectionEvents::BEFORE_CREATE_SECTION, $beforeEvent)->isPropagationStopped()) { |
||
| 53 | return $beforeEvent->getSection(); |
||
| 54 | } |
||
| 55 | |||
| 56 | $section = $beforeEvent->hasSection() |
||
| 57 | ? $beforeEvent->getSection() |
||
| 58 | : parent::createSection($sectionCreateStruct); |
||
| 59 | |||
| 60 | $this->eventDispatcher->dispatch( |
||
| 61 | SectionEvents::CREATE_SECTION, |
||
| 62 | new CreateSectionEvent($section, ...$eventData) |
||
| 63 | ); |
||
| 64 | |||
| 65 | return $section; |
||
| 66 | } |
||
| 67 | |||
| 68 | public function updateSection( |
||
| 69 | Section $section, |
||
| 70 | SectionUpdateStruct $sectionUpdateStruct |
||
| 71 | ) { |
||
| 72 | $eventData = [ |
||
| 73 | $section, |
||
| 74 | $sectionUpdateStruct, |
||
| 75 | ]; |
||
| 76 | |||
| 77 | $beforeEvent = new BeforeUpdateSectionEvent(...$eventData); |
||
|
|
|||
| 78 | if ($this->eventDispatcher->dispatch(SectionEvents::BEFORE_UPDATE_SECTION, $beforeEvent)->isPropagationStopped()) { |
||
| 79 | return $beforeEvent->getUpdatedSection(); |
||
| 80 | } |
||
| 81 | |||
| 82 | $updatedSection = $beforeEvent->hasUpdatedSection() |
||
| 83 | ? $beforeEvent->getUpdatedSection() |
||
| 84 | : parent::updateSection($section, $sectionUpdateStruct); |
||
| 85 | |||
| 86 | $this->eventDispatcher->dispatch( |
||
| 87 | SectionEvents::UPDATE_SECTION, |
||
| 88 | new UpdateSectionEvent($updatedSection, ...$eventData) |
||
| 89 | ); |
||
| 90 | |||
| 91 | return $updatedSection; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function assignSection( |
||
| 115 | |||
| 116 | View Code Duplication | public function assignSectionToSubtree( |
|
| 137 | |||
| 138 | public function deleteSection(Section $section) |
||
| 154 | } |
||
| 155 |
This check looks for function calls that miss required arguments.