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 |
||
19 | class NodeTwigExtension extends Twig_Extension |
||
|
|||
20 | { |
||
21 | /** |
||
22 | * @var EntityManagerInterface |
||
23 | */ |
||
24 | private $em; |
||
25 | |||
26 | /** |
||
27 | * @var UrlGeneratorInterface |
||
28 | */ |
||
29 | private $generator; |
||
30 | |||
31 | /** |
||
32 | * @var NodeMenu |
||
33 | */ |
||
34 | private $nodeMenu; |
||
35 | |||
36 | /** |
||
37 | * @var RequestStack |
||
38 | */ |
||
39 | private $requestStack; |
||
40 | |||
41 | /** |
||
42 | * @param \Doctrine\ORM\EntityManagerInterface $em |
||
43 | * @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $generator |
||
44 | * @param \Kunstmaan\NodeBundle\Helper\NodeMenu $nodeMenu |
||
45 | * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack |
||
46 | */ |
||
47 | public function __construct( |
||
48 | EntityManagerInterface $em, |
||
49 | UrlGeneratorInterface $generator, |
||
50 | NodeMenu $nodeMenu, |
||
51 | RequestStack $requestStack |
||
52 | ) { |
||
53 | $this->em = $em; |
||
54 | $this->generator = $generator; |
||
55 | $this->nodeMenu = $nodeMenu; |
||
56 | $this->requestStack = $requestStack; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Returns a list of functions to add to the existing list. |
||
61 | * |
||
62 | * @return array An array of functions |
||
63 | */ |
||
64 | public function getFunctions() |
||
65 | { |
||
66 | return array( |
||
67 | new \Twig_SimpleFunction( |
||
68 | 'get_node_for', array($this, 'getNodeFor') |
||
69 | ), |
||
70 | new \Twig_SimpleFunction( |
||
71 | 'get_node_translation_for', |
||
72 | array($this, 'getNodeTranslationFor') |
||
73 | ), |
||
74 | new \Twig_SimpleFunction( |
||
75 | 'get_node_by_internal_name', |
||
76 | array($this, 'getNodeByInternalName') |
||
77 | ), |
||
78 | new \Twig_SimpleFunction( |
||
79 | 'get_url_by_internal_name', |
||
80 | array($this, 'getUrlByInternalName') |
||
81 | ), |
||
82 | new \Twig_SimpleFunction( |
||
83 | 'get_path_by_internal_name', |
||
84 | array($this, 'getPathByInternalName') |
||
85 | ), |
||
86 | new \Twig_SimpleFunction( |
||
87 | 'get_page_by_node_translation', |
||
88 | array($this, 'getPageByNodeTranslation') |
||
89 | ), |
||
90 | new \Twig_SimpleFunction( |
||
91 | 'get_node_menu', |
||
92 | array($this, 'getNodeMenu') |
||
93 | ), |
||
94 | new \Twig_SimpleFunction( |
||
95 | 'is_structure_node', |
||
96 | array($this, 'isStructureNode') |
||
97 | ), |
||
98 | new \Twig_SimpleFunction( |
||
99 | 'file_exists', |
||
100 | array($this, 'fileExists') |
||
101 | ), |
||
102 | new \Twig_SimpleFunction( |
||
103 | 'get_node_trans_by_node_id', |
||
104 | array($this, 'getNodeTranslationByNodeId') |
||
105 | ), |
||
106 | new \Twig_SimpleFunction( |
||
107 | 'getOverviewRoute', |
||
108 | array($this, 'getOverviewRoute') |
||
109 | ), |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get the node translation object based on node id and language. |
||
115 | * |
||
116 | * @param int $nodeId |
||
117 | * @param string $lang |
||
118 | * |
||
119 | * @return NodeTranslation |
||
120 | */ |
||
121 | public function getNodeTranslationByNodeId($nodeId, $lang) |
||
127 | |||
128 | /** |
||
129 | * @param NodeTranslation $nodeTranslation |
||
130 | * |
||
131 | * @return null|object |
||
132 | */ |
||
133 | public function getPageByNodeTranslation(NodeTranslation $nodeTranslation) |
||
137 | |||
138 | /** |
||
139 | * @param PageInterface $page |
||
140 | * |
||
141 | * @return Node |
||
142 | */ |
||
143 | public function getNodeFor(PageInterface $page) |
||
147 | |||
148 | /** |
||
149 | * @param PageInterface $page |
||
150 | * |
||
151 | * @return NodeTranslation |
||
152 | */ |
||
153 | public function getNodeTranslationFor(PageInterface $page) |
||
157 | |||
158 | /** |
||
159 | * @param string $internalName |
||
160 | * @param string $locale |
||
161 | * |
||
162 | * @return Node|null |
||
163 | */ |
||
164 | public function getNodeByInternalName($internalName, $locale) |
||
174 | |||
175 | /** |
||
176 | * @param string $internalName Internal name of the node |
||
177 | * @param string $locale Locale |
||
178 | * @param array $parameters (optional) extra parameters |
||
179 | * @param bool $relative (optional) return relative path? |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | View Code Duplication | public function getPathByInternalName($internalName, $locale, $parameters = array(), $relative = false) |
|
193 | |||
194 | /** |
||
195 | * @param string $internalName Internal name of the node |
||
196 | * @param string $locale Locale |
||
197 | * @param array $parameters (optional) extra parameters |
||
198 | * @param bool $schemeRelative (optional) return relative scheme? |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | View Code Duplication | public function getUrlByInternalName($internalName, $locale, $parameters = array(), $schemeRelative = false) |
|
212 | |||
213 | /** |
||
214 | * @param string $locale |
||
215 | * @param Node $node |
||
216 | * @param bool $includeHiddenFromNav |
||
217 | * |
||
218 | * @return NodeMenu |
||
219 | */ |
||
220 | public function getNodeMenu($locale, Node $node = null, $includeHiddenFromNav = false) |
||
231 | |||
232 | public function isStructureNode($page) |
||
236 | |||
237 | public function fileExists($filename) |
||
241 | |||
242 | /** |
||
243 | * @param string $internalName |
||
244 | * @param string $locale |
||
245 | * @param array $parameters |
||
246 | * |
||
247 | * @return array |
||
248 | */ |
||
249 | private function getRouteParametersByInternalName($internalName, $locale, $parameters = array()) |
||
267 | |||
268 | public function getOverviewRoute($node) |
||
269 | { |
||
276 | } |
||
277 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.