Skip to content

Commit 5cd62f7

Browse files
committed
performance improvements
1 parent 2128f9d commit 5cd62f7

13 files changed

+89
-115
lines changed

.travis.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
sudo: false
22
language: php
33

4+
cache:
5+
directories:
6+
- $HOME/.composer/cache
7+
48
matrix:
59
fast_finish: true
610
include:
711
- php: 5.3
812
- php: 5.4
913
- php: 5.5
1014
- php: 5.6
15+
- php: 7.0
1116
env: WITH_COVERAGE=true
12-
- php: 7
17+
- php: 7.1
18+
- php: 'nightly'
1319
- php: hhvm
20+
allow_failures:
21+
- php: 'nightly'
1422

1523
before_install:
16-
- if [[ "$WITH_COVERAGE" != "true" && "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini; fi
24+
- if [[ "$WITH_COVERAGE" != "true" && "$TRAVIS_PHP_VERSION" != "hhvm" && "$TRAVIS_PHP_VERSION" != "nightly" && "$TRAVIS_PHP_VERSION" != "7.1" ]]; then phpenv config-rm xdebug.ini; fi
1725
- composer selfupdate
1826

1927
install:
20-
- travis_retry composer install --no-interaction --prefer-source
28+
- travis_retry composer install --no-interaction --prefer-dist
2129

2230
script:
23-
- if [[ "$WITH_COVERAGE" == "true" ]]; then vendor/bin/phpunit --coverage-text; else vendor/bin/phpunit; fi
31+
- if [[ "$WITH_COVERAGE" == "true" ]]; then composer coverage; else composer test; fi

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,9 @@
5454
"branch-alias": {
5555
"dev-master": "3.0.x-dev"
5656
}
57+
},
58+
"scripts": {
59+
"test" : "vendor/bin/phpunit",
60+
"coverage" : "vendor/bin/phpunit --coverage-text"
5761
}
5862
}

src/JsonSchema/Constraints/CollectionConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected function validateItems($value, $schema = null, JsonPointer $path = nul
106106
// Treat when we have more schema definitions than values, not for empty arrays
107107
if (count($value) > 0) {
108108
for ($k = count($value); $k < count($schema->items); $k++) {
109-
$this->checkUndefined(new UndefinedConstraint(), $schema->items[$k], $path, $k);
109+
$this->checkUndefined($this->factory->createInstanceFor('undefined'), $schema->items[$k], $path, $k);
110110
}
111111
}
112112
}

src/JsonSchema/Constraints/Constraint.php

Lines changed: 18 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -22,80 +22,23 @@
2222
*/
2323
abstract class Constraint implements ConstraintInterface
2424
{
25-
protected $schemaStorage;
26-
protected $checkMode = self::CHECK_MODE_NORMAL;
27-
protected $uriRetriever;
2825
protected $errors = array();
2926
protected $inlineSchemaProperty = '$schema';
3027

3128
const CHECK_MODE_NORMAL = 1;
3229
const CHECK_MODE_TYPE_CAST = 2;
3330

3431
/**
35-
* @var null|Factory
32+
* @var Factory
3633
*/
37-
private $factory;
34+
protected $factory;
3835

3936
/**
40-
* @param int $checkMode
41-
* @param SchemaStorage $schemaStorage
42-
* @param UriRetrieverInterface $uriRetriever
4337
* @param Factory $factory
4438
*/
45-
public function __construct(
46-
$checkMode = self::CHECK_MODE_NORMAL,
47-
SchemaStorage $schemaStorage = null,
48-
UriRetrieverInterface $uriRetriever = null,
49-
Factory $factory = null
50-
) {
51-
$this->checkMode = $checkMode;
52-
$this->uriRetriever = $uriRetriever;
53-
$this->factory = $factory;
54-
$this->schemaStorage = $schemaStorage;
55-
}
56-
57-
/**
58-
* @return UriRetrieverInterface $uriRetriever
59-
*/
60-
public function getUriRetriever()
61-
{
62-
if (is_null($this->uriRetriever)) {
63-
$this->setUriRetriever(new UriRetriever);
64-
}
65-
66-
return $this->uriRetriever;
67-
}
68-
69-
/**
70-
* @return Factory
71-
*/
72-
public function getFactory()
39+
public function __construct(Factory $factory = null)
7340
{
74-
if (!$this->factory) {
75-
$this->factory = new Factory($this->getSchemaStorage(), $this->getUriRetriever(), $this->checkMode);
76-
}
77-
78-
return $this->factory;
79-
}
80-
81-
/**
82-
* @return SchemaStorage
83-
*/
84-
public function getSchemaStorage()
85-
{
86-
if (is_null($this->schemaStorage)) {
87-
$this->schemaStorage = new SchemaStorage($this->getUriRetriever());
88-
}
89-
90-
return $this->schemaStorage;
91-
}
92-
93-
/**
94-
* @param UriRetrieverInterface $uriRetriever
95-
*/
96-
public function setUriRetriever(UriRetrieverInterface $uriRetriever)
97-
{
98-
$this->uriRetriever = $uriRetriever;
41+
$this->factory = $factory ? : new Factory();
9942
}
10043

10144
/**
@@ -123,7 +66,9 @@ public function addError(JsonPointer $path = null, $message, $constraint='', arr
12366
*/
12467
public function addErrors(array $errors)
12568
{
126-
$this->errors = array_merge($this->errors, $errors);
69+
if ($errors) {
70+
$this->errors = array_merge($this->errors, $errors);
71+
}
12772
}
12873

12974
/**
@@ -181,7 +126,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
181126
*/
182127
protected function checkArray($value, $schema = null, JsonPointer $path = null, $i = null)
183128
{
184-
$validator = $this->getFactory()->createInstanceFor('collection');
129+
$validator = $this->factory->createInstanceFor('collection');
185130
$validator->check($value, $schema, $path, $i);
186131

187132
$this->addErrors($validator->getErrors());
@@ -198,7 +143,7 @@ protected function checkArray($value, $schema = null, JsonPointer $path = null,
198143
*/
199144
protected function checkObject($value, $schema = null, JsonPointer $path = null, $i = null, $patternProperties = null)
200145
{
201-
$validator = $this->getFactory()->createInstanceFor('object');
146+
$validator = $this->factory->createInstanceFor('object');
202147
$validator->check($value, $schema, $path, $i, $patternProperties);
203148

204149
$this->addErrors($validator->getErrors());
@@ -214,7 +159,7 @@ protected function checkObject($value, $schema = null, JsonPointer $path = null,
214159
*/
215160
protected function checkType($value, $schema = null, JsonPointer $path = null, $i = null)
216161
{
217-
$validator = $this->getFactory()->createInstanceFor('type');
162+
$validator = $this->factory->createInstanceFor('type');
218163
$validator->check($value, $schema, $path, $i);
219164

220165
$this->addErrors($validator->getErrors());
@@ -230,8 +175,9 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
230175
*/
231176
protected function checkUndefined($value, $schema = null, JsonPointer $path = null, $i = null)
232177
{
233-
$validator = $this->getFactory()->createInstanceFor('undefined');
234-
$validator->check($value, $this->schemaStorage->resolveRefSchema($schema), $path, $i);
178+
$validator = $this->factory->createInstanceFor('undefined');
179+
180+
$validator->check($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i);
235181

236182
$this->addErrors($validator->getErrors());
237183
}
@@ -246,7 +192,7 @@ protected function checkUndefined($value, $schema = null, JsonPointer $path = nu
246192
*/
247193
protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null)
248194
{
249-
$validator = $this->getFactory()->createInstanceFor('string');
195+
$validator = $this->factory->createInstanceFor('string');
250196
$validator->check($value, $schema, $path, $i);
251197

252198
$this->addErrors($validator->getErrors());
@@ -262,7 +208,7 @@ protected function checkString($value, $schema = null, JsonPointer $path = null,
262208
*/
263209
protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null)
264210
{
265-
$validator = $this->getFactory()->createInstanceFor('number');
211+
$validator = $this->factory->createInstanceFor('number');
266212
$validator->check($value, $schema, $path, $i);
267213

268214
$this->addErrors($validator->getErrors());
@@ -278,7 +224,7 @@ protected function checkNumber($value, $schema = null, JsonPointer $path = null,
278224
*/
279225
protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null)
280226
{
281-
$validator = $this->getFactory()->createInstanceFor('enum');
227+
$validator = $this->factory->createInstanceFor('enum');
282228
$validator->check($value, $schema, $path, $i);
283229

284230
$this->addErrors($validator->getErrors());
@@ -294,7 +240,7 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $
294240
*/
295241
protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null)
296242
{
297-
$validator = $this->getFactory()->createInstanceFor('format');
243+
$validator = $this->factory->createInstanceFor('format');
298244
$validator->check($value, $schema, $path, $i);
299245

300246
$this->addErrors($validator->getErrors());
@@ -307,7 +253,7 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
307253
*/
308254
protected function getTypeCheck()
309255
{
310-
return $this->getFactory()->getTypeCheck();
256+
return $this->factory->getTypeCheck();
311257
}
312258

313259
/**

src/JsonSchema/Constraints/EnumConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function check($element, $schema = null, JsonPointer $path = null, $i = n
3232

3333
foreach ($schema->enum as $enum) {
3434
$enumType = gettype($enum);
35-
if ($this->checkMode === self::CHECK_MODE_TYPE_CAST && $type == "array" && $enumType == "object") {
35+
if ($this->factory->getCheckMode() === self::CHECK_MODE_TYPE_CAST && $type == "array" && $enumType == "object") {
3636
if ((object)$element == $enum) {
3737
return;
3838
}

src/JsonSchema/Constraints/Factory.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,22 @@ public function setConstraintClass($name, $class)
128128
*/
129129
public function createInstanceFor($constraintName)
130130
{
131-
if (array_key_exists($constraintName, $this->constraintMap)) {
132-
if (!isset($this->instanceCache[$constraintName])) {
133-
$this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName](
134-
$this->checkMode,
135-
$this->schemaStorage,
136-
$this->uriRetriever,
137-
$this
138-
);
139-
}
140-
return clone $this->instanceCache[$constraintName];
131+
if (!isset($this->constraintMap[$constraintName])) {
132+
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
141133
}
142-
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
134+
135+
if (!isset($this->instanceCache[$constraintName])) {
136+
$this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName]($this);
137+
}
138+
139+
return clone $this->instanceCache[$constraintName];
140+
}
141+
142+
/**
143+
* @return int
144+
*/
145+
public function getCheckMode()
146+
{
147+
return $this->checkMode;
143148
}
144149
}

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function validatePatternProperties($element, JsonPointer $path = null, $p
8282
public function validateElement($element, $matches, $objectDefinition = null, JsonPointer $path = null, $additionalProp = null)
8383
{
8484
$this->validateMinMaxConstraint($element, $objectDefinition, $path);
85+
8586
foreach ($element as $i => $value) {
8687
$definition = $this->getProperty($objectDefinition, $i);
8788

@@ -105,7 +106,7 @@ public function validateElement($element, $matches, $objectDefinition = null, Js
105106
$this->addError($path, "The presence of the property " . $i . " requires that " . $require . " also be present", 'requires');
106107
}
107108

108-
$property = $this->getProperty($element, $i, new UndefinedConstraint());
109+
$property = $this->getProperty($element, $i, $this->factory->createInstanceFor('undefined'));
109110
if (is_object($property)) {
110111
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
111112
}
@@ -117,12 +118,14 @@ public function validateElement($element, $matches, $objectDefinition = null, Js
117118
*
118119
* @param \stdClass $element Element to validate
119120
* @param \stdClass $objectDefinition ObjectConstraint definition
120-
* @param JsoinPointer|null $path Path?
121+
* @param JsonPointer|null $path Path?
121122
*/
122123
public function validateDefinition($element, $objectDefinition = null, JsonPointer $path = null)
123124
{
125+
$undefinedConstraint = $this->factory->createInstanceFor('undefined');
126+
124127
foreach ($objectDefinition as $i => $value) {
125-
$property = $this->getProperty($element, $i, $this->getFactory()->createInstanceFor('undefined'));
128+
$property = $this->getProperty($element, $i, $undefinedConstraint);
126129
$definition = $this->getProperty($objectDefinition, $i);
127130

128131
if (is_object($definition)) {
@@ -143,10 +146,10 @@ public function validateDefinition($element, $objectDefinition = null, JsonPoint
143146
*/
144147
protected function getProperty($element, $property, $fallback = null)
145148
{
146-
if (is_array($element) /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/) {
147-
return array_key_exists($property, $element) ? $element[$property] : $fallback;
148-
} elseif (is_object($element)) {
149-
return property_exists($element, $property) ? $element->$property : $fallback;
149+
if (is_array($element) && (isset($element[$property]) || array_key_exists($property, $element)) /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/) {
150+
return $element[$property];
151+
} elseif (is_object($element) && property_exists($element, $property)) {
152+
return $element->$property;
150153
}
151154

152155
return $fallback;

src/JsonSchema/Constraints/TypeConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected function validateTypesArray($value, array $type, &$validTypesWording,
8181
// with a new type constraint
8282
if (is_object($tp)) {
8383
if (!$isValid) {
84-
$validator = $this->getFactory()->createInstanceFor('type');
84+
$validator = $this->factory->createInstanceFor('type');
8585
$subSchema = new \stdClass();
8686
$subSchema->type = $tp;
8787
$validator->check($value, $subSchema, $path, null);

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ protected function validateDependencies($value, $dependencies, JsonPointer $path
267267
protected function validateUri($schema, $schemaUri = null)
268268
{
269269
$resolver = new UriResolver();
270-
$retriever = $this->getUriRetriever();
270+
$retriever = $this->factory->getUriRetriever();
271271

272272
$jsonSchema = null;
273273
if ($resolver->isValid($schemaUri)) {

src/JsonSchema/Validator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace JsonSchema;
1111

12-
use JsonSchema\Constraints\SchemaConstraint;
1312
use JsonSchema\Constraints\Constraint;
1413
use JsonSchema\Entity\JsonPointer;
1514

@@ -33,7 +32,7 @@ class Validator extends Constraint
3332
*/
3433
public function check($value, $schema = null, JsonPointer $path = null, $i = null)
3534
{
36-
$validator = $this->getFactory()->createInstanceFor('schema');
35+
$validator = $this->factory->createInstanceFor('schema');
3736
$validator->check($value, $schema);
3837

3938
$this->addErrors(array_unique($validator->getErrors(), SORT_REGULAR));

0 commit comments

Comments
 (0)