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 |
||
60 | class SectionViewHelper extends AbstractViewHelper implements NodeFilterInterface |
||
61 | { |
||
62 | use ParserRuntimeOnly; |
||
63 | |||
64 | /** |
||
65 | * @var boolean |
||
66 | */ |
||
67 | protected $escapeOutput = false; |
||
68 | |||
69 | /** |
||
70 | * Initialize the arguments. |
||
71 | * |
||
72 | * @return void |
||
73 | * @api |
||
74 | */ |
||
75 | public function initializeArguments() |
||
79 | |||
80 | /** |
||
81 | * Save the associated ViewHelper node in a static public class variable. |
||
82 | * called directly after the ViewHelper was built. |
||
83 | * |
||
84 | * @param ViewHelperNode $node |
||
85 | * @param TextNode[] $arguments |
||
86 | * @param VariableProviderInterface $variableContainer |
||
87 | * @return void |
||
88 | */ |
||
89 | public static function postParseEvent(ViewHelperNode $node, array $arguments, VariableProviderInterface $variableContainer) |
||
98 | |||
99 | /** |
||
100 | * Section ViewHelper disallows any whitespace-only text nodes as children. The result is that |
||
101 | * the section wrapping itself becomes "invisible" and the output of the section when rendered |
||
102 | * does not include the whitespace leading up to or trailing any "real" child content. |
||
103 | * |
||
104 | * @param NodeInterface $node |
||
105 | * @return bool |
||
106 | */ |
||
107 | View Code Duplication | public function allowsChildNodeType(NodeInterface $node): bool |
|
114 | |||
115 | /** |
||
116 | * Rendering directly returns all child nodes. |
||
117 | * |
||
118 | * @return string HTML String of all child nodes. |
||
119 | * @api |
||
120 | */ |
||
121 | public function render() |
||
130 | } |
||
131 |
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.