Completed
Pull Request — master (#10)
by Jan
02:30
created

TypeConstraint::apply()   D

Complexity

Conditions 17
Paths 11

Size

Total Lines 45
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 17.0184

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 45
ccs 24
cts 25
cp 0.96
rs 4.9807
cc 17
eloc 23
nc 11
nop 4
crap 17.0184

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
/*
4
 * This file is part of the JVal package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace JVal\Constraint;
11
12
use JVal\Constraint;
13
use JVal\Context;
14
use JVal\Exception\Constraint\InvalidTypeException;
15
use JVal\Exception\Constraint\NotPrimitiveTypeException;
16
use JVal\Exception\Constraint\NotUniqueException;
17
use JVal\Types;
18
use JVal\Walker;
19
use stdClass;
20
21
/**
22
 * Constraint for the "type" keyword.
23
 */
24
class TypeConstraint implements Constraint
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 357
    public function keywords()
30
    {
31 357
        return ['type'];
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 348
    public function supports($type)
38
    {
39 348
        return true;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 205
    public function normalize(stdClass $schema, Context $context, Walker $walker)
46
    {
47 205
        $context->enterNode($schema->type, 'type');
48
49 205
        if (is_string($schema->type)) {
50 186
            if (!Types::isPrimitive($schema->type)) {
51 1
                throw new NotPrimitiveTypeException($context);
52
            }
53 204
        } elseif (is_array($schema->type)) {
54 18
            foreach ($schema->type as $index => $type) {
55 18
                $context->enterNode($type, $index);
56
57 18
                if (!is_string($type)) {
58 1
                    throw new InvalidTypeException($context, Types::TYPE_STRING);
59
                }
60
61 18
                if (!Types::isPrimitive($type)) {
62 1
                    throw new NotPrimitiveTypeException($context);
63
                }
64
65 18
                $context->leaveNode();
66 18
            }
67
68 16
            if (count(array_unique($schema->type)) !== count($schema->type)) {
69 1
                throw new NotUniqueException($context);
70
            }
71 15
        } else {
72 1
            throw new InvalidTypeException($context, [Types::TYPE_STRING, Types::TYPE_ARRAY]);
73
        }
74
75 200
        $context->leaveNode();
76 200
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 184
    public function apply($instance, stdClass $schema, Context $context, Walker $walker)
82
    {
83 184
        $actualType = gettype($instance);
84
85 184
        if (is_string($schema->type)) {
86 169
            if ($actualType === $schema->type) { // integer, string, boolean, array, object
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
87 86
                return;
88
            }
89
90 100
            if ($actualType === 'double' && $schema->type === Types::TYPE_NUMBER) {
91 4
                return;
92
            }
93
94 97
            if ($actualType === 'integer' && $schema->type === Types::TYPE_NUMBER) {
95 3
                return;
96
            }
97
98 94
            if ($actualType === 'NULL' && $schema->type === Types::TYPE_NULL) {
99 4
                return;
100
            }
101
102 90
            $context->addViolation('instance must be of type %s', [$schema->type]);
103
104 90
        } else {
105 15
            if (in_array($actualType, $schema->type)) { // integer, string, boolean, array, object
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106 6
                return;
107
            }
108
109 9
            foreach ($schema->type as $expected) {
110 9
                if ($actualType === 'NULL' && $expected === Types::TYPE_NULL) {
111
                    return;
112
                }
113
114 9
                if ($actualType === 'double' && $expected === Types::TYPE_NUMBER) {
115 1
                    return;
116
                }
117
118 8
                if ($actualType === 'integer' && $expected === Types::TYPE_NUMBER) {
119 1
                    return;
120
                }
121 7
            }
122
123 7
            $context->addViolation('instance does not match any of the expected types');
124
        }
125 97
    }
126
}
127