Skip to content

Commit 3c2e380

Browse files
committed
minor changes to better performance; try to minimize calls and if() checks
1 parent 8490e82 commit 3c2e380

File tree

3 files changed

+30
-43
lines changed

3 files changed

+30
-43
lines changed

src/JsonSchema/Constraints/Factory.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class Factory
4040
'validator' => 'JsonSchema\Validator',
4141
);
4242

43+
/**
44+
* @var array<ConstraintInterface>
45+
*/
46+
private $instanceCache = array();
47+
4348
/**
4449
* @param UriRetriever $uriRetriever
4550
*/
@@ -89,7 +94,14 @@ public function setConstraintClass($name, $class)
8994
public function createInstanceFor($constraintName)
9095
{
9196
if (array_key_exists($constraintName, $this->constraintMap)) {
92-
return new $this->constraintMap[$constraintName](Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
97+
if (!isset($this->instanceCache[$constraintName])) {
98+
$this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName](
99+
Constraint::CHECK_MODE_NORMAL,
100+
$this->uriRetriever,
101+
$this
102+
);
103+
}
104+
return clone $this->instanceCache[$constraintName];
93105
}
94106
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
95107
}

src/JsonSchema/Constraints/TypeConstraint.php

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -98,45 +98,26 @@ public function check($value = null, $schema = null, $path = null, $i = null)
9898
*/
9999
protected function validateType($value, $type)
100100
{
101-
//mostly the case for inline schema
102101
if (!$type) {
102+
//mostly the case for inline schema
103103
return true;
104-
}
105-
106-
if ('integer' === $type) {
104+
} elseif ('integer' === $type) {
107105
return is_int($value);
108-
}
109-
110-
if ('number' === $type) {
106+
} elseif ('number' === $type) {
111107
return is_numeric($value) && !is_string($value);
112-
}
113-
114-
if ('boolean' === $type) {
108+
} elseif ('boolean' === $type) {
115109
return is_bool($value);
116-
}
117-
118-
if ('object' === $type) {
110+
} elseif ('object' === $type) {
119111
return is_object($value);
120-
//return ($this::CHECK_MODE_TYPE_CAST == $this->checkMode) ? is_array($value) : is_object($value);
121-
}
122-
123-
if ('array' === $type) {
112+
} elseif ('array' === $type) {
124113
return is_array($value);
125-
}
126-
127-
if ('string' === $type) {
114+
} elseif ('string' === $type) {
128115
return is_string($value);
129-
}
130-
131-
if ('email' === $type) {
116+
} elseif ('email' === $type) {
132117
return is_string($value);
133-
}
134-
135-
if ('null' === $type) {
118+
} elseif ('null' === $type) {
136119
return is_null($value);
137-
}
138-
139-
if ('any' === $type) {
120+
} elseif ('any' === $type) {
140121
return true;
141122
}
142123

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,23 @@ public function check($value, $schema = null, $path = null, $i = null)
5252
*/
5353
public function validateTypes($value, $schema = null, $path = null, $i = null)
5454
{
55-
// check array
5655
if (is_array($value)) {
56+
// check array
5757
$this->checkArray($value, $schema, $path, $i);
58-
}
59-
60-
// check object
61-
if (is_object($value)) {
58+
} elseif (is_object($value)) {
59+
// check object
6260
$this->checkObject(
6361
$value,
6462
isset($schema->properties) ? $schema->properties : $schema,
6563
$path,
6664
isset($schema->additionalProperties) ? $schema->additionalProperties : null,
6765
isset($schema->patternProperties) ? $schema->patternProperties : null
6866
);
69-
}
70-
71-
// check string
72-
if (is_string($value)) {
67+
} elseif (is_string($value)) {
68+
// check string
7369
$this->checkString($value, $schema, $path, $i);
74-
}
75-
76-
// check numeric
77-
if (is_numeric($value)) {
70+
} elseif (is_numeric($value)) {
71+
// check numeric
7872
$this->checkNumber($value, $schema, $path, $i);
7973
}
8074

0 commit comments

Comments
 (0)