Completed
Push — master ( 8f69e2...0f05a9 )
by ARCANEDEV
06:56
created

NullablePartition::type()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelMetrics\Metrics;
2
3
use Arcanedev\LaravelMetrics\Metrics\Concerns\HasExpressions;
4
use Arcanedev\LaravelMetrics\Results\PartitionResult;
5
use Illuminate\Support\Facades\DB;
6
7
/**
8
 * Class     NullablePartition
9
 *
10
 * @package  Arcanedev\LaravelMetrics\Metrics
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
abstract class NullablePartition extends Metric
14
{
15
    /* -----------------------------------------------------------------
16
     |  Traits
17
     | -----------------------------------------------------------------
18
     */
19
20
    use HasExpressions;
21
22
    /* -----------------------------------------------------------------
23
     |  Getters
24
     | -----------------------------------------------------------------
25
     */
26
27
    /**
28
     * Get the metric's type.
29
     *
30
     * @return string
31
     */
32 4
    public function type(): string
33
    {
34 4
        return 'partition';
35
    }
36
37
    /* -----------------------------------------------------------------
38
     |  Main Methods
39
     | -----------------------------------------------------------------
40
     */
41
42
    /**
43
     * Calculate the `count` of the metric.
44
     *
45
     * @param  \Illuminate\Database\Eloquent\Builder|string  $model
46
     * @param  string                                        $groupBy
47
     * @param  string|null                                   $column
48
     *
49
     * @return \Arcanedev\LaravelMetrics\Results\PartitionResult|mixed
50
     */
51 4
    protected function count(string $model, string $groupBy, $column = null)
52
    {
53 4
        return $this->aggregate('count', $model, $column, $groupBy);
54
    }
55
56
    /* -----------------------------------------------------------------
57
     |  Other Methods
58
     | -----------------------------------------------------------------
59
     */
60
61
    /**
62
     * Handle the aggregate calculation of the metric.
63
     *
64
     * @param  string                                        $method
65
     * @param  \Illuminate\Database\Eloquent\Builder|string  $model
66
     * @param  string                                        $column
67
     * @param  string                                        $groupBy
68
     *
69
     * @return \Arcanedev\LaravelMetrics\Results\PartitionResult|mixed
70
     */
71 4
    protected function aggregate(string $method, $model, ?string $column, string $groupBy)
72
    {
73 4
        $query         = static::getQuery($model);
74 4
        $wrappedColumn = $query->getQuery()->getGrammar()->wrap(
75 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...
76
        );
77
78 4
        $groupAlias = "{$groupBy}_count";
79
80 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...
81 4
            DB::raw("{$this->getExpression($query, 'if_null', $groupBy)} as {$groupAlias}"),
82 4
            DB::raw("{$method}({$wrappedColumn}) as aggregate"),
83
        ])
84 4
            ->groupBy($groupAlias)
85 4
            ->get()
86
            ->mapWithKeys(function ($result) use ($groupAlias) {
87 4
                return $this->formatAggregateResult($result, $groupAlias);
88 4
            })
89 4
            ->all();
90
91 4
        return $this->result($value);
92
    }
93
94
    /**
95
     * Format the aggregate result for the partition.
96
     *
97
     * @param  \Illuminate\Database\Eloquent\Model  $result
98
     * @param  string                               $groupBy
99
     *
100
     * @return array
101
     */
102 4
    protected function formatAggregateResult($result, $groupBy): array
103
    {
104 4
        $key = $result->getAttribute(last(explode('.', $groupBy)));
105
106
        return [
107 4
            $key => $result->getAttribute('aggregate'),
108
        ];
109
    }
110
111
    /**
112
     * Make a new result instance.
113
     *
114
     * @param  mixed|null $value
115
     *
116
     * @return \Arcanedev\LaravelMetrics\Results\PartitionResult|mixed
117
     */
118 4
    protected function result($value = null)
119
    {
120 4
        return new PartitionResult($value);
121
    }
122
}
123