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 |
||
17 | final class Processor implements ProcessorInterface |
||
18 | { |
||
19 | /** @var Handlers */ |
||
20 | private $handlers; |
||
21 | 36 | /** @var ParserInterface */ |
|
22 | private $parser; |
||
23 | 36 | /** @var EventContainerInterface */ |
|
24 | 36 | private $eventContainer; |
|
25 | 36 | ||
26 | private $recursionDepth = null; // infinite recursion |
||
27 | private $maxIterations = 1; // one iteration |
||
28 | private $autoProcessContent = true; // automatically process shortcode content |
||
29 | |||
30 | public function __construct(ParserInterface $parser, Handlers $handlers) |
||
35 | 32 | ||
36 | /** |
||
37 | 32 | * Entry point for shortcode processing. Implements iterative algorithm for |
|
38 | 32 | * both limited and unlimited number of iterations. |
|
39 | 32 | * |
|
40 | * @param string $text Text to process |
||
41 | 32 | * |
|
42 | 32 | * @return string |
|
43 | 32 | */ |
|
44 | 32 | public function process($text) |
|
62 | 32 | ||
63 | 32 | private function dispatchEvent($name, $event) |
|
76 | 32 | ||
77 | private function processIteration($text, ProcessorContext $context, ShortcodeInterface $parent = null) |
||
104 | 24 | ||
105 | private function applyReplaces($text, array $replaces) |
||
111 | |||
112 | 17 | private function prepareHandlerContext(ParsedShortcodeInterface $shortcode, ProcessorContext $context) |
|
123 | |||
124 | 3 | private function processHandler(ParsedShortcodeInterface $parsed, ProcessorContext $context, $handler) |
|
134 | 2 | ||
135 | private function processRecursion(ParsedShortcodeInterface $shortcode, ProcessorContext $context) |
||
148 | |||
149 | 3 | /** |
|
150 | 1 | * Container for event handlers used in this processor. |
|
151 | 1 | * |
|
152 | * @param EventContainerInterface $eventContainer |
||
153 | * |
||
154 | 2 | * @return self |
|
155 | 2 | */ |
|
156 | public function withEventContainer(EventContainerInterface $eventContainer) |
||
163 | |||
164 | /** |
||
165 | * Recursion depth level, null means infinite, any integer greater than or |
||
166 | * equal to zero sets value (number of recursion levels). Zero disables |
||
167 | * recursion. Defaults to null. |
||
168 | * |
||
169 | 3 | * @param int|null $depth |
|
170 | * |
||
171 | 3 | * @return self |
|
172 | 1 | */ |
|
173 | 1 | View Code Duplication | public function withRecursionDepth($depth) |
185 | |||
186 | /** |
||
187 | * Maximum number of iterations, null means infinite, any integer greater |
||
188 | * than zero sets value. Zero is invalid because there must be at least one |
||
189 | * iteration. Defaults to 1. Loop breaks if result of two consequent |
||
190 | * iterations shows no change in processed text. |
||
191 | * |
||
192 | * @param int|null $iterations |
||
193 | * |
||
194 | * @return self |
||
195 | */ |
||
196 | View Code Duplication | public function withMaxIterations($iterations) |
|
208 | |||
209 | /** |
||
210 | * Whether shortcode content will be automatically processed and handler |
||
211 | * already receives shortcode with processed content. If false, every |
||
212 | * shortcode handler needs to process content on its own. Default true. |
||
213 | * |
||
214 | * @param bool $flag True if enabled (default), false otherwise |
||
215 | * |
||
216 | * @return self |
||
217 | */ |
||
218 | public function withAutoProcessContent($flag) |
||
230 | } |
||
231 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.