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 |
||
| 14 | class ViewDeriver extends ViewDeriverBase implements ContainerDeriverInterface { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * {@inheritdoc} |
||
| 18 | */ |
||
| 19 | public function getDerivativeDefinitions($basePluginDefinition) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Helper function to return the contextual filter argument if any exist. |
||
| 61 | * |
||
| 62 | * @param array $arguments |
||
| 63 | * The array of available arguments. |
||
| 64 | * @param $id |
||
| 65 | * The plugin derivative id. |
||
| 66 | * |
||
| 67 | * @return array |
||
| 68 | * The contextual filter argument if applicable. |
||
| 69 | */ |
||
| 70 | View Code Duplication | protected function getContextualArguments(array $arguments, $id) { |
|
| 71 | if (!empty($arguments)) { |
||
| 72 | return [ |
||
| 73 | 'contextualFilter' => [ |
||
| 74 | 'type' => StringHelper::camelCase($id, 'contextual', 'filter', 'input'), |
||
| 75 | ], |
||
| 76 | ]; |
||
| 77 | } |
||
| 78 | |||
| 79 | return []; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Helper function to retrieve the sort arguments if any are exposed. |
||
| 84 | * |
||
| 85 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 86 | * The display plugin. |
||
| 87 | * @param $id |
||
| 88 | * The plugin derivative id. |
||
| 89 | * |
||
| 90 | * @return array |
||
| 91 | * The sort arguments if any exposed sorts are available. |
||
| 92 | */ |
||
| 93 | View Code Duplication | protected function getSortArguments(DisplayPluginInterface $display, $id) { |
|
| 94 | $sorts = array_filter($display->getOption('sorts') ?: [], function ($sort) { |
||
| 95 | return $sort['exposed']; |
||
| 96 | }); |
||
| 97 | return $sorts ? [ |
||
| 98 | 'sortDirection' => [ |
||
| 99 | 'type' => 'ViewSortDirection', |
||
| 100 | 'default' => 'asc', |
||
| 101 | ], |
||
| 102 | 'sortBy' => [ |
||
| 103 | 'type' => StringHelper::camelCase($id, 'sort', 'by'), |
||
| 104 | ], |
||
| 105 | ] : []; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Helper function to return the filter argument if applicable. |
||
| 110 | * |
||
| 111 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 112 | * The display plugin. |
||
| 113 | * @param $id |
||
| 114 | * The plugin derivative id. |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | * The filter argument if any exposed filters are available. |
||
| 118 | */ |
||
| 119 | View Code Duplication | protected function getFilterArguments(DisplayPluginInterface $display, $id) { |
|
| 120 | $filters = array_filter($display->getOption('filters') ?: [], function($filter) { |
||
| 121 | return array_key_exists('exposed', $filter) && $filter['exposed']; |
||
| 122 | }); |
||
| 123 | |||
| 124 | return !empty($filters) ? [ |
||
| 125 | 'filter' => [ |
||
| 126 | 'type' => $display->getGraphQLFilterInputName(), |
||
| 127 | ], |
||
| 128 | ] : []; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Helper function to retrieve the pager arguments if the display is paged. |
||
| 133 | * |
||
| 134 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 135 | * The display plugin. |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | * An array of pager arguments if the view display is paged. |
||
| 139 | */ |
||
| 140 | protected function getPagerArguments(DisplayPluginInterface $display) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Helper function to retrieve the types that the view can be attached to. |
||
| 149 | * |
||
| 150 | * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display |
||
| 151 | * The display plugin. |
||
| 152 | * @param array $arguments |
||
| 153 | * An array containing information about the available arguments. |
||
| 154 | * @return array |
||
| 155 | * An array of additional types the view can be embedded in. |
||
| 156 | */ |
||
| 157 | protected function getTypes(DisplayPluginInterface $display, array $arguments) { |
||
| 197 | |||
| 198 | } |
||
| 199 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: