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() |
||
| 66 | { |
||
| 67 | return [ |
||
| 68 | new Twig_SimpleFunction('getSortColumnUrl', [$this, 'getSortColumnUrl']), |
||
| 69 | new Twig_SimpleFunction('getSortColumnIconClass', [$this, 'getSortColumnIconClass']), |
||
| 70 | new Twig_SimpleFunction('field', [$this, 'field']), |
||
| 71 | new Twig_SimpleFunction('field_title', [$this, 'fieldTitle']), |
||
| 72 | new Twig_SimpleFunction('route_parameters', [$this, 'routeParameters']), |
||
| 73 | ]; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function getFilters() |
||
| 80 | { |
||
| 81 | return [ |
||
| 82 | new Twig_SimpleFilter('camelize', [$this, 'camelize']), |
||
| 83 | ]; |
||
| 84 | } |
||
| 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 | */ |
||
| 94 | public function getSortColumnUrl(Request $request, $fieldName) |
||
| 95 | { |
||
| 96 | // get query string to not delete existing parameters |
||
| 97 | $queryString = $request->query->all(); |
||
| 98 | $queryString['sort'] = $fieldName; |
||
| 99 | |||
| 100 | if (array_key_exists('order', $queryString)) { |
||
| 101 | // sort by opposite sorting than current |
||
| 102 | $sort = $queryString['order'] == 'ASC' ? 'DESC' : 'ASC'; |
||
| 103 | $queryString['order'] = $sort; |
||
| 104 | } else { |
||
| 105 | // if no order was provided, it means that list is sorted by default order (ASC), so we must sort by DESC |
||
| 106 | $queryString['order'] = 'DESC'; |
||
| 107 | } |
||
| 108 | |||
| 109 | return $this |
||
| 110 | ->router |
||
| 111 | ->generate($request->get('_route'), $queryString); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Return an array of query string parameters, updated with sort field name. |
||
| 116 | * |
||
| 117 | * @param ParameterBagInterface $parameters |
||
| 118 | * @param $fieldName |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public function getOrderQueryString(ParameterBagInterface $parameters, $fieldName) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param null $order |
||
| 138 | * @param $fieldName |
||
| 139 | * @param $sort |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function getSortColumnIconClass($order = null, $fieldName, $sort) |
||
| 143 | { |
||
| 144 | // when no order is provided, no icon should be displayed |
||
| 145 | $class = ''; |
||
| 146 | |||
| 147 | if ($fieldName == $sort) { |
||
| 148 | if ($order == 'ASC') { |
||
| 149 | $class = 'fa fa-sort-asc'; |
||
| 150 | } elseif ($order == 'DESC') { |
||
| 151 | $class = 'fa fa-sort-desc'; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | return $class; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Render a field of an entity. |
||
| 160 | * |
||
| 161 | * @param FieldInterface $field |
||
| 162 | * @param $entity |
||
| 163 | * |
||
| 164 | * @return mixed |
||
| 165 | */ |
||
| 166 | public function field(FieldInterface $field, $entity) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Return a the title of the field, camelized or translated. |
||
| 187 | * |
||
| 188 | * @param $fieldName |
||
| 189 | * @param null $adminName |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function fieldTitle($fieldName, $adminName = null) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param array $parameters |
||
| 207 | * @param $entity |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public function routeParameters(array $parameters, $entity) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Camelize a string (using Container camelize method) |
||
| 227 | * |
||
| 228 | * @param $string |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public function camelize($string) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns the name of the extension. |
||
| 238 | * |
||
| 239 | * @return string The extension name |
||
| 240 | */ |
||
| 241 | public function getName() |
||
| 245 | } |
||
| 246 |