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 |
||
| 29 | class PaginationBuilder extends AbstractPlugin |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The internal configuration stack. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $stack = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The generated result array. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $result = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Entry point. |
||
| 48 | * |
||
| 49 | * if $stack is provided, the internal stack is set to exact that value, so |
||
| 50 | * please be sure what you're doing. |
||
| 51 | * |
||
| 52 | * If you pass a boolean TRUE as $stack, the internal stack is reset. |
||
| 53 | * |
||
| 54 | * @param null|array|bool $stack |
||
| 55 | * @param bool $returnResult Should the result be immediately be returned instead of |
||
| 56 | * self. Only affective when $stack is an array. |
||
| 57 | * |
||
| 58 | * @return self|array |
||
| 59 | * @throws \InvalidArgumentException |
||
| 60 | */ |
||
| 61 | public function __invoke($stack = null, $returnResult = true) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Add arguments for the call to the CreatePaginator plugin. |
||
| 83 | * |
||
| 84 | * @see \Core\Controller\Plugin\CreatePaginator::__invoke() |
||
| 85 | * |
||
| 86 | * @param string $paginatorName |
||
| 87 | * @param array $defaultParams |
||
| 88 | * @param bool $usePostParams |
||
| 89 | * @param string $as The name of the key in the result array. |
||
| 90 | * |
||
| 91 | * @return self |
||
| 92 | */ |
||
| 93 | public function paginator($paginatorName, $defaultParams = [], $usePostParams = false, $as = 'paginator') |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add arguments for the call to the SearchForm plugin. |
||
| 109 | * |
||
| 110 | * @see \Core\Controller\Plugin\SearchForm::get() |
||
| 111 | * |
||
| 112 | * @param $elementsFieldset |
||
| 113 | * @param null $buttonsFieldset |
||
| 114 | * @param string $as The name of the key in the result array. |
||
| 115 | * |
||
| 116 | * @return self |
||
| 117 | */ |
||
| 118 | public function form($elementsFieldset, $buttonsFieldset = null, $as = 'searchform') |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Add arguments for the call to the PaginatorParams plugin. |
||
| 131 | * |
||
| 132 | * @see \Core\Controller\Plugin\PaginationParams::getParams() |
||
| 133 | * |
||
| 134 | * @param $namespace |
||
| 135 | * @param array $defaults |
||
| 136 | * |
||
| 137 | * @return self |
||
| 138 | */ |
||
| 139 | public function params($namespace, $defaults = [ 'page' => 1 ]) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Calls the stacked plugins in the right order and returns the result array. |
||
| 147 | * |
||
| 148 | * The returned array can directly be returned from the controller or be used to populate a |
||
| 149 | * view model. |
||
| 150 | * |
||
| 151 | * The search form plugin is only called (and thus the form only present in the result array) |
||
| 152 | * if the request is NOT an ajax request. (as the form is never rerendered on ajax requests) |
||
| 153 | * |
||
| 154 | * @param null|string $paginatorAlias Name of the paginator in the result array |
||
| 155 | * @param null|string $formAlias Name of the search form in the result array |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | public function getResult($paginatorAlias = null, $formAlias = null) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Calls an invokable controller plugin. |
||
| 194 | * |
||
| 195 | * @param string $name |
||
| 196 | * @param array $args |
||
| 197 | * |
||
| 198 | * @return mixed The return value of the called plugin. |
||
| 199 | */ |
||
| 200 | protected function callPlugin($name, $args) |
||
| 212 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.