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

TypeConstraint   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 97.96%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 27
c 4
b 1
f 2
lcom 0
cbo 5
dl 0
loc 103
ccs 48
cts 49
cp 0.9796
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A keywords() 0 4 1
A supports() 0 4 1
C normalize() 0 32 8
D apply() 0 45 17
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