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

EnumDetector::enumValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Field\Detectors;
4
5
use function admin\db\connection;
6
use function admin\db\enum_values;
7
use function admin\db\translated_values;
8
use Doctrine\DBAL\Schema\Column;
9
use Doctrine\DBAL\Types\StringType;
10
use Illuminate\Database\Eloquent\Model;
11
use Terranet\Administrator\Field\Enum;
12
use Terranet\Administrator\Field\Link;
13
14
class EnumDetector extends AbstractDetector
15
{
16
    /** @var array */
17
    protected $values = [];
18
19
    /**
20
     * @param string $column
21
     * @param Model $model
22
     * @return mixed
23
     */
24
    protected function enumValues(string $column, Model $model)
25
    {
26
        return $this->values = enum_values($model->getTable(), $column);
27
    }
28
29
    /**
30
     * Authorize execution.
31
     *
32
     * @param string $column
33
     * @param Column $metadata
34
     * @param Model $model
35
     *
36
     * @return bool
37
     */
38
    protected function authorize(string $column, Column $metadata, Model $model): bool
39
    {
40
        return connection('mysql') && !$metadata->getLength() && !empty($this->enumValues($column, $model));
0 ignored issues
show
Bug Best Practice introduced by
The expression $metadata->getLength() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
41
    }
42
43
    /**
44
     * Detect field class.
45
     *
46
     * @param string $column
47
     * @param Column $metadata
48
     * @param Model $model
49
     *
50
     * @return mixed
51
     */
52
    protected function detect(string $column, Column $metadata, Model $model)
53
    {
54
        $values = translated_values($this->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

54
        $values = translated_values($this->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...
55
56
        if (!$metadata->getNotNull()) {
57
            $values = ['' => '----'] + $values;
58
        }
59
60
        return Enum::make($column, $column)->setOptions($values);
61
    }
62
}