NullablePartition::formatAggregateResult()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMetrics\Metrics;
6
7
use Arcanedev\LaravelMetrics\Metrics\Concerns\HasExpressions;
8
use Arcanedev\LaravelMetrics\Results\PartitionResult;
9
use Illuminate\Support\Facades\DB;
10
11
/**
12
 * Class     NullablePartition
13
 *
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
abstract class NullablePartition extends Metric
17
{
18
    /* -----------------------------------------------------------------
19
     |  Traits
20
     | -----------------------------------------------------------------
21
     */
22
23
    use HasExpressions;
24
25
    /* -----------------------------------------------------------------
26
     |  Getters
27
     | -----------------------------------------------------------------
28
     */
29
30
    /**
31
     * Get the metric's type.
32
     *
33
     * @return string
34
     */
35 4
    public function type(): string
36
    {
37 4
        return 'partition';
38
    }
39
40
    /* -----------------------------------------------------------------
41
     |  Main Methods
42
     | -----------------------------------------------------------------
43
     */
44
45
    /**
46
     * Calculate the `count` of the metric.
47
     *
48
     * @param  \Illuminate\Database\Eloquent\Builder|string  $model
49
     * @param  string                                        $groupBy
50
     * @param  string|null                                   $column
51
     *
52
     * @return \Arcanedev\LaravelMetrics\Results\PartitionResult|mixed
53
     */
54 4
    protected function count($model, string $groupBy, $column = null)
55
    {
56 4
        return $this->aggregate('count', $model, $column, $groupBy);
57
    }
58
59
    /* -----------------------------------------------------------------
60
     |  Other Methods
61
     | -----------------------------------------------------------------
62
     */
63
64
    /**
65
     * Handle the aggregate calculation of the metric.
66
     *
67
     * @param  string                                        $method
68
     * @param  \Illuminate\Database\Eloquent\Builder|string  $model
69
     * @param  string                                        $column
70
     * @param  string                                        $groupBy
71
     *
72
     * @return \Arcanedev\LaravelMetrics\Results\PartitionResult|mixed
73
     */
74 4
    protected function aggregate(string $method, $model, ?string $column, string $groupBy)
75
    {
76 4
        $query         = static::getQuery($model);
77 4
        $wrappedColumn = $query->getQuery()->getGrammar()->wrap(
78 4
            $column ?? $query->getModel()->getQualifiedKeyName()
0 ignored issues
show
Bug introduced by
The method getQualifiedKeyName does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Eloquent\Builder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
79
        );
80
81 4
        $groupAlias = "{$groupBy}_count";
82 4
        $expression = static::getExpression($query, 'if_null', $groupBy);
83
84 4
        $value = $query->select([
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
85 4
            DB::raw("{$expression} as {$groupAlias}"),
86 4
            DB::raw("{$method}({$wrappedColumn}) as aggregate"),
87
        ])
88 4
            ->groupBy($groupAlias)
89 4
            ->get()
90 4
            ->mapWithKeys(function ($result) use ($groupAlias) {
91 4
                return $this->formatAggregateResult($result, $groupAlias);
92 4
            })
93 4
            ->all();
94
95 4
        return $this->result($value);
96
    }
97
98
    /**
99
     * Format the aggregate result for the partition.
100
     *
101
     * @param  \Illuminate\Database\Eloquent\Model  $result
102
     * @param  string                               $groupBy
103
     *
104
     * @return array
105
     */
106 4
    protected function formatAggregateResult($result, $groupBy): array
107
    {
108 4
        $key = $result->getAttribute(last(explode('.', $groupBy)));
109
110
        return [
111 4
            $key => $result->getAttribute('aggregate'),
112
        ];
113
    }
114
115
    /**
116
     * Make a new result instance.
117
     *
118
     * @param  mixed|null $value
119
     *
120
     * @return \Arcanedev\LaravelMetrics\Results\PartitionResult|mixed
121
     */
122 4
    protected function result($value = null)
123
    {
124 4
        return new PartitionResult($value);
125
    }
126
}
127