Completed
Push — develop ( c8e703...0b876c )
by Freddie
03:17
created

SchemaAttributeLogicValidation   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 97.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 108
ccs 47
cts 48
cp 0.9792
rs 9.0399
wmc 42

11 Methods

Rating   Name   Duplication   Size   Complexity  
A hasSize() 0 3 2
A isInt() 0 3 1
A isBinary() 0 3 1
A isString() 0 3 2
A isNumeric() 0 3 1
A hasCheck() 0 3 2
A hasLength() 0 3 2
A isDate() 0 3 1
A hasSizingConstraint() 0 3 3
D validate() 0 50 26
A __construct() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like SchemaAttributeLogicValidation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SchemaAttributeLogicValidation, and based on these observations, apply Extract Interface, too.

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 113
    public function __construct(SchemaAttributeInterface $property)
23
    {
24 113
        $this->property = $property;
25 113
    }
26
27 113
    public function validate(): void
28
    {
29 113
        if (empty($this->property->constraints())) {
30 19
            return;
31
        }
32
33 96
        $name = 'Logic: [' . $this->property->name() . '] ';
34
35 96
        if ($this->property->isPk() && !$this->property->isRequired()) {
36 3
            throw new InvalidSchemaAttributeException($name . 'Primary Key must be required.');
37
        }
38
39 93
        if ($this->property->isAi() && !$this->property->isPk()) {
40 1
            throw new InvalidSchemaAttributeException($name . 'Autoincrement must be Primary Key too.');
41
        }
42
43 92
        if (!$this->property->isAi() && $this->property->isPk() && $this->isInt()) {
44 1
            throw new InvalidSchemaAttributeException($name . 'Primary Key numeric not autoincrement.');
45
        }
46
47 91
        if ($this->property->isAi() && !$this->isInt()) {
48 1
            throw new InvalidSchemaAttributeException($name . 'Autoincrement must be a numeric datatype.');
49
        }
50
51 90
        if ($this->property->isPk() && $this->property->isFk()) {
52 2
            throw new InvalidSchemaAttributeException($name . 'Primary Key cannot be Foreing Key too.');
53
        }
54
55 88
        if ($this->property->isAi() && $this->property->isFk()) {
56
            throw new InvalidSchemaAttributeException($name . 'Foreign Key cannot be autoincrement.');
57
        }
58
59 88
        if ($this->property->isBlame() && !$this->isDate()) {
60 2
            throw new InvalidSchemaAttributeException($name . 'Blame property must be date datetype valid.');
61
        }
62
63 86
        if ($this->property->isCa() && $this->property->isUa()) {
64 1
            throw new InvalidSchemaAttributeException($name . 'Created and Updated At in same property is not valid.');
65
        }
66
67 85
        if ($this->isNumeric() && $this->hasLength()) {
68 10
            throw new InvalidSchemaAttributeException($name . 'Numeric properties use: min, max.');
69
        }
70
71 75
        if ($this->isString() && $this->hasSize()) {
72 4
            throw new InvalidSchemaAttributeException($name . 'String properties use: minlength, maxlength.');
73
        }
74
75 71
        if (($this->isDate() || $this->isBinary()) && $this->hasSizingConstraint()) {
76 4
            throw new InvalidSchemaAttributeException($name . 'Date, bool, blob properties not use min, max, etc');
77
        }
78 67
    }
79
80 16
    private function isInt(): bool
81
    {
82 16
        return \in_array($this->property->dataType(), ['smallint', 'integer', 'bigint']);
83
    }
84
85 74
    private function isDate(): bool
86
    {
87 74
        return \strpos($this->property->typeHint(), '\Date') !== false;
88
    }
89
90 85
    private function isNumeric(): bool
91
    {
92 85
        return \in_array($this->property->dataType(), ['smallint', 'integer', 'bigint', 'double', 'float']);
93
    }
94
95 75
    private function isString(): bool
96
    {
97 75
        return $this->property->dataType() !== 'bigint' && $this->property->typeHint() === 'string';
98
    }
99
100 55
    private function isBinary(): bool
101
    {
102 55
        return \in_array($this->property->dataType(), ['bool', 'blob']);
103
    }
104
105 56
    private function hasLength(): bool
106
    {
107 56
        return $this->property->minLength() !== null || $this->property->maxLength() !== null;
108
    }
109
110 50
    private function hasSize(): bool
111
    {
112 50
        return $this->property->min() !== null || $this->property->max() !== null;
113
    }
114
115 15
    private function hasCheck(): bool
116
    {
117 15
        return $this->property->minCheck() !== null || $this->property->maxCheck() !== null;
118
    }
119
120 17
    private function hasSizingConstraint(): bool
121
    {
122 17
        return $this->hasSize() || $this->hasLength() || $this->hasCheck();
123
    }
124
}
125