Skip to content

Commit 0a48d3a

Browse files
committed
avoid unnessary array_merge calls
1 parent 2128f9d commit 0a48d3a

File tree

4 files changed

+24
-52
lines changed

4 files changed

+24
-52
lines changed

src/JsonSchema/Constraints/Constraint.php

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,19 @@ abstract class Constraint implements ConstraintInterface
3737
private $factory;
3838

3939
/**
40-
* @param int $checkMode
41-
* @param SchemaStorage $schemaStorage
42-
* @param UriRetrieverInterface $uriRetriever
4340
* @param Factory $factory
4441
*/
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;
42+
public function __construct(Factory $factory = null)
43+
{
44+
$this->factory = $factory;
5545
}
5646

5747
/**
5848
* @return UriRetrieverInterface $uriRetriever
5949
*/
6050
public function getUriRetriever()
6151
{
62-
if (is_null($this->uriRetriever)) {
63-
$this->setUriRetriever(new UriRetriever);
64-
}
65-
66-
return $this->uriRetriever;
52+
return $this->getFactory()->getUriRetriever();
6753
}
6854

6955
/**
@@ -72,7 +58,7 @@ public function getUriRetriever()
7258
public function getFactory()
7359
{
7460
if (!$this->factory) {
75-
$this->factory = new Factory($this->getSchemaStorage(), $this->getUriRetriever(), $this->checkMode);
61+
$this->factory = new Factory();
7662
}
7763

7864
return $this->factory;
@@ -83,19 +69,7 @@ public function getFactory()
8369
*/
8470
public function getSchemaStorage()
8571
{
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;
72+
return $this->getFactory()->getSchemaStorage();
9973
}
10074

10175
/**
@@ -123,7 +97,9 @@ public function addError(JsonPointer $path = null, $message, $constraint='', arr
12397
*/
12498
public function addErrors(array $errors)
12599
{
126-
$this->errors = array_merge($this->errors, $errors);
100+
if ($errors) {
101+
$this->errors = array_merge($this->errors, $errors);
102+
}
127103
}
128104

129105
/**
@@ -231,7 +207,7 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
231207
protected function checkUndefined($value, $schema = null, JsonPointer $path = null, $i = null)
232208
{
233209
$validator = $this->getFactory()->createInstanceFor('undefined');
234-
$validator->check($value, $this->schemaStorage->resolveRefSchema($schema), $path, $i);
210+
$validator->check($value, $this->getSchemaStorage()->resolveRefSchema($schema), $path, $i);
235211

236212
$this->addErrors($validator->getErrors());
237213
}

src/JsonSchema/Constraints/Factory.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Factory
3737
/**
3838
* @var TypeCheck\TypeCheckInterface[]
3939
*/
40-
private $typeCheck = array();
40+
public static $typeCheck = array();
4141

4242
/**
4343
* @var array $constraintMap
@@ -59,7 +59,7 @@ class Factory
5959
/**
6060
* @var array<ConstraintInterface>
6161
*/
62-
private $instanceCache = array();
62+
public static $instanceCache = array();
6363

6464
/**
6565
* @param SchemaStorage $schemaStorage
@@ -91,13 +91,13 @@ public function getSchemaStorage()
9191

9292
public function getTypeCheck()
9393
{
94-
if (!isset($this->typeCheck[$this->checkMode])) {
95-
$this->typeCheck[$this->checkMode] = $this->checkMode === Constraint::CHECK_MODE_TYPE_CAST
94+
if (!isset(self::$typeCheck[$this->checkMode])) {
95+
self::$typeCheck[$this->checkMode] = $this->checkMode === Constraint::CHECK_MODE_TYPE_CAST
9696
? new TypeCheck\LooseTypeCheck
9797
: new TypeCheck\StrictTypeCheck;
9898
}
9999

100-
return $this->typeCheck[$this->checkMode];
100+
return self::$typeCheck[$this->checkMode];
101101
}
102102

103103
/**
@@ -128,17 +128,14 @@ 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(self::$instanceCache[$constraintName])) {
136+
self::$instanceCache[$constraintName] = new $this->constraintMap[$constraintName]($this);
137+
}
138+
139+
return clone self::$instanceCache[$constraintName];
143140
}
144141
}

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function validateDefinition($element, $objectDefinition = null, JsonPoint
144144
protected function getProperty($element, $property, $fallback = null)
145145
{
146146
if (is_array($element) /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/) {
147-
return array_key_exists($property, $element) ? $element[$property] : $fallback;
147+
return isset($element[$property]) ? $element[$property] : $fallback;
148148
} elseif (is_object($element)) {
149149
return property_exists($element, $property) ? $element->$property : $fallback;
150150
}

src/JsonSchema/Validator.php

Lines changed: 0 additions & 1 deletion
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

0 commit comments

Comments
 (0)