Passed
Pull Request — next (#160)
by Bas
04:08
created

IsAranguentRelation::getKeys()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 2
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Eloquent\Relations\Concerns;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Query\Expression;
9
10
trait IsAranguentRelation
11
{
12
    /**
13
     * Get all of the primary keys for an array of models.
14
     *
15
     * @param  array<mixed>  $models
16
     * @param  string|null  $key
17
     * @return array<int, int|string>
18
     */
19 9
    protected function getKeys(array $models, $key = null)
20
    {
21
        // The original function orders the results associatively by value which means the keys reorder too.
22
        // However, a list of keys with unordered numeric keys will be recognized as an object down the line
23
        // for json casting while we need a list of keys.
24
25 9
        $keys = collect($models)->map(function ($value) use ($key) {
0 ignored issues
show
Bug introduced by
$models of type array<mixed,mixed> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        $keys = collect(/** @scrutinizer ignore-type */ $models)->map(function ($value) use ($key) {
Loading history...
26 9
            return $key ? $value->getAttribute($key) : $value->getKey();
27 9
        })->values()->unique(null, true)->all();
28
29 9
        sort($keys);
30
31 9
        return $keys;
32
    }
33
34
    /**
35
     * Add the constraints for a relationship count query.
36
     *
37
     * @return Builder
38
     */
39 1
    public function getRelationExistenceCountQuery(Builder $query, Builder $parentQuery)
40
    {
41 1
        return $this->getRelationExistenceQuery(
0 ignored issues
show
Bug introduced by
The method getRelationExistenceQuery() does not exist on LaravelFreelancerNL\Aran...rns\IsAranguentRelation. Did you maybe mean getRelationExistenceCountQuery()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        return $this->/** @scrutinizer ignore-call */ getRelationExistenceQuery(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42 1
            $query,
43 1
            $parentQuery,
44 1
            new Expression('*'),
45 1
        );
46
    }
47
}
48