Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 6c53a53

Browse files
committed
Fix validation of $creationOptions in ElementFactory
In setCreationOptions(array $options), a TypeError would be thrown if it was anything other than an array. Move the exceptions and traversable checks to the constructor.
1 parent ff4c9ec commit 6c53a53

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/ElementFactory.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ public function __construct($creationOptions = null)
3737
if (null === $creationOptions) {
3838
return;
3939
}
40+
if ($creationOptions instanceof Traversable) {
41+
$creationOptions = iterator_to_array($creationOptions);
42+
}
43+
44+
if (! is_array($creationOptions)) {
45+
throw new InvalidServiceException(sprintf(
46+
'%s cannot use non-array, non-traversable, non-null creation options; received %s',
47+
__CLASS__,
48+
(is_object($creationOptions) ? get_class($creationOptions) : gettype($creationOptions))
49+
));
50+
}
4051

4152
$this->setCreationOptions($creationOptions);
4253
}
@@ -118,18 +129,6 @@ public function createService(ServiceLocatorInterface $serviceLocator, $canonica
118129
*/
119130
public function setCreationOptions(array $creationOptions)
120131
{
121-
if ($creationOptions instanceof Traversable) {
122-
$creationOptions = iterator_to_array($creationOptions);
123-
}
124-
125-
if (! is_array($creationOptions)) {
126-
throw new InvalidServiceException(sprintf(
127-
'%s cannot use non-array, non-traversable creation options; received %s',
128-
__CLASS__,
129-
(is_object($creationOptions) ? get_class($creationOptions) : gettype($creationOptions))
130-
));
131-
}
132-
133132
$this->creationOptions = $creationOptions;
134133
}
135134
}

0 commit comments

Comments
 (0)