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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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.