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 |
||
| 25 | class SlugRouter implements RouterInterface |
||
| 26 | { |
||
| 27 | public static $SLUG = '_slug'; |
||
| 28 | |||
| 29 | public static $SLUG_PREVIEW = '_slug_preview'; |
||
| 30 | |||
| 31 | /** @var DomainConfigurationInterface */ |
||
| 32 | protected $domainConfiguration; |
||
| 33 | |||
| 34 | /** @var RequestStack */ |
||
| 35 | private $requestStack; |
||
| 36 | |||
| 37 | /** @var EntityManagerInterface */ |
||
| 38 | private $em; |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | protected $adminKey; |
||
| 42 | |||
| 43 | /** @var RequestContext */ |
||
| 44 | protected $context; |
||
| 45 | |||
| 46 | /** @var RouteCollection */ |
||
| 47 | protected $routeCollection; |
||
| 48 | |||
| 49 | /** @var UrlGenerator */ |
||
| 50 | protected $urlGenerator; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var ContainerInterface |
||
| 54 | * |
||
| 55 | * @deprecated in KunstmaanNodeBundle 5.1 and will be removed in KunstmaanNodeBundle 6.0. |
||
| 56 | */ |
||
| 57 | protected $container; |
||
| 58 | |||
| 59 | /** @var string */ |
||
| 60 | protected $slugPattern; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The constructor for this service |
||
| 64 | * |
||
| 65 | * @param ContainerInterface $container |
||
|
|
|||
| 66 | */ |
||
| 67 | 12 | View Code Duplication | public function __construct( |
| 92 | |||
| 93 | /** |
||
| 94 | * Match given urls via the context to the routes we defined. |
||
| 95 | * This functionality re-uses the default Symfony way of routing and its |
||
| 96 | * components |
||
| 97 | * |
||
| 98 | * @param string $pathinfo |
||
| 99 | * |
||
| 100 | * @throws ResourceNotFoundException |
||
| 101 | * |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | 2 | public function match($pathinfo) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Gets the request context. |
||
| 125 | * |
||
| 126 | * @return RequestContext The context |
||
| 127 | * |
||
| 128 | * @api |
||
| 129 | */ |
||
| 130 | 10 | public function getContext() |
|
| 131 | { |
||
| 132 | 10 | if (!isset($this->context)) { |
|
| 133 | /** @var Request $request */ |
||
| 134 | 9 | $request = $this->getMasterRequest(); |
|
| 135 | |||
| 136 | 9 | $this->context = new RequestContext(); |
|
| 137 | 9 | $this->context->fromRequest($request); |
|
| 138 | } |
||
| 139 | |||
| 140 | 10 | return $this->context; |
|
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Sets the request context. |
||
| 145 | * |
||
| 146 | * @param RequestContext $context The context |
||
| 147 | * |
||
| 148 | * @api |
||
| 149 | */ |
||
| 150 | 1 | public function setContext(RequestContext $context) |
|
| 151 | { |
||
| 152 | 1 | $this->context = $context; |
|
| 153 | 1 | } |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Generate an url for a supplied route. |
||
| 157 | * |
||
| 158 | * @param string $name The path |
||
| 159 | * @param array $parameters The route parameters |
||
| 160 | * @param int|bool $referenceType The type of reference to be generated (one of the UrlGeneratorInterface constants) |
||
| 161 | * |
||
| 162 | * @return null|string |
||
| 163 | */ |
||
| 164 | 2 | public function generate($name, $parameters = array(), $referenceType = UrlGenerator::ABSOLUTE_PATH) |
|
| 165 | { |
||
| 166 | 2 | $this->urlGenerator = new UrlGenerator( |
|
| 167 | 2 | $this->getRouteCollection(), |
|
| 168 | 2 | $this->getContext() |
|
| 169 | ); |
||
| 170 | |||
| 171 | 2 | return $this->urlGenerator->generate($name, $parameters, $referenceType); |
|
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Getter for routeCollection |
||
| 176 | * |
||
| 177 | * @return \Symfony\Component\Routing\RouteCollection |
||
| 178 | */ |
||
| 179 | 4 | public function getRouteCollection() |
|
| 180 | { |
||
| 181 | 4 | if (is_null($this->routeCollection)) { |
|
| 182 | 4 | $this->routeCollection = new RouteCollection(); |
|
| 183 | |||
| 184 | 4 | $this->addPreviewRoute(); |
|
| 185 | 4 | $this->addSlugRoute(); |
|
| 186 | } |
||
| 187 | |||
| 188 | 4 | return $this->routeCollection; |
|
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return null|\Symfony\Component\HttpFoundation\Request |
||
| 193 | */ |
||
| 194 | 9 | protected function getMasterRequest() |
|
| 195 | { |
||
| 196 | 9 | if (is_null($this->requestStack)) { |
|
| 197 | return null; |
||
| 198 | } |
||
| 199 | |||
| 200 | 9 | return $this->requestStack->getMasterRequest(); |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Add the preview route to the route collection |
||
| 205 | */ |
||
| 206 | 5 | protected function addPreviewRoute() |
|
| 207 | { |
||
| 208 | 5 | $routeParameters = $this->getPreviewRouteParameters(); |
|
| 209 | 5 | $this->addRoute(self::$SLUG_PREVIEW, $routeParameters); |
|
| 210 | 5 | } |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Add the slug route to the route collection |
||
| 214 | */ |
||
| 215 | 4 | protected function addSlugRoute() |
|
| 216 | { |
||
| 217 | 4 | $routeParameters = $this->getSlugRouteParameters(); |
|
| 218 | 4 | $this->addRoute(self::$SLUG, $routeParameters); |
|
| 219 | 4 | } |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Return preview route parameters |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | 11 | View Code Duplication | protected function getPreviewRouteParameters() |
| 251 | |||
| 252 | /** |
||
| 253 | * Return slug route parameters |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | 4 | View Code Duplication | protected function getSlugRouteParameters() |
| 282 | |||
| 283 | /** |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | 11 | protected function isMultiLanguage($host = null) |
|
| 287 | { |
||
| 288 | 11 | return $this->domainConfiguration->isMultiLanguage($host); |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | 7 | protected function getFrontendLocales() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | 8 | protected function getBackendLocales() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | 11 | protected function getDefaultLocale() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | protected function getHost() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | 11 | protected function getSlugPattern() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $name |
||
| 333 | * @param array $parameters |
||
| 334 | */ |
||
| 335 | 5 | View Code Duplication | protected function addRoute($name, array $parameters = array()) |
| 346 | |||
| 347 | /** |
||
| 348 | * @param array $matchResult |
||
| 349 | * |
||
| 350 | * @return \Kunstmaan\NodeBundle\Entity\NodeTranslation |
||
| 351 | */ |
||
| 352 | 2 | protected function getNodeTranslation($matchResult) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @return \Kunstmaan\NodeBundle\Repository\NodeTranslationRepository |
||
| 368 | */ |
||
| 369 | 4 | protected function getNodeTranslationRepository() |
|
| 378 | |||
| 379 | /** |
||
| 380 | * @param array $locales |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | 8 | protected function getEscapedLocales($locales) |
|
| 393 | } |
||
| 394 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.