Skip to content

Commit

Permalink
Merge pull request #275 from entering/avoid-warning-ref-class-parents
Browse files Browse the repository at this point in the history
RuntimeReflectionService - throw exception instead of php warning
  • Loading branch information
beberlei committed May 9, 2013
2 parents 7eea788 + 660900a commit ce87784
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Doctrine/Common/Persistence/Mapping/MappingException.php
Expand Up @@ -83,4 +83,14 @@ public static function invalidMappingFile($entityName, $fileName)
{ {
return new self("Invalid mapping file '$fileName' for class '$entityName'."); return new self("Invalid mapping file '$fileName' for class '$entityName'.");
} }

/**
* @param string $className
*
* @return \Doctrine\Common\Persistence\Mapping\MappingException
*/
public static function nonExistingClass($className)
{
return new self("Class '$className' does not exists");
}
} }
3 changes: 3 additions & 0 deletions lib/Doctrine/Common/Persistence/Mapping/ReflectionService.php
Expand Up @@ -33,6 +33,9 @@ interface ReflectionService
* Return an array of the parent classes (not interfaces) for the given class. * Return an array of the parent classes (not interfaces) for the given class.
* *
* @param string $class * @param string $class
*
* @throws \Doctrine\Common\Persistence\Mapping\MappingException
*
* @return array * @return array
*/ */
function getParentClasses($class); function getParentClasses($class);
Expand Down
Expand Up @@ -22,6 +22,7 @@
use ReflectionClass; use ReflectionClass;
use ReflectionProperty; use ReflectionProperty;
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty; use Doctrine\Common\Reflection\RuntimePublicReflectionProperty;
use Doctrine\Common\Persistence\Mapping\MappingException;


/** /**
* PHP Runtime Reflection Service * PHP Runtime Reflection Service
Expand All @@ -35,6 +36,10 @@ class RuntimeReflectionService implements ReflectionService
*/ */
public function getParentClasses($class) public function getParentClasses($class)
{ {
if ( ! class_exists($class)) {
throw MappingException::nonExistingClass($class);
}

return class_parents($class); return class_parents($class);
} }


Expand Down
Expand Up @@ -39,6 +39,12 @@ public function testGetMetadataFor()
$this->assertTrue($this->cmf->hasMetadataFor('stdClass')); $this->assertTrue($this->cmf->hasMetadataFor('stdClass'));
} }


public function testGetMetadataForAbsentClass()
{
$this->setExpectedException('Doctrine\Common\Persistence\Mapping\MappingException');
$this->cmf->getMetadataFor(__NAMESPACE__ . '\AbsentClass');
}

public function testGetParentMetadata() public function testGetParentMetadata()
{ {
$metadata = $this->cmf->getMetadataFor(__NAMESPACE__ . '\ChildEntity'); $metadata = $this->cmf->getMetadataFor(__NAMESPACE__ . '\ChildEntity');
Expand Down
Expand Up @@ -54,6 +54,12 @@ public function testGetParentClasses()
$this->assertTrue(count($classes) >= 1, "The test class ".__CLASS__." should have at least one parent."); $this->assertTrue(count($classes) >= 1, "The test class ".__CLASS__." should have at least one parent.");
} }


public function testGetParentClassesForAbsentClass()
{
$this->setExpectedException('Doctrine\Common\Persistence\Mapping\MappingException');
$this->reflectionService->getParentClasses(__NAMESPACE__ . '\AbsentClass');
}

public function testGetReflectionClass() public function testGetReflectionClass()
{ {
$class = $this->reflectionService->getClass(__CLASS__); $class = $this->reflectionService->getClass(__CLASS__);
Expand Down

0 comments on commit ce87784

Please sign in to comment.