Completed
Push — develop ( 0b876c...311b68 )
by Freddie
03:14
created

SchemaAttributeLogicValidation::validate()   D

Complexity

Conditions 23
Paths 33

Size

Total Lines 54
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 23

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 23
eloc 27
c 1
b 0
f 0
nc 33
nop 0
dl 0
loc 54
rs 4.1666
ccs 28
cts 28
cp 1
crap 23

How to fix   Long Method    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 declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Schema\Validations;
11
12
use FlexPHP\Schema\Exception\InvalidSchemaAttributeException;
13
use FlexPHP\Schema\SchemaAttributeInterface;
14
15
class SchemaAttributeLogicValidation implements ValidationInterface
16
{
17
    /**
18
     * @var SchemaAttributeInterface
19
     */
20
    private $property;
21
22 114
    public function __construct(SchemaAttributeInterface $property)
23
    {
24 114
        $this->property = $property;
25 114
    }
26
27 114
    public function validate(): void
28
    {
29 114
        if (empty($this->property->constraints())) {
30 19
            return;
31
        }
32
33 97
        $name = 'Logic: [' . $this->property->name() . '] ';
34
35 97
        if ($this->property->isPk()) {
36 20
            if (!$this->property->isRequired()) {
37 3
                throw new InvalidSchemaAttributeException($name . 'Primary Key must be required.');
38
            }
39
40 17
            if ($this->property->isFk()) {
41 2
                throw new InvalidSchemaAttributeException($name . 'Primary Key cannot be Foreing Key too.');
42
            }
43
44 15
            if ($this->isInt() && !$this->property->isAi()) {
45 1
                throw new InvalidSchemaAttributeException($name . 'Primary Key numeric not autoincrement.');
46
            }
47
48 14
            if ($this->property->isAi() && $this->hasSizingConstraint()) {
49 1
                throw new InvalidSchemaAttributeException($name . 'Primary Key autoincrement cannot has sizing.');
50
            }
51
        }
52
53 90
        if ($this->property->isAi()) {
54 10
            if (!$this->property->isPk()) {
55 1
                throw new InvalidSchemaAttributeException($name . 'Autoincrement must be Primary Key too.');
56
            }
57
58 9
            if (!$this->isInt()) {
59 1
                throw new InvalidSchemaAttributeException($name . 'Autoincrement must be numeric.');
60
            }
61
        }
62
63 88
        if ($this->property->isBlame() && !$this->isDate()) {
64 2
            throw new InvalidSchemaAttributeException($name . 'Blame property must be date datetype valid.');
65
        }
66
67 86
        if ($this->property->isCa() && $this->property->isUa()) {
68 1
            throw new InvalidSchemaAttributeException($name . 'Created and Updated At in same property is not valid.');
69
        }
70
71 85
        if ($this->isNumeric() && $this->hasLength()) {
72 10
            throw new InvalidSchemaAttributeException($name . 'Numeric properties use: min, max.');
73
        }
74
75 75
        if ($this->isString() && $this->hasSize()) {
76 4
            throw new InvalidSchemaAttributeException($name . 'String properties use: minlength, maxlength.');
77
        }
78
79 71
        if (($this->isDate() || $this->isBinary()) && $this->hasSizingConstraint()) {
80 4
            throw new InvalidSchemaAttributeException($name . 'Date, bool, blob properties not use min, max, etc');
81
        }
82 67
    }
83
84 15
    private function isInt(): bool
85
    {
86 15
        return \in_array($this->property->dataType(), ['smallint', 'integer', 'bigint']);
87
    }
88
89 74
    private function isDate(): bool
90
    {
91 74
        return \strpos($this->property->typeHint(), '\Date') !== false;
92
    }
93
94 85
    private function isNumeric(): bool
95
    {
96 85
        return \in_array($this->property->dataType(), ['smallint', 'integer', 'bigint', 'double', 'float']);
97
    }
98
99 75
    private function isString(): bool
100
    {
101 75
        return $this->property->dataType() !== 'bigint' && $this->property->typeHint() === 'string';
102
    }
103
104 55
    private function isBinary(): bool
105
    {
106 55
        return \in_array($this->property->dataType(), ['bool', 'blob']);
107
    }
108
109 57
    private function hasLength(): bool
110
    {
111 57
        return $this->property->minLength() !== null || $this->property->maxLength() !== null;
112
    }
113
114 57
    private function hasSize(): bool
115
    {
116 57
        return $this->property->min() !== null || $this->property->max() !== null;
117
    }
118
119 24
    private function hasCheck(): bool
120
    {
121 24
        return $this->property->minCheck() !== null || $this->property->maxCheck() !== null;
122
    }
123
124 27
    private function hasSizingConstraint(): bool
125
    {
126 27
        return $this->hasSize() || $this->hasLength() || $this->hasCheck();
127
    }
128
}
129