Test Failed
Push — master ( d27946...98f74e )
by Terzi
08:07
created

Grid::detectField()   C

Complexity

Conditions 15
Paths 16

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 29
nc 16
nop 1
dl 0
loc 46
rs 5.9166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Field\Boolean;
11
use Terranet\Administrator\Field\Email;
12
use Terranet\Administrator\Field\Enum;
13
use Terranet\Administrator\Field\File;
14
use Terranet\Administrator\Field\Generic;
15
use Terranet\Administrator\Field\Id;
16
use Terranet\Administrator\Field\Image;
17
use Terranet\Administrator\Field\Link;
18
use Terranet\Administrator\Field\Phone;
19
use Terranet\Administrator\Field\Rank;
20
use Terranet\Administrator\Field\Text;
21
use Terranet\Administrator\Field\Textarea;
22
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...
23
use Terranet\Translatable\Translatable;
24
25
class Grid
26
{
27
    /** @var Model */
28
    private $model;
29
30
    /**
31
     * Grid constructor.
32
     *
33
     * @param null|Model $model
34
     */
35
    public function __construct(Model $model = null)
36
    {
37
        $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

37
        $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...
38
    }
39
40
    /**
41
     * @param $element
42
     *
43
     * @return mixed|\Terranet\Administrator\Field\Generic
44
     */
45
    public function make($element)
46
    {
47
        // decorate attachment
48
        if ($this->model instanceof AttachableInterface
49
            && method_exists($this->model, 'getAttachedFiles')
50
            && 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 mixed and Illuminate\Database\Eloquent\Builder; 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

50
            && array_key_exists($element, /** @scrutinizer ignore-type */ $this->model->getAttachedFiles())
Loading history...
51
        ) {
52
            $file = $this->model->getAttachedFiles()[$element];
53
            if ($file->variants()) {
54
                return Image::make($element, $element);
55
            }
56
57
            return File::make($element, $element);
58
        }
59
60
        // decorate translatable attachment
61
        if ($this->model instanceof Translatable) {
62
            $translation = $this->model->getTranslationModel();
63
64
            if ($translation instanceof AttachableInterface
65
                && method_exists($translation, 'getAttachedFiles')
66
                && 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

66
                && array_key_exists($element, /** @scrutinizer ignore-type */ $translation->getAttachedFiles())
Loading history...
67
            ) {
68
                return Image::make($element);
69
            }
70
        }
71
72
        // decorate table column
73
        if ($this->realColum($element)) {
74
            $field = $this->detectField($element);
75
76
            if (is_object($field)) {
77
                return $field;
78
            }
79
80
            return forward_static_call_array([$field, 'make'], [$element, $element]);
81
        }
82
83
        return Text::make($element, $element);
84
    }
85
86
    /**
87
     * @param $column
88
     *
89
     * @return bool
90
     */
91
    protected function realColum($column)
92
    {
93
        return array_key_exists($column, $this->fetchTablesColumns());
94
    }
95
96
    /**
97
     * @param $column
98
     *
99
     * @return Generic
100
     */
101
    protected function detectField($column)
102
    {
103
        if ($column === $this->model->getKeyName()) {
104
            return Id::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Terranet\Administrator\Field\Id::class returns the type string which is incompatible with the documented return type Terranet\Administrator\Field\Generic.
Loading history...
105
        }
106
107
        $data = $this->fetchTablesColumns()[$column];
108
        $className = class_basename($data->getType());
109
110
        switch (true) {
111
            case $this->model instanceof Rankable && $column === $this->model->getRankableColumn():
112
                return Rank::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Terranet\Administrator\Field\Rank::class returns the type string which is incompatible with the documented return type Terranet\Administrator\Field\Generic.
Loading history...
113
            case in_array($className, ['TimeType', 'DateType', 'DateTimeType'], true):
114
                $type = str_replace('Type', '', $className);
115
116
                return "\\Terranet\\Administrator\\Field\\{$type}";
0 ignored issues
show
Bug Best Practice introduced by
The expression return '\\Terranet\\Administrator\\Field\\'.$type returns the type string which is incompatible with the documented return type Terranet\Administrator\Field\Generic.
Loading history...
117
            case 'BooleanType' === $className:
118
                return Boolean::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Terranet\Administrator\Field\Boolean::class returns the type string which is incompatible with the documented return type Terranet\Administrator\Field\Generic.
Loading history...
119
            case 'TextType' === $className:
120
                return Textarea::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Terranet\Administ...r\Field\Textarea::class returns the type string which is incompatible with the documented return type Terranet\Administrator\Field\Generic.
Loading history...
121
            case 'StringType' === $className:
122
                if (connection('mysql') && !$data->getLength()) {
123
                    if ($values = enum_values($this->model->getTable(), $column)) {
124
                        $values = translated_values($values, app('scaffold.module')->url(), $column);
0 ignored issues
show
Bug introduced by
The method url() 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

124
                        $values = translated_values($values, app('scaffold.module')->/** @scrutinizer ignore-call */ url(), $column);

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...
125
                        if (!$data->getNotNull()) {
126
                            $values = ['' => '----'] + $values;
127
                        }
128
129
                        return Enum::make($column, $column)->setOptions($values);
130
                    }
131
                }
132
133
                if (str_contains($column, 'email')) {
134
                    return Email::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Terranet\Administrator\Field\Email::class returns the type string which is incompatible with the documented return type Terranet\Administrator\Field\Generic.
Loading history...
135
                }
136
137
                if (str_contains($column, ['url', 'site', 'host'])) {
138
                    return Link::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Terranet\Administrator\Field\Link::class returns the type string which is incompatible with the documented return type Terranet\Administrator\Field\Generic.
Loading history...
139
                }
140
141
                if (str_contains($column, ['phone', 'gsm'])) {
142
                    return Phone::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Terranet\Administrator\Field\Phone::class returns the type string which is incompatible with the documented return type Terranet\Administrator\Field\Generic.
Loading history...
143
                }
144
            // no break
145
            default:
146
                return Text::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return Terranet\Administrator\Field\Text::class returns the type string which is incompatible with the documented return type Terranet\Administrator\Field\Generic.
Loading history...
147
        }
148
    }
149
150
    /**
151
     * @return array
152
     */
153
    protected function fetchTablesColumns()
154
    {
155
        return \admin\db\table_columns($this->model, true);
156
    }
157
}
158