Issues (33)

src/Relations/MorphTo.php (3 issues)

1
<?php
2
3
namespace Volosyuk\SimpleEloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\MorphTo as BaseMorphTo;
7
use Illuminate\Support\Collection;
8
use Volosyuk\SimpleEloquent\Builder;
9
use Volosyuk\SimpleEloquent\ModelAccessor;
10
11
/**
12
 * Class MorphToWithSimple
13
 * @package Volosyuk\SimpleEloquent
14
 */
15
class MorphTo extends BaseMorphTo
16
{
17
    use Relation;
18
19
    /**
20
     * Get the results of the relationship.
21
     *
22
     * Called via eager load method of Eloquent query builder.
23
     *
24
     * @return mixed
25
     */
26 1
    protected function getEagerSimple()
27
    {
28 1
        foreach (array_keys($this->dictionary) as $type) {
29 1
            $this->matchSimpleToMorphParents($type, $this->getSimpleResultsByType($type));
30
        }
31
32 1
        return $this->models;
33
    }
34
35
    /**
36
     * @param $models
37
     * @param $name
38
     * @return mixed
39
     */
40 1
    public function eagerLoadAndMatchSimple($models, $name)
0 ignored issues
show
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

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

40
    public function eagerLoadAndMatchSimple($models, /** @scrutinizer ignore-unused */ $name)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42 1
        $this->addEagerConstraintsSimple($models);
43
44 1
        return $this->getEagerSimple();
45
    }
46
47
    /**
48
     * Get all of the relation results for a type.
49
     *
50
     * @param  string  $type
51
     * @return Collection
52
     */
53 1
    protected function getSimpleResultsByType($type)
54
    {
55
        /**
56
         * @var Model $instance
57
         */
58 1
        $instance = $this->createModelByType($type);
59
60
        /**
61
         * @var Builder $query
62
         */
63 1
        $query = $this->replayMacros($instance->newQuery())
64 1
            ->mergeConstraintsFrom($this->getQuery())
65 1
            ->with($this->getQuery()->getEagerLoads());
66
67 1
        return $query->whereIn(
0 ignored issues
show
The method whereIn() does not exist on Volosyuk\SimpleEloquent\Builder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

67
        return $query->/** @scrutinizer ignore-call */ whereIn(
Loading history...
68 1
            $instance->getTable().'.'.$instance->getKeyName(), $this->gatherKeysByType($type, $instance->getKeyType())
69 1
        )->getSimple();
70
    }
71
72
    /**
73
     * Match the results for a given type to their parents.
74
     *
75
     * @param  string  $type
76
     * @param  Collection  $results
77
     * @return void
78
     */
79 1
    protected function matchSimpleToMorphParents($type, Collection $results)
80
    {
81 1
        foreach ($results as $result) {
82 1
            foreach ($this->models as &$model) {
83
                if (
84 1
                    ModelAccessor::get($model, $this->morphType) == $type
85
                    &&
86 1
                    ModelAccessor::get($model, $this->foreignKey) == ModelAccessor::get($result, $this->parent->getKeyName())
87
                ) {
88 1
                    ModelAccessor::set($model, $this->relationName, $result);
89
                }
90
            }
91 1
            unset($model);
92
        }
93 1
    }
94
95
96
    /**
97
     * Set the constraints for an eager load of the relation.
98
     *
99
     * @param  array  $models
100
     * @return void
101
     */
102 1
    public function addEagerConstraintsSimple(array $models)
103
    {
104 1
        $this->buildDictionarySimple($this->models = $models);
0 ignored issues
show
Documentation Bug introduced by
It seems like $models of type array is incompatible with the declared type Illuminate\Database\Eloquent\Collection of property $models.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105 1
    }
106
107
108
    /**
109
     * Build a dictionary with the models.
110
     *
111
     * @param  array  $models
112
     * @return void
113
     */
114 1
    protected function buildDictionarySimple(array $models)
115
    {
116 1
        foreach ($models as $model) {
117 1
            if (ModelAccessor::get($model, $this->morphType)) {
118 1
                $this->dictionary[ModelAccessor::get($model, $this->morphType)][ModelAccessor::get($model, $this->foreignKey)][] = $model;
119
            }
120
        }
121 1
    }
122
}
123

 

OSZAR »