Skip to content

Commit d45e792

Browse files
committed
Check parameter type
Since the setParent and setPosition arguments expect an object we should check that that is what being provided. Rationale - As a noob user of the CMF components I wasn't sure I was supposed to be passing the parent object or path. When passing the path I would get odd errors such as: ``` PHP Warning: spl_object_hash() expects parameter 1 to be object, string given in /home/gnat/Projects/photography/vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/UnitOfWork.php on line 942 PHP Warning: get_class() expects parameter 1 to be object, string given in /home/gnat/Projects/photography/vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/UnitOfWork.php on line 944 ``` This change fails sooner and in a better way in my opinion.
1 parent dce6ad8 commit d45e792

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Doctrine/Phpcr/Route.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use Doctrine\Common\Collections\Collection;
1616
use Doctrine\ODM\PHPCR\Document\Generic;
17+
use Doctrine\ODM\PHPCR\Exception\InvalidArgumentException;
1718
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
1819
use Symfony\Cmf\Bundle\RoutingBundle\Model\Route as RouteModel;
1920

@@ -96,6 +97,9 @@ public function setAddTrailingSlash($addTrailingSlash)
9697
*/
9798
public function setParent($parent)
9899
{
100+
if(!is_object($parent))
101+
throw new InvalidArgumentException("Parent must be an object ".gettype ($parent)." given.");
102+
99103
$this->parent = $parent;
100104

101105
return $this;
@@ -138,6 +142,9 @@ public function getName()
138142
*/
139143
public function setPosition($parent, $name)
140144
{
145+
if(!is_object($parent))
146+
throw new InvalidArgumentException("Parent must be an object ".gettype ($parent)." given.");
147+
141148
$this->parent = $parent;
142149
$this->name = $name;
143150

0 commit comments

Comments
 (0)