Completed
Push — master ( b9cb31...17909b )
by
unknown
14s queued 11s
created

TranslationRelation::match()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 3
nop 3
1
<?php
2
3
namespace BBSLab\NovaTranslation\Models;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\Relation;
8
use Illuminate\Database\Query\Builder;
9
use Illuminate\Support\Facades\DB;
10
11
class TranslationRelation extends Relation
12
{
13
    /**
14
     * @var \BBSLab\NovaTranslation\Models\Translation|\Illuminate\Database\Eloquent\Builder
15
     */
16
    protected $query;
17
18
    /**
19
     * @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable
20
     */
21
    protected $parent;
22
23
    /**
24
     * @var \BBSLab\NovaTranslation\Models\Translation
25
     */
26
    protected $related;
27
28
    public function __construct(Model $parent)
29
    {
30
        parent::__construct(Translation::query(), $parent);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function addConstraints()
37
    {
38
        if (static::$constraints) {
39
            $this->query
40 View Code Duplication
                ->whereExists(function (Builder $query) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
                    $query->select(DB::raw(1))
42
                        ->from('translations as original')
43
                        ->whereRaw('original.translation_id = translations.translation_id')
44
                        ->where('original.translatable_id', '=', $this->parent->getKey())
45
                        ->where('original.translatable_type', '=', get_class($this->parent));
46
                })
47
                ->where('translatable_type', '=', get_class($this->parent))
48
                ->where('translatable_id', '<>', $this->parent->getKey());
49
        }
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function addEagerConstraints(array $models)
56
    {
57
        $ids = collect($models)->pluck($this->parent->getKeyName());
58
59
        $this->query
60 View Code Duplication
            ->whereExists(function (Builder $query) use ($ids) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
                $query->select(DB::raw(1))
62
                    ->from('translations as original')
63
                    ->whereRaw('translations.translation_id = original.translation_id')
64
                    ->whereIn('original.translatable_id', $ids)
65
                    ->where('original.translatable_type', '=', get_class($this->parent));
66
            })
67
            ->where('translatable_type', '=', get_class($this->parent))
68
            ->whereNotIn('translatable_id', $ids);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function initRelation(array $models, $relation)
75
    {
76
        foreach ($models as $model) {
77
            /** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */
78
            $model->setRelation($relation, $this->related->newCollection());
79
        }
80
81
        return $models;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function match(array $models, Collection $results, $relation)
88
    {
89
        if ($results->isEmpty()) {
90
            return $models;
91
        }
92
93
        foreach ($models as $model) {
94
            /** @var \BBSLab\NovaTranslation\Models\Contracts\IsTranslatable $model */
95
            $model->setRelation(
96
                $relation,
97
                $results->filter(function (Translation $translation) use ($model) {
98
                    return $translation->translation_id === $model->translation->translation_id
0 ignored issues
show
Bug introduced by
Accessing translation on the interface BBSLab\NovaTranslation\M...ontracts\IsTranslatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
99
                        && $translation->translatable_id !== $model->getKey();
100
                })
101
            );
102
        }
103
104
        return $models;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getResults()
111
    {
112
        return ! is_null($this->getParent()->getKey())
113
            ? $this->query->get()
0 ignored issues
show
Bug introduced by
The method get does only exist in Illuminate\Database\Eloquent\Builder, but not in BBSLab\NovaTranslation\Models\Translation.

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...
114
            : $this->related->newCollection();
115
    }
116
}
117