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 |
||
23 | class CaseViewHelper extends AbstractViewHelper implements NodeFilterInterface |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var boolean |
||
28 | */ |
||
29 | protected $escapeOutput = false; |
||
30 | |||
31 | /** |
||
32 | * @return void |
||
33 | */ |
||
34 | public function initializeArguments() |
||
39 | |||
40 | /** |
||
41 | * @return string the contents of this view helper if $value equals the expression of the surrounding switch view helper, otherwise an empty string |
||
42 | * @throws ViewHelper\Exception |
||
43 | * @api |
||
44 | */ |
||
45 | public function render() |
||
61 | |||
62 | /** |
||
63 | * Case ViewHelper disallows any whitespace-only text nodes as children. This means that |
||
64 | * when rendered, the output of the node excludes any whitespace before and after the |
||
65 | * "real" output. |
||
66 | * |
||
67 | * @param NodeInterface $node |
||
68 | * @return bool |
||
69 | */ |
||
70 | View Code Duplication | public function allowsChildNodeType(NodeInterface $node): bool |
|
77 | |||
78 | /** |
||
79 | * @param string $argumentsName |
||
80 | * @param string $closureName |
||
81 | * @param string $initializationPhpCode |
||
82 | * @param ViewHelperNode $node |
||
83 | * @param TemplateCompiler $compiler |
||
84 | * @return string |
||
85 | */ |
||
86 | public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler) |
||
90 | } |
||
91 |
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.