FormatLogicValidator::validate()   B
last analyzed

Complexity

Conditions 11
Paths 9

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11.1043

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 20
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 36
ccs 19
cts 21
cp 0.9048
crap 11.1043
rs 7.3166

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 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\Validators\Logics;
11
12
use FlexPHP\Schema\Constants\Format;
13
use FlexPHP\Schema\Exception\InvalidSchemaAttributeException;
14
use FlexPHP\Schema\SchemaAttributeInterface;
15
use FlexPHP\Schema\Traits\AttributeHelperTrait;
16
use FlexPHP\Schema\Validations\ValidationInterface;
17
18
class FormatLogicValidator implements ValidationInterface
19
{
20
    use AttributeHelperTrait;
21
22
    private SchemaAttributeInterface $property;
23
24 220
    public function __construct(SchemaAttributeInterface $property)
25
    {
26 220
        $this->property = $property;
27 220
    }
28
29 220
    public function validate(): void
30
    {
31 220
        if ($this->hasFormat($this->property)) {
32 35
            if ($this->isString($this->property)) {
33 7
                throw new InvalidSchemaAttributeException('String properties not allow format');
34
            }
35
36 28
            if ($this->isText($this->property)) {
37
                throw new InvalidSchemaAttributeException('Text properties not allow default');
38
            }
39
40 28
            if ($this->isObject($this->property)) {
41
                throw new InvalidSchemaAttributeException('Object properties not allow format');
42
            }
43
44 28
            if ($this->isBinary($this->property)) {
45 6
                throw new InvalidSchemaAttributeException('Binary (binary, bool, blob) properties not allow format');
46
            }
47
48 22
            if ($this->isArray($this->property)) {
49 9
                throw new InvalidSchemaAttributeException(
50 9
                    'Array (array, simple_array, json) properties not allow format',
51
                );
52
            }
53
54 13
            if ($this->isNumeric($this->property) && !$this->property->isFormat(Format::MONEY)) {
55 4
                throw new InvalidSchemaAttributeException(\sprintf(
56 4
                    'Numeric properties not allow format: %s',
57 4
                    $this->property->format()
58
                ));
59
            }
60
61 9
            if ($this->isDate($this->property) && $this->property->isFormat(Format::MONEY)) {
62 6
                throw new InvalidSchemaAttributeException(\sprintf(
63 6
                    'Date property not allow format: %s',
64 6
                    $this->property->format()
65
                ));
66
            }
67
        }
68 188
    }
69
}
70