| Conditions | 24 |
| Paths | 3464 |
| Total Lines | 120 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 1 | Features | 2 |
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 |
||
| 61 | public function show($collection, $interval = 'count', $timeframe = null, $filters = null, $groupBy = null) |
||
| 62 | { |
||
| 63 | $start = $timeframe ? Arr::get($timeframe, 'start') : null; |
||
| 64 | $end = $timeframe ? Arr::get($timeframe, 'end') : null; |
||
| 65 | $matchArray = []; |
||
| 66 | $filters = valid_json($filters) ? json_decode($filters) : $filters; |
||
| 67 | $intervalFormat = '%Y-%m-%dT%H'; |
||
| 68 | $aggregate = []; |
||
| 69 | $model = $this->namespace.Str::studly(Str::singular($collection)).'Analytic'; |
||
| 70 | |||
| 71 | if (! class_exists($model)) { |
||
| 72 | throw new InvalidArgumentException("Model {$model} does not exist."); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($filters) { |
||
| 76 | foreach ($filters as $filter) { |
||
| 77 | if (is_array($filter)) { |
||
| 78 | $matchArray = array_merge($matchArray, $filter); |
||
| 79 | } else { |
||
| 80 | $propertyValue = $filter->property_value; |
||
| 81 | |||
| 82 | if (is_numeric($propertyValue)) { |
||
| 83 | $propertyValue = (int) $propertyValue; |
||
| 84 | } |
||
| 85 | |||
| 86 | $matchArray = array_merge($matchArray, [$filter->property_name => $propertyValue]); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | abort_unless(auth()->check() && $this->canViewAnalytic($model, $matchArray, auth()->user()), 403, 'You dont have permission to view these analytics'); |
||
| 92 | |||
| 93 | if ($start) { |
||
| 94 | $matchArray['created_at']['$gte'] = mongoTime($start); |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($end) { |
||
| 98 | $matchArray['created_at']['$lt'] = mongoTime($end); |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($matchArray) { |
||
| 102 | $aggregate[] = ['$match' => $matchArray]; |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($interval != 'count') { |
||
| 106 | if ($interval == 'daily') { |
||
| 107 | $intervalFormat = '%Y-%m-%d'; |
||
| 108 | } |
||
| 109 | |||
| 110 | if ($interval == 'weekly') { |
||
| 111 | $intervalFormat = '%Y-%U'; |
||
| 112 | } |
||
| 113 | |||
| 114 | if ($interval == 'monthly' || $interval == 'growth') { |
||
| 115 | $intervalFormat = '%Y-%m'; |
||
| 116 | } |
||
| 117 | |||
| 118 | $aggregate[] = [ |
||
| 119 | '$group' => [ |
||
| 120 | '_id' => [ |
||
| 121 | '$dateToString' => ['date' => '$created_at', 'format' => $intervalFormat], |
||
| 122 | ], |
||
| 123 | 'count' => [ |
||
| 124 | '$sum' => 1, |
||
| 125 | ], |
||
| 126 | 'created_at' => [ |
||
| 127 | '$last' => '$created_at', |
||
| 128 | ], |
||
| 129 | ], |
||
| 130 | ]; |
||
| 131 | |||
| 132 | $aggregate[] = ['$sort' => ['created_at' => 1]]; |
||
| 133 | |||
| 134 | $aggregate[] = [ |
||
| 135 | '$project' => [ |
||
| 136 | '_id' => 0, |
||
| 137 | 'created_at' => 1, |
||
| 138 | 'count' => 1, |
||
| 139 | ], |
||
| 140 | ]; |
||
| 141 | } |
||
| 142 | |||
| 143 | if ($interval == 'count' && $groupBy != null) { |
||
| 144 | $nested = Str::contains($groupBy, '.'); |
||
| 145 | |||
| 146 | if ($nested) { |
||
| 147 | $aggregate[] = ['$unwind' => '$'.Str::before($groupBy, '.')]; |
||
| 148 | } |
||
| 149 | |||
| 150 | $aggregate[] = [ |
||
| 151 | '$group' => [ |
||
| 152 | '_id' => '$'.$groupBy, |
||
| 153 | 'count' => [ |
||
| 154 | '$sum' => 1, |
||
| 155 | ], |
||
| 156 | ], |
||
| 157 | ]; |
||
| 158 | |||
| 159 | $aggregate[] = ['$sort' => ['count' => 1]]; |
||
| 160 | |||
| 161 | $aggregate[] = [ |
||
| 162 | '$project' => [ |
||
| 163 | '_id' => 0, |
||
| 164 | Str::after($groupBy, '.') => '$_id', |
||
| 165 | 'count' => 1, |
||
| 166 | ], |
||
| 167 | ]; |
||
| 168 | } |
||
| 169 | |||
| 170 | $data = $model::raw(function ($collection) use ($matchArray, $interval, $aggregate, $groupBy) { |
||
| 171 | if ($interval == 'count' && ! $groupBy) { |
||
| 172 | return $collection->count($matchArray); |
||
| 173 | } |
||
| 174 | |||
| 175 | if ($aggregate) { |
||
| 176 | return $collection->aggregate($aggregate, ['allowDiskUse' => true]); |
||
| 177 | } |
||
| 178 | }); |
||
| 179 | |||
| 180 | return $data; |
||
| 181 | } |
||
| 260 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths