Completed
Push — master ( bfc41a...343cdb )
by wen
12:33
created

Input   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 19
c 1
b 1
f 0
lcom 1
cbo 2
dl 0
loc 119
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getSize() 0 4 1
A setSize() 0 6 1
A mediumSize() 0 4 1
A smallSize() 0 4 1
A miniSize() 0 4 1
A getModelFieldLength() 0 8 3
A getModelColumn() 0 16 3
A getMaxLength() 0 8 2
A setMaxLength() 0 7 1
A getMinLength() 0 4 1
A setMinLength() 0 9 1
A isReadonly() 0 4 1
A readonly() 0 6 1
A toArray() 0 9 1
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use DB;
6
use Doctrine\DBAL\Schema\Column;
7
use Doctrine\DBAL\Types\Type;
8
9
class Input extends NamedElement
10
{
11
    protected $maxLength;
12
    protected $minLength = 0;
13
14
    protected $size = '';
15
16
    protected $readonly = false;
17
18
    public function getSize()
19
    {
20
        return $this->size;
21
    }
22
23
    public function setSize($value)
24
    {
25
        $this->size = $value;
26
27
        return $this;
28
    }
29
30
    public function mediumSize()
31
    {
32
        return $this->setSize('medium');
33
    }
34
35
    public function smallSize()
36
    {
37
        return $this->setSize('small');
38
    }
39
40
    public function miniSize()
41
    {
42
        return $this->setSize('mini');
43
    }
44
45
    protected function getModelFieldLength()
46
    {
47
        $column = $this->getModelColumn();
48
        if ($column instanceof Column && $column->getType()->getName() == Type::STRING) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Schema\Column does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
49
            return $column->getLength();
50
        }
51
        return;
52
    }
53
54
    /**
55
     * @return \Doctrine\DBAL\Schema\Column
56
     */
57
    protected function getModelColumn()
58
    {
59
        // Doctrine\DBAL\Platforms\MySQL57Platform not support "enum" "string"
60
        $schema = DB::getDoctrineSchemaManager();
61
        $databasePlatform = $schema->getDatabasePlatform();
62
        $databasePlatform->registerDoctrineTypeMapping('enum', 'string');
63
64
        $table  = DB::getTablePrefix() . $this->getModel()->getTable();
65
        $column = $this->getName();
66
        if ($column) {
67
            $columns = $schema->listTableDetails($table);
68
            if ($columns->hasColumn($column)) {
69
                return $columns->getColumn($column);
70
            }
71
        }
72
    }
73
74
    public function getMaxLength()
75
    {
76
        if ($this->maxLength) {
77
            return $this->maxLength;
78
        }
79
80
        return $this->getModelFieldLength();
81
    }
82
83
    public function setMaxLength($value)
84
    {
85
        $value           = intval($value);
86
        $this->maxLength = $value;
87
        $this->addValidationRule('max:' . $value);
88
        return $this;
89
    }
90
91
    public function getMinLength()
92
    {
93
        return $this->minLength;
94
    }
95
96
    public function setMinLength($value)
97
    {
98
        $value           = intval($value);
99
        $this->minLength = $value;
100
101
        $this->addValidationRule('min:' . $value);
102
103
        return $this;
104
    }
105
106
    public function isReadonly()
107
    {
108
        return $this->readonly;
109
    }
110
111
    public function readonly()
112
    {
113
        $this->readonly = true;
114
115
        return $this;
116
    }
117
118
    public function toArray()
119
    {
120
        return parent::toArray() + [
121
                'minLength' => $this->getMinLength(),
122
                'maxLength' => $this->getMaxLength(),
123
                'size'      => $this->getSize(),
124
                'readonly'  => $this->isReadonly(),
125
            ];
126
    }
127
}
128