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:
Complex classes like ViewDeriverHelperTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ViewDeriverHelperTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | trait ViewDeriverHelperTrait { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Helper function to return the contextual filter argument if any exist. |
||
| 20 | * |
||
| 21 | * @param array $arguments |
||
| 22 | * The array of available arguments. |
||
| 23 | * @param string $id |
||
| 24 | * The plugin derivative id. |
||
| 25 | * |
||
| 26 | * @return array |
||
| 27 | * The contextual filter argument if applicable. |
||
| 28 | */ |
||
| 29 | protected function getContextualArguments(array $arguments, $id) { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Helper function to retrieve the sort arguments if any are exposed. |
||
| 43 | * |
||
| 44 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 45 | * The display plugin. |
||
| 46 | * @param string $id |
||
| 47 | * The plugin derivative id. |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | * The sort arguments if any exposed sorts are available. |
||
| 51 | */ |
||
| 52 | protected function getSortArguments(DisplayPluginInterface $display, $id) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Helper function to return the filter argument if applicable. |
||
| 69 | * |
||
| 70 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 71 | * The display plugin. |
||
| 72 | * @param string $id |
||
| 73 | * The plugin derivative id. |
||
| 74 | * |
||
| 75 | * @return array |
||
| 76 | * The filter argument if any exposed filters are available. |
||
| 77 | */ |
||
| 78 | protected function getFilterArguments(DisplayPluginInterface $display, $id) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Helper function to retrieve the pager arguments if the display is paged. |
||
| 92 | * |
||
| 93 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 94 | * The display plugin. |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | * An array of pager arguments if the view display is paged. |
||
| 98 | */ |
||
| 99 | protected function getPagerArguments(DisplayPluginInterface $display) { |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Helper function to retrieve the types that the view can be attached to. |
||
| 111 | * |
||
| 112 | * @param array $arguments |
||
| 113 | * An array containing information about the available arguments. |
||
| 114 | * @param array $types |
||
| 115 | * Types where it needs to be added. |
||
| 116 | * |
||
| 117 | * @return array |
||
| 118 | * An array of additional types the view can be embedded in. |
||
| 119 | */ |
||
| 120 | protected function getTypes(array $arguments, array $types = ['Root']) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Check if a pager is configured. |
||
| 152 | * |
||
| 153 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 154 | * The display configuration. |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | * Flag indicating if the view is configured with a pager. |
||
| 158 | */ |
||
| 159 | protected function isPaged(DisplayPluginInterface $display) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns a view display object. |
||
| 169 | * |
||
| 170 | * @param \Drupal\views\ViewEntityInterface $view |
||
| 171 | * The view object. |
||
| 172 | * @param string $displayId |
||
| 173 | * The display ID to use. |
||
| 174 | * |
||
| 175 | * @return \Drupal\views\Plugin\views\display\DisplayPluginInterface |
||
| 176 | * The view display object. |
||
| 177 | */ |
||
| 178 | protected function getViewDisplay(ViewEntityInterface $view, $displayId) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the configured default limit. |
||
| 186 | * |
||
| 187 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 188 | * The display configuration. |
||
| 189 | * |
||
| 190 | * @return int |
||
| 191 | * The default limit. |
||
| 192 | */ |
||
| 193 | View Code Duplication | protected function getPagerLimit(DisplayPluginInterface $display) { |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Get the configured default offset. |
||
| 203 | * |
||
| 204 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 205 | * The display configuration. |
||
| 206 | * |
||
| 207 | * @return int |
||
| 208 | * The default offset. |
||
| 209 | */ |
||
| 210 | View Code Duplication | protected function getPagerOffset(DisplayPluginInterface $display) { |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Check if a certain interface exists. |
||
| 220 | * |
||
| 221 | * @param string $interface |
||
| 222 | * The GraphQL interface name. |
||
| 223 | * @param \Drupal\Component\Plugin\PluginManagerInterface $interfacePluginManager |
||
| 224 | * Plugin interface manager. |
||
| 225 | * |
||
| 226 | * @return bool |
||
| 227 | * Boolean flag indicating if the interface exists. |
||
| 228 | */ |
||
| 229 | protected function interfaceExists($interface, PluginManagerInterface $interfacePluginManager) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Retrieves the type the view's rows resolve to. |
||
| 237 | * |
||
| 238 | * @param \Drupal\views\ViewEntityInterface $view |
||
| 239 | * The view entity. |
||
| 240 | * @param string $displayId |
||
| 241 | * The id of the current display. |
||
| 242 | * @param \Drupal\Component\Plugin\PluginManagerInterface $interfacePluginManager |
||
| 243 | * Interface plugin manager. |
||
| 244 | * |
||
| 245 | * @return null|string |
||
| 246 | * The name of the type or NULL if the type could not be derived. |
||
| 247 | */ |
||
| 248 | protected function getRowResolveType(ViewEntityInterface $view, $displayId, PluginManagerInterface $interfacePluginManager) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns a view style object. |
||
| 292 | * |
||
| 293 | * @param \Drupal\views\ViewEntityInterface $view |
||
| 294 | * The view object. |
||
| 295 | * @param string $displayId |
||
| 296 | * The display ID to use. |
||
| 297 | * |
||
| 298 | * @return \Drupal\views\Plugin\views\style\StylePluginBase |
||
| 299 | * The view style object. |
||
| 300 | */ |
||
| 301 | protected function getViewStyle(ViewEntityInterface $view, $displayId) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Returns cache metadata plugin definitions. |
||
| 309 | * |
||
| 310 | * @param \Drupal\views\ViewEntityInterface $view |
||
| 311 | * The view object. |
||
| 312 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 313 | * The view display. |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | * The cache metadata definitions for the plugin definition. |
||
| 317 | */ |
||
| 318 | protected function getCacheMetadataDefinition(ViewEntityInterface $view, DisplayPluginInterface $display) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Returns information about view arguments (contextual filters). |
||
| 336 | * |
||
| 337 | * @param array $viewArguments |
||
| 338 | * The "arguments" option of a view display. |
||
| 339 | * |
||
| 340 | * @return array |
||
| 341 | * Arguments information keyed by the argument ID. Subsequent array keys: |
||
| 342 | * - index: argument index. |
||
| 343 | * - entity_type: target entity type. |
||
| 344 | * - bundles: target bundles (can be empty). |
||
| 345 | * |
||
| 346 | * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
||
| 347 | */ |
||
| 348 | protected function getArgumentsInfo(array $viewArguments) { |
||
| 384 | |||
| 385 | } |
||
| 386 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.