Skip to content

Commit 0f87b31

Browse files
narcoticfreshbighappyface
authored andcommitted
add instance cache, remove unnecessary execution paths (#288)
fix wrong merge
1 parent 1296583 commit 0f87b31

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/JsonSchema/Constraints/Factory.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Factory
2323
* @var SchemaStorage
2424
*/
2525
protected $schemaStorage;
26-
26+
2727
/**
2828
* @var UriRetriever $uriRetriever
2929
*/
@@ -56,6 +56,11 @@ class Factory
5656
'validator' => 'JsonSchema\Validator',
5757
);
5858

59+
/**
60+
* @var array<ConstraintInterface>
61+
*/
62+
private $instanceCache = array();
63+
5964
/**
6065
* @param SchemaStorage $schemaStorage
6166
* @param UriRetrieverInterface $uriRetriever
@@ -78,7 +83,7 @@ public function getUriRetriever()
7883
{
7984
return $this->uriRetriever;
8085
}
81-
86+
8287
public function getSchemaStorage()
8388
{
8489
return $this->schemaStorage;
@@ -124,12 +129,15 @@ public function setConstraintClass($name, $class)
124129
public function createInstanceFor($constraintName)
125130
{
126131
if (array_key_exists($constraintName, $this->constraintMap)) {
127-
return new $this->constraintMap[$constraintName](
128-
$this->checkMode,
129-
$this->schemaStorage,
130-
$this->uriRetriever,
131-
$this
132-
);
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];
133141
}
134142
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
135143
}

src/JsonSchema/Constraints/ObjectConstraint.php

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

108-
if (!$definition) {
109-
// normal property verification
110-
$this->checkUndefined($value, new \stdClass(), $path, $i);
111-
}
112-
113108
$property = $this->getProperty($element, $i, new UndefinedConstraint());
114109
if (is_object($property)) {
115110
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
@@ -129,7 +124,11 @@ public function validateDefinition($element, $objectDefinition = null, JsonPoint
129124
foreach ($objectDefinition as $i => $value) {
130125
$property = $this->getProperty($element, $i, $this->getFactory()->createInstanceFor('undefined'));
131126
$definition = $this->getProperty($objectDefinition, $i);
132-
$this->checkUndefined($property, $definition, $path, $i);
127+
128+
if (is_object($definition)) {
129+
// Undefined constraint will check for is_object() and quit if is not - so why pass it?
130+
$this->checkUndefined($property, $definition, $path, $i);
131+
}
133132
}
134133
}
135134

0 commit comments

Comments
 (0)