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 |
||
25 | abstract class AbstractStepAction implements StepActionInterface |
||
26 | { |
||
27 | /** |
||
28 | * Used when you need to change the translation domain used in controller-generated messages. |
||
29 | */ |
||
30 | protected static $translationDomain = 'PierstovalCharacterBundle'; |
||
31 | |||
32 | /** @var string */ |
||
33 | protected $class; |
||
34 | |||
35 | /** @var Request */ |
||
36 | protected $request; |
||
37 | |||
38 | /** @var StepInterface */ |
||
39 | protected $step; |
||
40 | |||
41 | /** @var StepInterface[] */ |
||
42 | protected $steps = []; |
||
43 | |||
44 | /** @var string */ |
||
45 | protected $stepName; |
||
46 | |||
47 | /** @var string */ |
||
48 | protected $managerName; |
||
49 | |||
50 | /** @var RouterInterface */ |
||
51 | protected $router; |
||
52 | |||
53 | /** @var ObjectManager */ |
||
54 | protected $em; |
||
55 | |||
56 | /** @var Environment */ |
||
57 | protected $twig; |
||
58 | |||
59 | /** @var TranslatorInterface */ |
||
60 | protected $translator; |
||
61 | |||
62 | public function configure(string $managerName, string $stepName, string $characterClassName, StepResolverInterface $resolver): void |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function setRequest(Request $request): void |
||
84 | |||
85 | public function setRouter(RouterInterface $router): void |
||
89 | |||
90 | public function setObjectManager(ObjectManager $em): void |
||
94 | |||
95 | public function setTwig(Environment $twig): void |
||
99 | |||
100 | public function setTranslator(TranslatorInterface $translator): void |
||
104 | |||
105 | public function getStep(): StepInterface |
||
113 | |||
114 | public function stepName(): string |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | protected function getCurrentCharacter(): array |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | protected function getCharacterProperty(string $key = null) |
||
140 | |||
141 | /** |
||
142 | * @return RedirectResponse |
||
143 | */ |
||
144 | protected function nextStep(): RedirectResponse |
||
148 | |||
149 | /** |
||
150 | * Redirects to a specific step and updates the session. |
||
151 | * |
||
152 | * @param int $stepNumber |
||
153 | * |
||
154 | * @return RedirectResponse |
||
155 | */ |
||
156 | protected function goToStep(int $stepNumber): RedirectResponse |
||
172 | |||
173 | /** |
||
174 | * @param mixed $value |
||
175 | */ |
||
176 | protected function updateCharacterStep($value): void |
||
189 | |||
190 | /** |
||
191 | * Adds a new flash message. |
||
192 | */ |
||
193 | protected function flashMessage(string $msg, string $type = null, array $msgParams = []): self |
||
216 | |||
217 | protected function getRequest(): Request |
||
225 | |||
226 | protected function getSession(): Session |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | private function setSteps(array $steps): void |
||
253 | } |
||
254 |
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.