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( |
||
| 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() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the node translation object based on node id and language. |
||
| 120 | * |
||
| 121 | * @param int $nodeId |
||
| 122 | * @param string $lang |
||
| 123 | * |
||
| 124 | * @return NodeTranslation |
||
| 125 | */ |
||
| 126 | public function getNodeTranslationByNodeId($nodeId, $lang) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param NodeTranslation $nodeTranslation |
||
| 135 | * |
||
| 136 | * @return null|object |
||
| 137 | */ |
||
| 138 | public function getPageByNodeTranslation(NodeTranslation $nodeTranslation) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param PageInterface $page |
||
| 145 | * |
||
| 146 | * @return Node |
||
| 147 | */ |
||
| 148 | public function getNodeFor(PageInterface $page) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param PageInterface $page |
||
| 155 | * |
||
| 156 | * @return NodeTranslation |
||
| 157 | */ |
||
| 158 | public function getNodeTranslationFor(PageInterface $page) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $internalName |
||
| 165 | * @param string $locale |
||
| 166 | * |
||
| 167 | * @return Node|null |
||
| 168 | */ |
||
| 169 | public function getNodeByInternalName($internalName, $locale) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $internalName Internal name of the node |
||
| 182 | * @param string $locale Locale |
||
| 183 | * @param array $parameters (optional) extra parameters |
||
| 184 | * @param bool $relative (optional) return relative path? |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function getPathByInternalName($internalName, $locale, $parameters = array(), $relative = false) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $internalName Internal name of the node |
||
| 201 | * @param string $locale Locale |
||
| 202 | * @param array $parameters (optional) extra parameters |
||
| 203 | * @param bool $schemeRelative (optional) return relative scheme? |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | View Code Duplication | public function getUrlByInternalName($internalName, $locale, $parameters = array(), $schemeRelative = false) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $locale |
||
| 220 | * @param Node $node |
||
| 221 | * @param bool $includeHiddenFromNav |
||
| 222 | * |
||
| 223 | * @return NodeMenu |
||
| 224 | */ |
||
| 225 | public function getNodeMenu($locale, Node $node = null, $includeHiddenFromNav = false) |
||
| 236 | |||
| 237 | public function isStructureNode($page) |
||
| 241 | |||
| 242 | public function isStructurePage($page) |
||
| 246 | |||
| 247 | public function fileExists($filename) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $internalName |
||
| 254 | * @param string $locale |
||
| 255 | * @param array $parameters |
||
| 256 | * |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | private function getRouteParametersByInternalName($internalName, $locale, $parameters = array()) |
||
| 277 | |||
| 278 | public function getOverviewRoute($node) |
||
| 286 | } |
||
| 287 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.