Skip to content

Commit 31b2954

Browse files
digitalkaozbighappyface
authored andcommitted
performance improvements (#310)
1 parent 70a1e2a commit 31b2954

14 files changed

+224
-250
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,9 +22,6 @@
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

@@ -33,70 +30,16 @@ abstract class Constraint implements ConstraintInterface
3330
const CHECK_MODE_COERCE = 0x00000004;
3431

3532
/**
36-
* @var null|Factory
33+
* @var Factory
3734
*/
38-
private $factory;
35+
protected $factory;
3936

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

10245
/**
@@ -124,7 +67,9 @@ public function addError(JsonPointer $path = null, $message, $constraint='', arr
12467
*/
12568
public function addErrors(array $errors)
12669
{
127-
$this->errors = array_merge($this->errors, $errors);
70+
if ($errors) {
71+
$this->errors = array_merge($this->errors, $errors);
72+
}
12873
}
12974

13075
/**
@@ -182,7 +127,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
182127
*/
183128
protected function checkArray($value, $schema = null, JsonPointer $path = null, $i = null)
184129
{
185-
$validator = $this->getFactory()->createInstanceFor('collection');
130+
$validator = $this->factory->createInstanceFor('collection');
186131
$validator->check($value, $schema, $path, $i);
187132

188133
$this->addErrors($validator->getErrors());
@@ -199,7 +144,7 @@ protected function checkArray($value, $schema = null, JsonPointer $path = null,
199144
*/
200145
protected function checkObject($value, $schema = null, JsonPointer $path = null, $i = null, $patternProperties = null)
201146
{
202-
$validator = $this->getFactory()->createInstanceFor('object');
147+
$validator = $this->factory->createInstanceFor('object');
203148
$validator->check($value, $schema, $path, $i, $patternProperties);
204149

205150
$this->addErrors($validator->getErrors());
@@ -215,7 +160,7 @@ protected function checkObject($value, $schema = null, JsonPointer $path = null,
215160
*/
216161
protected function checkType($value, $schema = null, JsonPointer $path = null, $i = null)
217162
{
218-
$validator = $this->getFactory()->createInstanceFor('type');
163+
$validator = $this->factory->createInstanceFor('type');
219164
$validator->check($value, $schema, $path, $i);
220165

221166
$this->addErrors($validator->getErrors());
@@ -231,8 +176,9 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
231176
*/
232177
protected function checkUndefined($value, $schema = null, JsonPointer $path = null, $i = null)
233178
{
234-
$validator = $this->getFactory()->createInstanceFor('undefined');
235-
$validator->check($value, $this->schemaStorage->resolveRefSchema($schema), $path, $i);
179+
$validator = $this->factory->createInstanceFor('undefined');
180+
181+
$validator->check($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i);
236182

237183
$this->addErrors($validator->getErrors());
238184
}
@@ -247,7 +193,7 @@ protected function checkUndefined($value, $schema = null, JsonPointer $path = nu
247193
*/
248194
protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null)
249195
{
250-
$validator = $this->getFactory()->createInstanceFor('string');
196+
$validator = $this->factory->createInstanceFor('string');
251197
$validator->check($value, $schema, $path, $i);
252198

253199
$this->addErrors($validator->getErrors());
@@ -263,7 +209,7 @@ protected function checkString($value, $schema = null, JsonPointer $path = null,
263209
*/
264210
protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null)
265211
{
266-
$validator = $this->getFactory()->createInstanceFor('number');
212+
$validator = $this->factory->createInstanceFor('number');
267213
$validator->check($value, $schema, $path, $i);
268214

269215
$this->addErrors($validator->getErrors());
@@ -279,7 +225,7 @@ protected function checkNumber($value, $schema = null, JsonPointer $path = null,
279225
*/
280226
protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null)
281227
{
282-
$validator = $this->getFactory()->createInstanceFor('enum');
228+
$validator = $this->factory->createInstanceFor('enum');
283229
$validator->check($value, $schema, $path, $i);
284230

285231
$this->addErrors($validator->getErrors());
@@ -295,7 +241,7 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $
295241
*/
296242
protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null)
297243
{
298-
$validator = $this->getFactory()->createInstanceFor('format');
244+
$validator = $this->factory->createInstanceFor('format');
299245
$validator->check($value, $schema, $path, $i);
300246

301247
$this->addErrors($validator->getErrors());
@@ -308,7 +254,7 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
308254
*/
309255
protected function getTypeCheck()
310256
{
311-
return $this->getFactory()->getTypeCheck();
257+
return $this->factory->getTypeCheck();
312258
}
313259

314260
/**

src/JsonSchema/Constraints/EnumConstraint.php

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

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

src/JsonSchema/Constraints/Factory.php

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

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 10 additions & 9 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
}
@@ -121,17 +122,17 @@ public function validateElement($element, $matches, $objectDefinition = null, Js
121122
*/
122123
public function validateDefinition($element, $objectDefinition = null, JsonPointer $path = null)
123124
{
124-
$default = $this->getFactory()->createInstanceFor('undefined');
125+
$undefinedConstraint = $this->factory->createInstanceFor('undefined');
125126

126127
foreach ($objectDefinition as $i => $value) {
127-
$property = $this->getProperty($element, $i, $default);
128+
$property = $this->getProperty($element, $i, $undefinedConstraint);
128129
$definition = $this->getProperty($objectDefinition, $i);
129130

130-
if($this->checkMode & Constraint::CHECK_MODE_TYPE_CAST){
131+
if($this->factory->getCheckMode() & Constraint::CHECK_MODE_TYPE_CAST){
131132
if(!($property instanceof Constraint)) {
132133
$property = $this->coerce($property, $definition);
133134

134-
if($this->checkMode & Constraint::CHECK_MODE_COERCE) {
135+
if($this->factory->getCheckMode() & Constraint::CHECK_MODE_COERCE) {
135136
if (is_object($element)) {
136137
$element->{$i} = $property;
137138
} else {
@@ -228,10 +229,10 @@ protected function coerce($value, $definition)
228229
*/
229230
protected function getProperty($element, $property, $fallback = null)
230231
{
231-
if (is_array($element) /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/) {
232-
return array_key_exists($property, $element) ? $element[$property] : $fallback;
233-
} elseif (is_object($element)) {
234-
return property_exists($element, $property) ? $element->$property : $fallback;
232+
if (is_array($element) && (isset($element[$property]) || array_key_exists($property, $element)) /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/) {
233+
return $element[$property];
234+
} elseif (is_object($element) && property_exists($element, $property)) {
235+
return $element->$property;
235236
}
236237

237238
return $fallback;

src/JsonSchema/Constraints/TypeConstraint.php

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

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function validateTypes($value, $schema = null, JsonPointer $path, $i = nu
6060
if ($this->getTypeCheck()->isObject($value)) {
6161
$this->checkObject(
6262
$value,
63-
isset($schema->properties) ? $this->schemaStorage->resolveRefSchema($schema->properties) : $schema,
63+
isset($schema->properties) ? $this->factory->getSchemaStorage()->resolveRefSchema($schema->properties) : $schema,
6464
$path,
6565
isset($schema->additionalProperties) ? $schema->additionalProperties : null,
6666
isset($schema->patternProperties) ? $schema->patternProperties : null
@@ -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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Validator extends Constraint
3232
*/
3333
public function check($value, $schema = null, JsonPointer $path = null, $i = null)
3434
{
35-
$validator = $this->getFactory()->createInstanceFor('schema');
35+
$validator = $this->factory->createInstanceFor('schema');
3636
$validator->check($value, $schema);
3737

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

0 commit comments

Comments
 (0)