| Conditions | 24 |
| Paths | 4 |
| Total Lines | 92 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 62 |
| CRAP Score | 24.0173 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 47 | 13 | public function process(array $settings = []) |
|
| 48 | { |
||
| 49 | 13 | $data = $this->source; |
|
| 50 | |||
| 51 | // process filters |
||
| 52 | 13 | if (!empty($settings['filters'])) { |
|
| 53 | 5 | $data = array_filter( |
|
| 54 | 5 | $data, |
|
| 55 | 5 | function ($row) use ($settings) { |
|
| 56 | 5 | foreach ($settings['filters'] as $column => $filters) { |
|
| 57 | 5 | foreach ($filters as $filter => $value) { |
|
| 58 | // switch statement for filter |
||
| 59 | switch ($filter) { |
||
| 60 | 5 | case Grid\Grid::FILTER_EQ: |
|
| 61 | 2 | if ($row[$column] != $value) { |
|
| 62 | 2 | return false; |
|
| 63 | } |
||
| 64 | 2 | break; |
|
| 65 | 4 | case Grid\Grid::FILTER_NE: |
|
| 66 | 2 | if ($row[$column] == $value) { |
|
| 67 | 1 | return false; |
|
| 68 | } |
||
| 69 | 2 | break; |
|
| 70 | 2 | case Grid\Grid::FILTER_GT: |
|
| 71 | 1 | if ($row[$column] <= $value) { |
|
| 72 | 1 | return false; |
|
| 73 | } |
||
| 74 | 1 | break; |
|
| 75 | 2 | case Grid\Grid::FILTER_GE: |
|
| 76 | 1 | if ($row[$column] < $value) { |
|
| 77 | return false; |
||
| 78 | } |
||
| 79 | 1 | break; |
|
| 80 | 2 | case Grid\Grid::FILTER_LT: |
|
| 81 | 1 | if ($row[$column] >= $value) { |
|
| 82 | 1 | return false; |
|
| 83 | } |
||
| 84 | 1 | break; |
|
| 85 | 2 | case Grid\Grid::FILTER_LE: |
|
| 86 | 1 | if ($row[$column] > $value) { |
|
| 87 | return false; |
||
| 88 | } |
||
| 89 | 1 | break; |
|
| 90 | 1 | case Grid\Grid::FILTER_LIKE: |
|
| 91 | 1 | if (!preg_match('/'.$value.'/', $row[$column])) { |
|
| 92 | 1 | return false; |
|
| 93 | } |
||
| 94 | 1 | break; |
|
| 95 | } |
||
| 96 | 5 | } |
|
| 97 | 5 | } |
|
| 98 | 5 | return true; |
|
| 99 | } |
||
| 100 | 5 | ); |
|
| 101 | 5 | } |
|
| 102 | |||
| 103 | // process orders |
||
| 104 | 13 | if (!empty($settings['orders'])) { |
|
| 105 | // Create empty column stack |
||
| 106 | 2 | $orders = []; |
|
| 107 | 2 | foreach ($settings['orders'] as $column => $order) { |
|
| 108 | 2 | $orders[$column] = []; |
|
| 109 | 2 | } |
|
| 110 | |||
| 111 | // Obtain a list of columns |
||
| 112 | 2 | foreach ($data as $key => $row) { |
|
| 113 | 2 | foreach ($settings['orders'] as $column => $order) { |
|
| 114 | 2 | $orders[$column][$key] = $row[$column]; |
|
| 115 | 2 | } |
|
| 116 | 2 | } |
|
| 117 | // Prepare array of arguments |
||
| 118 | 2 | $funcArgs = []; |
|
| 119 | 2 | foreach ($settings['orders'] as $column => $order) { |
|
| 120 | 2 | $funcArgs[] = $orders[$column]; |
|
| 121 | 2 | $funcArgs[] = ($order == Grid\Grid::ORDER_ASC) ? SORT_ASC : SORT_DESC; |
|
| 122 | 2 | } |
|
| 123 | 2 | $funcArgs[] = & $data; |
|
| 124 | |||
| 125 | // Sort the data with volume descending, edition ascending |
||
| 126 | // Add $data as the last parameter, to sort by the common key |
||
| 127 | 2 | array_multisort(...$funcArgs); |
|
| 128 | 2 | } |
|
| 129 | |||
| 130 | 13 | $total = sizeof($data); |
|
| 131 | |||
| 132 | // process pages |
||
| 133 | 13 | $data = array_slice($data, ($settings['limit'] * ($settings['page'] - 1)), $settings['limit']); |
|
| 134 | |||
| 135 | 13 | $gridData = new Grid\Data($data); |
|
| 136 | 13 | $gridData->setTotal($total); |
|
| 137 | 13 | return $gridData; |
|
| 138 | } |
||
| 139 | } |
||
| 140 |