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 |
||
| 26 | class AdminExtension extends Twig_Extension |
||
| 27 | { |
||
| 28 | use TranslationKeyTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var ApplicationConfiguration |
||
| 32 | */ |
||
| 33 | protected $configuration; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var RouterInterface |
||
| 37 | */ |
||
| 38 | protected $router; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var TranslatorInterface |
||
| 42 | */ |
||
| 43 | protected $translator; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * AdminExtension constructor. |
||
| 47 | * |
||
| 48 | * @param RouterInterface $router |
||
| 49 | * @param TranslatorInterface $translator |
||
| 50 | * @param ConfigurationFactory $configurationFactory |
||
| 51 | */ |
||
| 52 | public function __construct( |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function getFunctions() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function getFilters() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Return sort column url, with existing request parameters, according to field name. |
||
| 88 | * |
||
| 89 | * @param Request $request |
||
| 90 | * @param $fieldName |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | * TODO rename function |
||
| 94 | */ |
||
| 95 | public function getSortColumnUrl(Request $request, $fieldName) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Return an array of query string parameters, updated with sort field name. |
||
| 117 | * |
||
| 118 | * @param ParameterBagInterface $parameters |
||
| 119 | * @param $fieldName |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function getOrderQueryString(ParameterBagInterface $parameters, $fieldName) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param null $order |
||
| 139 | * @param $fieldName |
||
| 140 | * @param $sort |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getSortColumnIconClass($order = null, $fieldName, $sort) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Render a field of an entity. |
||
| 161 | * |
||
| 162 | * @param FieldInterface $field |
||
| 163 | * @param $entity |
||
| 164 | * |
||
| 165 | * @return mixed |
||
| 166 | */ |
||
| 167 | public function field(FieldInterface $field, $entity) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param $fieldName |
||
| 188 | * @param null $adminName |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function fieldTitle($fieldName, $adminName = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param array $parameters |
||
| 206 | * @param $entity |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public function routeParameters(array $parameters, $entity) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Camelize a string (using Container camelize method) |
||
| 226 | * |
||
| 227 | * @param $string |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function camelize($string) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns the name of the extension. |
||
| 237 | * |
||
| 238 | * @return string The extension name |
||
| 239 | */ |
||
| 240 | public function getName() |
||
| 244 | } |
||
| 245 |