Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Improve code on loadMetadata() to verify if class exists #274

Merged
merged 4 commits into from

3 participants

@entering

Improve code on loadMetadata() to verify if class exists, avoid a later warning when calling class_parents()

Instead throws a \RuntimeException if class doesn't exists.

Before changing code, just after add test:

1) Doctrine\Tests\Common\Persistence\Mapping\ClassMetadataFactoryTest::testGetMetadataForAbsentClass
class_parents(): Class Doctrine\Tests\Common\Persistence\Mapping\AbsentClass does not exist and could not be loaded

/home/www/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php:38
/home/www/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:257
/home/www/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:281
/home/www/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:212
/home/www/common/tests/Doctrine/Tests/Common/Persistence/Mapping/ClassMetadataFactoryTest.php:44
entering added some commits
@entering entering Improve code on loadMetadata() to verify if class exists, avoid a lat…
…er warning when calling class_parents()

Instead throws a \RuntimeException if class doesn't exists.
68a40cc
@entering entering Improve code to fulfill coding standards. 9ac67af
.../Persistence/Mapping/AbstractClassMetadataFactory.php
@@ -272,6 +272,12 @@ protected function getParentClasses($name)
*/
protected function loadMetadata($name)
{
+ if ( !class_exists($name) ) {
+ throw new \RuntimeException(
@Ocramius Owner
Ocramius added a note

Can you use an exception from the common namespace?

@entering
entering added a note

I can find the following exceptions: CommonException, MappingException, InvalidArgumentException, UnexpectedValueException.php. I can see this exception being a RuntimeException (but there is no RuntimeException on common namespace) or an InvalidArgumentException, the name of the class should be a valid class, CommonException looks too generic exception too me (it extends \Exception).

InvalidArgumentException from common namespace sounds good to you?

@Ocramius After you opinion I can change the code and update the phpdoc with @throws notation that I forgot.

@Ocramius Owner
Ocramius added a note

Looks more like a mapping exception to me - mappings cannot be retrieved here, and the argument is not given by the user itself.

Also, move the message generation to a static method of the exception class:

public static function nonExistingClass($className) 
{
    return new self(sprintf(...));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
.../Persistence/Mapping/AbstractClassMetadataFactory.php
@@ -272,6 +272,12 @@ protected function getParentClasses($name)
*/
protected function loadMetadata($name)
{
+ if ( !class_exists($name) ) {
@Ocramius Owner
Ocramius added a note

CS:

if ( ! class_exists($name)) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...mmon/Persistence/Mapping/ClassMetadataFactoryTest.php
@@ -39,6 +39,14 @@ public function testGetMetadataFor()
$this->assertTrue($this->cmf->hasMetadataFor('stdClass'));
}
+ /**
+ * @expectedException \RuntimeException
@Ocramius Owner
Ocramius added a note

Use setExpectedException please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@entering entering Actions done:
- Change exception to MappingException
- Change test to use setExpectedException instead of @expectedException
- Update phpdoc of loadMetadata and fix CS
b8479cb
@entering

@Ocramius I changed my code according your suggestions.

.../Persistence/Mapping/AbstractClassMetadataFactory.php
@@ -20,7 +20,8 @@
namespace Doctrine\Common\Persistence\Mapping;
use Doctrine\Common\Cache\Cache,
@Ocramius Owner
Ocramius added a note

PSR-2 compliant would be:

use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\Common\Persistence\Mapping\MappingException;

So 1 use statement per line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
.../Persistence/Mapping/AbstractClassMetadataFactory.php
@@ -268,10 +269,16 @@ protected function getParentClasses($name)
*
* @param string $name The name of the class for which the metadata should get loaded.
*
+ * @throws MappingException
@Ocramius Owner
Ocramius added a note

Use the FQCN here: @throws \Doctrine\Common\Persistence\Mapping\MappingException

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...trine/Common/Persistence/Mapping/MappingException.php
@@ -83,4 +83,13 @@ public static function invalidMappingFile($entityName, $fileName)
{
return new self("Invalid mapping file '$fileName' for class '$entityName'.");
}
+
+ /**
+ * @param string $className
+ * @return MappingException
@Ocramius Owner
Ocramius added a note

Use the FQCN here: @return \Doctrine\Common\Persistence\Mapping\MappingException

Also, one empty newline required between params and return blocks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Ocramius
Owner

@entering fix the last CS, imo it's :+1: then

@entering

@Ocramius fixed the CS. I was trying to follow the already existing code and I checked: http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/coding-standards.html and didn't see any mention to PSR-*, probably that documentation needs an update.

@Ocramius
Owner
@entering

Google tricks me all the time when searching doctrine documentation, I searched "doctrine 2 coding standard" first link was that and I didn't notice was version 1 related. I found this now in a new search: http://www.doctrine-project.org/jira/browse/DDC-585

@Ocramius Ocramius merged commit 32c54b7 into doctrine:master

1 check failed

Details default The Travis CI build could not complete due to an error
@beberlei
Owner

I had to revert this, because ClassMetadata#loadMetadata can be called, when the classes don't exist.

@beberlei
Owner

If you want to try a new PR, see 7eea788 for a description of the problem and a possible solution.

@entering

@beberlei I can open a new PR, but before I would like if placing the class_exists and throw mapping exception at RuntimeReflectionService getParentClasses(), sounds good to you?

@beberlei
Owner

@entering yes that works :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 7, 2013
  1. @entering

    Improve code on loadMetadata() to verify if class exists, avoid a lat…

    entering authored
    …er warning when calling class_parents()
    
    Instead throws a \RuntimeException if class doesn't exists.
  2. @entering
Commits on May 8, 2013
  1. @entering

    Actions done:

    entering authored
    - Change exception to MappingException
    - Change test to use setExpectedException instead of @expectedException
    - Update phpdoc of loadMetadata and fix CS
  2. @entering

    Improve CS

    entering authored
This page is out of date. Refresh to see the latest.
View
11 lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php
@@ -19,8 +19,9 @@
namespace Doctrine\Common\Persistence\Mapping;
-use Doctrine\Common\Cache\Cache,
- Doctrine\Common\Util\ClassUtils;
+use Doctrine\Common\Cache\Cache;
+use Doctrine\Common\Util\ClassUtils;
+use Doctrine\Common\Persistence\Mapping\MappingException;
/**
* The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
@@ -268,10 +269,16 @@ protected function getParentClasses($name)
*
* @param string $name The name of the class for which the metadata should get loaded.
*
+ * @throws \Doctrine\Common\Persistence\Mapping\MappingException
+ *
* @return array
*/
protected function loadMetadata($name)
{
+ if ( ! class_exists($name)) {
+ throw MappingException::nonExistingClass($name);
+ }
+
if ( ! $this->initialized) {
$this->initialize();
}
View
10 lib/Doctrine/Common/Persistence/Mapping/MappingException.php
@@ -83,4 +83,14 @@ public static function invalidMappingFile($entityName, $fileName)
{
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");
+ }
}
View
6 tests/Doctrine/Tests/Common/Persistence/Mapping/ClassMetadataFactoryTest.php
@@ -39,6 +39,12 @@ public function testGetMetadataFor()
$this->assertTrue($this->cmf->hasMetadataFor('stdClass'));
}
+ public function testGetMetadataForAbsentClass()
+ {
+ $this->setExpectedException('Doctrine\Common\Persistence\Mapping\MappingException');
+ $this->cmf->getMetadataFor(__NAMESPACE__ . '\AbsentClass');
+ }
+
public function testGetParentMetadata()
{
$metadata = $this->cmf->getMetadataFor(__NAMESPACE__ . '\ChildEntity');
Something went wrong with that request. Please try again.