Skip to content

Commit 2f590d5

Browse files
committed
add instance cache, remove unnecessary execution paths
1 parent ddabcf8 commit 2f590d5

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/JsonSchema/Constraints/Factory.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class Factory
4949
'validator' => 'JsonSchema\Validator',
5050
);
5151

52+
/**
53+
* @var array<ConstraintInterface>
54+
*/
55+
private $instanceCache = array();
56+
5257
/**
5358
* @param UriRetriever $uriRetriever
5459
*/
@@ -112,7 +117,14 @@ public function setConstraintClass($name, $class)
112117
public function createInstanceFor($constraintName)
113118
{
114119
if (array_key_exists($constraintName, $this->constraintMap)) {
115-
return new $this->constraintMap[$constraintName]($this->checkMode, $this->uriRetriever, $this);
120+
if (!isset($this->instanceCache[$constraintName])) {
121+
$this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName](
122+
$this->checkMode,
123+
$this->uriRetriever,
124+
$this
125+
);
126+
}
127+
return clone $this->instanceCache[$constraintName];
116128
}
117129
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
118130
}

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ public function validateElement($element, $matches, $objectDefinition = null, $p
103103
$this->addError($path, "The presence of the property " . $i . " requires that " . $require . " also be present", 'requires');
104104
}
105105

106-
if (!$definition) {
107-
// normal property verification
108-
$this->checkUndefined($value, new \stdClass(), $path, $i);
109-
}
110-
111106
$property = $this->getProperty($element, $i, new UndefinedConstraint());
112107
if (is_object($property)) {
113108
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
@@ -127,7 +122,11 @@ public function validateDefinition($element, $objectDefinition = null, $path = n
127122
foreach ($objectDefinition as $i => $value) {
128123
$property = $this->getProperty($element, $i, new UndefinedConstraint());
129124
$definition = $this->getProperty($objectDefinition, $i);
130-
$this->checkUndefined($property, $definition, $path, $i);
125+
126+
if (is_object($definition)) {
127+
// Undefined constraint will check for is_object() and quit if is not - so why pass it?
128+
$this->checkUndefined($property, $definition, $path, $i);
129+
}
131130
}
132131
}
133132

0 commit comments

Comments
 (0)