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 BaseController extends Controller { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Returns the default locale for the system |
||
| 23 | * |
||
| 24 | * @return string |
||
| 25 | */ |
||
| 26 | protected function getDefaultLocale() { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Returns all available (i.e. |
||
| 32 | * configured) locales of the system |
||
| 33 | * |
||
| 34 | * @return string[] |
||
| 35 | */ |
||
| 36 | protected function getLocales() { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Proxy to use the transChoice method of the translator |
||
| 44 | * |
||
| 45 | * @see TranslatorInterface::transChoice() |
||
| 46 | * |
||
| 47 | * @param string $id |
||
| 48 | * @param int $number |
||
| 49 | * @param array $parameters |
||
| 50 | * @param string $domain |
||
|
|
|||
| 51 | * @param string $locale |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | View Code Duplication | protected function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Proxy to use the transChoice method of the translator |
||
| 65 | * |
||
| 66 | * @see TranslatorInterface::trans() |
||
| 67 | * |
||
| 68 | * @param string $id |
||
| 69 | * @param array $parameters |
||
| 70 | * @param string $domain |
||
| 71 | * @param string $locale |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | View Code Duplication | protected function trans($id, array $parameters = array(), $domain = null, $locale = null) { |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the translator service |
||
| 85 | * |
||
| 86 | * @return TranslatorInterface |
||
| 87 | */ |
||
| 88 | protected function getTranslator() { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns the paginator service |
||
| 94 | * |
||
| 95 | * @return \Knp\Component\Pager\PaginatorInterface |
||
| 96 | */ |
||
| 97 | protected function getPaginator() { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns the stopwatch service if available |
||
| 103 | * |
||
| 104 | * @return Stopwatch|NULL |
||
| 105 | */ |
||
| 106 | protected function getStopWatch() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Checks whether the current user has the specified role or not |
||
| 115 | * |
||
| 116 | * @param string $role |
||
| 117 | * @throws \LogicException |
||
| 118 | * @return boolean |
||
| 119 | */ |
||
| 120 | protected function hasRole($role) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns the repository for the given clazz |
||
| 129 | * |
||
| 130 | * @param string $class |
||
| 131 | * @return ObjectRepository |
||
| 132 | */ |
||
| 133 | protected function getRepository($class) { |
||
| 136 | } |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.