Test Failed
Push — master ( b480ce...8fe18f )
by Terzi
03:48
created

Grid::realColum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Terranet\Administrator\Decorators;
4
5
use function admin\db\connection;
6
use function admin\db\enum_values;
7
use function admin\db\translated_values;
8
use Czim\Paperclip\Contracts\AttachableInterface;
9
use Illuminate\Database\Eloquent\Model;
10
use Terranet\Administrator\Chain;
11
use Terranet\Administrator\Field\Boolean;
12
use Terranet\Administrator\Field\Detectors\BooleanDetector;
13
use Terranet\Administrator\Field\Detectors\DateTimeDetector;
14
use Terranet\Administrator\Field\Detectors\EmailDetector;
15
use Terranet\Administrator\Field\Detectors\EnumDetector;
16
use Terranet\Administrator\Field\Detectors\LinkDetector;
17
use Terranet\Administrator\Field\Detectors\PasswordDetector;
18
use Terranet\Administrator\Field\Detectors\PhoneDetector;
19
use Terranet\Administrator\Field\Detectors\PrimaryKeyDetector;
20
use Terranet\Administrator\Field\Detectors\RankableDetector;
21
use Terranet\Administrator\Field\Detectors\TextareaDetector;
22
use Terranet\Administrator\Field\Detectors\TextDetector;
23
use Terranet\Administrator\Field\Email;
24
use Terranet\Administrator\Field\Enum;
25
use Terranet\Administrator\Field\File;
26
use Terranet\Administrator\Field\Generic;
27
use Terranet\Administrator\Field\Id;
28
use Terranet\Administrator\Field\Image;
29
use Terranet\Administrator\Field\Link;
30
use Terranet\Administrator\Field\Password;
31
use Terranet\Administrator\Field\Phone;
32
use Terranet\Administrator\Field\Rank;
33
use Terranet\Administrator\Field\Text;
34
use Terranet\Administrator\Field\Textarea;
35
use Terranet\Rankable\Rankable;
0 ignored issues
show
Bug introduced by
The type Terranet\Rankable\Rankable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
use Terranet\Translatable\Translatable;
37
38
class Grid
39
{
40
    /** @var Model */
41
    private $model;
42
43
    /**
44
     * Grid constructor.
45
     *
46
     * @param null|Model $model
47
     */
48
    public function __construct(Model $model = null)
49
    {
50
        $this->model = $model ?: app('scaffold.module')->model();
0 ignored issues
show
Bug introduced by
The method model() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

50
        $this->model = $model ?: app('scaffold.module')->/** @scrutinizer ignore-call */ model();

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...
51
    }
52
53
    /**
54
     * @param $element
55
     *
56
     * @return mixed|\Terranet\Administrator\Field\Generic
57
     */
58
    public function make($element)
59
    {
60
        // decorate attachment
61
        if ($this->model instanceof AttachableInterface
62
            && method_exists($this->model, 'getAttachedFiles')
63
            && array_key_exists($element, $this->model->getAttachedFiles())
0 ignored issues
show
Bug introduced by
It seems like $this->model->getAttachedFiles() can also be of type Illuminate\Database\Eloquent\Builder and mixed; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

63
            && array_key_exists($element, /** @scrutinizer ignore-type */ $this->model->getAttachedFiles())
Loading history...
64
        ) {
65
            $file = $this->model->getAttachedFiles()[$element];
66
            if ($file->variants()) {
67
                return Image::make($element, $element);
68
            }
69
70
            return File::make($element, $element);
71
        }
72
73
        // decorate translatable attachment
74
        if ($this->model instanceof Translatable) {
75
            $translation = $this->model->getTranslationModel();
76
77
            if ($translation instanceof AttachableInterface
78
                && method_exists($translation, 'getAttachedFiles')
79
                && array_key_exists($element, $translation->getAttachedFiles())
0 ignored issues
show
Bug introduced by
It seems like $translation->getAttachedFiles() can also be of type Czim\Paperclip\Contracts...achableInterface&object and Illuminate\Database\Eloq...achableInterface&object; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

79
                && array_key_exists($element, /** @scrutinizer ignore-type */ $translation->getAttachedFiles())
Loading history...
80
            ) {
81
                return Image::make($element);
82
            }
83
        }
84
85
        // decorate table column
86
        if ($this->realColum($element)) {
87
            $field = $this->detectField($element);
88
89
            if (is_object($field)) {
90
                return $field;
91
            }
92
93
            return forward_static_call_array([$field, 'make'], [$element, $element]);
94
        }
95
96
        return Text::make($element, $element);
97
    }
98
99
    /**
100
     * @param $column
101
     *
102
     * @return bool
103
     */
104
    protected function realColum($column)
105
    {
106
        return array_key_exists($column, $this->fetchTablesColumns());
107
    }
108
109
    /**
110
     * @param $column
111
     *
112
     * @return Generic
113
     */
114
    protected function detectField($column)
115
    {
116
        return Chain::make([
117
            new PrimaryKeyDetector(),
118
            new RankableDetector(),
119
            new DateTimeDetector(),
120
            new BooleanDetector(),
121
            new TextareaDetector(),
122
            new EnumDetector(),
123
            new PasswordDetector(),
124
            new EmailDetector(),
125
            new LinkDetector(),
126
            new PhoneDetector(),
127
            new TextDetector(),
128
        ])($column, $this->fetchTablesColumns()[$column], $this->model);
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    protected function fetchTablesColumns()
135
    {
136
        return \admin\db\table_columns($this->model, true);
137
    }
138
}
139