Completed
Push — master ( 3e1348...65fa37 )
by Song
02:57
created

src/Show/Relation.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Show;
4
5
use Encore\Admin\Grid;
6
use Encore\Admin\Show;
7
use Illuminate\Contracts\Support\Renderable;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\BelongsTo;
10
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
11
use Illuminate\Database\Eloquent\Relations\HasMany;
12
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
13
use Illuminate\Database\Eloquent\Relations\HasOne;
14
use Illuminate\Database\Eloquent\Relations\MorphMany;
15
use Illuminate\Database\Eloquent\Relations\MorphOne;
16
17
class Relation extends Field
18
{
19
    /**
20
     * Relation name.
21
     *
22
     * @var string
23
     */
24
    protected $name;
25
26
    /**
27
     * Relation panel builder.
28
     *
29
     * @var callable
30
     */
31
    protected $builder;
32
33
    /**
34
     * Relation panel title.
35
     *
36
     * @var string
37
     */
38
    protected $title;
39
40
    /**
41
     * Parent model.
42
     *
43
     * @var Model
44
     */
45
    protected $model;
46
47
    /**
48
     * Relation constructor.
49
     *
50
     * @param string   $name
51
     * @param callable $builder
52
     * @param string   $title
53
     */
54
    public function __construct($name, $builder, $title = '')
55
    {
56
        $this->name = $name;
57
        $this->builder = $builder;
58
        $this->title = $this->formatLabel($title);
59
    }
60
61
    /**
62
     * Set parent model for relation.
63
     *
64
     * @param Model $model
65
     *
66
     * @return $this
67
     */
68
    public function setModel(Model $model)
69
    {
70
        $this->model = $model;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Get null renderable instance.
77
     *
78
     * @return Renderable|__anonymous@1539
79
     */
80
    protected function getNullRenderable()
81
    {
82
        return new class() implements Renderable {
83
            public function render()
84
            {
85
            }
86
        };
87
    }
88
89
    /**
90
     * Render this relation panel.
91
     *
92
     * @return string
93
     */
94
    public function render()
95
    {
96
        $relation = $this->model->{$this->name}();
97
98
        $renderable = $this->getNullRenderable();
99
100
        if ($relation    instanceof HasOne
101
            || $relation instanceof BelongsTo
102
            || $relation instanceof MorphOne
103
        ) {
104
            $model = $this->model->{$this->name};
105
106
            if (!$model instanceof Model) {
107
                $model = $relation->getRelated();
108
            }
109
110
            $renderable = new Show($model, $this->builder);
111
112
            $renderable->panel()->title($this->title);
113
        }
114
115
        if ($relation    instanceof HasMany
116
            || $relation instanceof MorphMany
117
            || $relation instanceof BelongsToMany
118
            || $relation instanceof HasManyThrough
119
        ) {
120
            $renderable = new Grid($relation->getRelated(), $this->builder);
0 ignored issues
show
$this->builder is of type callable, but the function expects a null|object<Closure>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
122
            $renderable->setName($this->name)
123
                ->setTitle($this->title)
124
                ->setRelation($relation);
125
        }
126
127
        return $renderable->render();
128
    }
129
}
130