Skip to content

Commit

Permalink
add instance cache, remove unnecessary execution paths
Browse files Browse the repository at this point in the history
  • Loading branch information
narcoticfresh committed Aug 19, 2016
1 parent 1296583 commit 72e5066
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
24 changes: 16 additions & 8 deletions src/JsonSchema/Constraints/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Factory
* @var SchemaStorage
*/
protected $schemaStorage;

/**
* @var UriRetriever $uriRetriever
*/
Expand Down Expand Up @@ -56,6 +56,11 @@ class Factory
'validator' => 'JsonSchema\Validator',
);

/**
* @var array<ConstraintInterface>
*/
private $instanceCache = array();

/**
* @param SchemaStorage $schemaStorage
* @param UriRetrieverInterface $uriRetriever
Expand All @@ -78,7 +83,7 @@ public function getUriRetriever()
{
return $this->uriRetriever;
}

public function getSchemaStorage()
{
return $this->schemaStorage;
Expand Down Expand Up @@ -124,12 +129,15 @@ public function setConstraintClass($name, $class)
public function createInstanceFor($constraintName)
{
if (array_key_exists($constraintName, $this->constraintMap)) {
return new $this->constraintMap[$constraintName](
$this->checkMode,
$this->schemaStorage,
$this->uriRetriever,
$this
);
if (!isset($this->instanceCache[$constraintName])) {
$this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName](
$this->checkMode,
$this->schemaStorage,
$this->uriRetriever,
$this
);
return clone $this->instanceCache[$constraintName];
}
}
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
}
Expand Down
11 changes: 5 additions & 6 deletions src/JsonSchema/Constraints/ObjectConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ public function validateElement($element, $matches, $objectDefinition = null, Js
$this->addError($path, "The presence of the property " . $i . " requires that " . $require . " also be present", 'requires');
}

if (!$definition) {
// normal property verification
$this->checkUndefined($value, new \stdClass(), $path, $i);
}

$property = $this->getProperty($element, $i, new UndefinedConstraint());
if (is_object($property)) {
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
Expand All @@ -129,7 +124,11 @@ public function validateDefinition($element, $objectDefinition = null, JsonPoint
foreach ($objectDefinition as $i => $value) {
$property = $this->getProperty($element, $i, $this->getFactory()->createInstanceFor('undefined'));
$definition = $this->getProperty($objectDefinition, $i);
$this->checkUndefined($property, $definition, $path, $i);

if (is_object($definition)) {
// Undefined constraint will check for is_object() and quit if is not - so why pass it?
$this->checkUndefined($property, $definition, $path, $i);
}
}
}

Expand Down

0 comments on commit 72e5066

Please sign in to comment.