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 |
||
30 | class TemplatePathResolver extends AbstractPathResolver |
||
31 | { |
||
32 | /** |
||
33 | * @var UrlGenerator |
||
34 | */ |
||
35 | private $urlGenerator; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | private $checkPaths; |
||
41 | |||
42 | 23 | public function __construct(ResourceRepository $repo, UrlGenerator $urlGenerator = null, $checkPaths = false) |
|
49 | |||
50 | /** |
||
51 | * Returns the priority for this visitor. |
||
52 | * |
||
53 | * Priority should be between -10 and 10 (0 is the default). |
||
54 | * |
||
55 | * @return int The priority level |
||
56 | */ |
||
57 | 23 | public function getPriority() |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 19 | protected function processNode(Twig_Node $node) |
|
85 | |||
86 | 19 | private function processModuleNode(Twig_Node_Module $node) |
|
87 | { |
||
88 | // Resolve relative parent template paths to absolute paths |
||
89 | 19 | $parentNode = $node->getNode('parent'); |
|
90 | 19 | $traitsNode = $node->getNode('traits'); |
|
91 | |||
92 | // If the template extends another template, resolve the path |
||
93 | 19 | if ($parentNode instanceof Twig_Node_Expression_Constant) { |
|
94 | 5 | $this->processConstantNode($parentNode, $this->checkPaths); |
|
95 | } |
||
96 | |||
97 | // Resolve paths of embedded templates |
||
98 | 19 | foreach ($node->getAttribute('embedded_templates') as $embeddedNode) { |
|
99 | 2 | $this->processEmbeddedTemplateNode($embeddedNode); |
|
100 | } |
||
101 | |||
102 | // Resolve paths of used templates |
||
103 | 19 | foreach ($traitsNode as $traitNode) { |
|
104 | 2 | $this->processTraitNode($traitNode); |
|
105 | } |
||
106 | |||
107 | 19 | return null; |
|
108 | } |
||
109 | |||
110 | 2 | private function processEmbeddedTemplateNode(Twig_Node_Module $embeddedNode) |
|
111 | { |
||
112 | 2 | $embedParent = $embeddedNode->getNode('parent'); |
|
113 | |||
114 | // If the template extends another template, resolve the path |
||
115 | 2 | if ($embedParent instanceof Twig_Node_Expression_Constant) { |
|
116 | 2 | $this->processConstantNode($embedParent, $this->checkPaths); |
|
117 | } |
||
118 | 2 | } |
|
119 | |||
120 | 2 | private function processTraitNode(Twig_Node $traitNode) |
|
121 | { |
||
122 | 2 | $usedTemplate = $traitNode->getNode('template'); |
|
123 | |||
124 | // If the template extends another template, resolve the path |
||
125 | 2 | if ($usedTemplate instanceof Twig_Node_Expression_Constant) { |
|
126 | 2 | $this->processConstantNode($usedTemplate, $this->checkPaths); |
|
127 | } |
||
128 | 2 | } |
|
129 | |||
130 | 6 | View Code Duplication | private function processIncludeNode(Twig_Node_Include $node) |
|
|||
131 | { |
||
132 | 6 | $exprNode = $node->getNode('expr'); |
|
133 | |||
134 | 6 | if ($exprNode instanceof Twig_Node_Expression_Constant) { |
|
135 | 6 | $this->processConstantNode($exprNode, $this->checkPaths); |
|
136 | } |
||
137 | |||
138 | 6 | return null; |
|
139 | } |
||
140 | |||
141 | 2 | View Code Duplication | private function processImportNode(Twig_Node_Import $node) |
142 | { |
||
143 | 2 | $exprNode = $node->getNode('expr'); |
|
144 | |||
145 | 2 | if ($exprNode instanceof Twig_Node_Expression_Constant) { |
|
146 | 2 | $this->processConstantNode($exprNode, $this->checkPaths); |
|
147 | } |
||
148 | |||
149 | 2 | return null; |
|
150 | } |
||
151 | |||
152 | 3 | protected function processResourceUrlFunction(Twig_Node $node) |
|
182 | |||
183 | 16 | private function processConstantNode(Twig_Node_Expression_Constant $node, $checkPath) |
|
187 | } |
||
188 |
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.