Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

ConfigValidator::validate()   C

Complexity

Conditions 13
Paths 28

Size

Total Lines 50
Code Lines 27

Duplication

Lines 8
Ratio 16 %

Code Coverage

Tests 33
CRAP Score 13

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 8
loc 50
rs 5.3808
ccs 33
cts 33
cp 1
cc 13
eloc 27
nc 28
nop 3
crap 13

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
2
/*
3
* This file is a part of graphql-youshido project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 11/28/15 2:25 AM
7
*/
8
9
namespace Youshido\GraphQL\Validator\ConfigValidator;
10
11
12
use Youshido\GraphQL\Validator\ConfigValidator\Rules\TypeValidationRule;
13
use Youshido\GraphQL\Validator\ConfigValidator\Rules\ValidationRuleInterface;
14
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerTrait;
15
use Youshido\GraphQL\Validator\Exception\ValidationException;
16
17
class ConfigValidator implements ConfigValidatorInterface
18
{
19
20
    use ErrorContainerTrait;
21
22
    protected $rules = [];
23
24
    protected $contextObject;
25
26
    protected $extraFieldsAllowed = false;
27
28
    /** @var ValidationRuleInterface[] */
29
    protected $validationRules = [];
30
31 125
    public function __construct($contextObject = null)
32
    {
33 125
        $this->contextObject = $contextObject;
34 125
        $this->initializeRules();
35 125
    }
36
37 99
    public function validate($data, $rules = [], $extraFieldsAllowed = null)
38
    {
39 99
        if ($extraFieldsAllowed !== null) $this->setExtraFieldsAllowed($extraFieldsAllowed);
40
41 99
        $processedFields = [];
42 99
        foreach ($rules as $fieldName => $fieldRules) {
43 99
            $processedFields[] = $fieldName;
44
45
            /** Custom validation of 'required' property */
46 99
            if (array_key_exists('required', $fieldRules)) {
47 86
                unset($fieldRules['required']);
48
49 86 View Code Duplication
                if (!array_key_exists($fieldName, $data)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50 12
                    $this->addError(new ValidationException('Field \'' . $fieldName . '\' of ' . $this->getContextName() . ' is required'));
51
52 12
                    continue;
53
                }
54 99
            } elseif (!array_key_exists($fieldName, $data)) {
55 94
                continue;
56
            }
57 98
            if (!empty($fieldRules['final'])) unset($fieldRules['final']);
58
59
            /** Validation of all other rules*/
60 98
            foreach ($fieldRules as $ruleName => $ruleInfo) {
61 98 View Code Duplication
                if (!array_key_exists($ruleName, $this->validationRules)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62 1
                    $this->addError(new ValidationException('Field \'' . $fieldName . '\' has invalid rule \'' . $ruleInfo . '\''));
63
64 1
                    continue;
65
                }
66
67 98
                if (!$this->validationRules[$ruleName]->validate($data[$fieldName], $ruleInfo)) {
68 13
                    $this->addError(
69 13
                        new ValidationException('Field \'' . $fieldName . '\' of ' . $this->getContextName()
70 13
                                                . ' expected to be ' . $ruleName . ': \'' . (string)$ruleInfo . '\', but got: ' . gettype($data[$fieldName])));
71 13
                }
72 98
            }
73 99
        }
74
75 99
        if (!$this->isExtraFieldsAllowed()) {
76 99
            foreach (array_keys($data) as $fieldName) {
77 99
                if (!in_array($fieldName, $processedFields)) {
78 1
                    $this->addError(
79 1
                        new ValidationException('Field \'' . $fieldName . '\' is not expected in ' . $this->getContextName()));
80
81 1
                }
82 99
            }
83 99
        }
84
85 99
        return $this->isValid();
86
    }
87
88 125
    protected function initializeRules()
89
    {
90 125
        $this->validationRules['type'] = new TypeValidationRule($this);
91 125
    }
92
93
    /**
94
     * @return string
95
     */
96 19
    protected function getContextName()
97
    {
98 19
        if (is_object($this->contextObject)) {
99 13
            $class = get_class($this->contextObject);
100 13
            $class = substr($class, strrpos($class, '\\') + 1);
101
102 13
            return $class;
103
        } else {
104 6
            return $this->contextObject ? $this->contextObject : '(context)';
105
        }
106
    }
107
108 99
    public function isValid()
109
    {
110 99
        return !$this->hasErrors();
111
    }
112
113
114
    /**
115
     * @return boolean
116
     */
117 99
    public function isExtraFieldsAllowed()
118
    {
119 99
        return $this->extraFieldsAllowed;
120
    }
121
122
    /**
123
     * @param boolean $extraFieldsAllowed
124
     *
125
     * @return ConfigValidator
126
     */
127 1
    public function setExtraFieldsAllowed($extraFieldsAllowed)
128
    {
129 1
        $this->extraFieldsAllowed = $extraFieldsAllowed;
130
131 1
        return $this;
132
    }
133
134
}
135