Skip to content

Commit fca11f3

Browse files
authored
Merge pull request #53 from php-translation/issue-51
Catch errors better.
2 parents b11a4b1 + 2749f86 commit fca11f3

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/Visitor/Php/Symfony/ValidationAnnotation.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Translation\Extractor\Visitor\Php\Symfony;
1313

14+
use Doctrine\Common\Annotations\AnnotationException;
1415
use PhpParser\Node;
1516
use PhpParser\NodeVisitor;
1617
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -69,8 +70,15 @@ public function enterNode(Node $node)
6970
return;
7071
}
7172

72-
/** @var ClassMetadata $metadata */
73-
$metadata = $this->metadataFactory->getMetadataFor($name);
73+
try {
74+
/** @var ClassMetadata $metadata */
75+
$metadata = $this->metadataFactory->getMetadataFor($name);
76+
} catch (AnnotationException $e) {
77+
$this->addError($node, 'Could not parse class "%s" for annotations. %s', $this->namespace, $e->getMessage());
78+
79+
return;
80+
}
81+
7482
if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) {
7583
return;
7684
}

tests/Functional/Visitor/Php/Symfony/ValidationAnnotationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@ public function testExtractAnnotation()
3838
$this->assertEquals('end.blank', $source->getMessage());
3939
$this->assertEquals('validators', $source->getContext()['domain']);
4040
}
41+
42+
public function testExtractAnnotationError()
43+
{
44+
$factory = new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader()));
45+
$extractor = new ValidationAnnotation($factory);
46+
$collection = $this->getSourceLocations($extractor, Resources\Php\Symfony\ValidatorAnnotationError::class);
47+
48+
$errors = $collection->getErrors();
49+
$this->assertCount(1, $errors);
50+
}
4151
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
4+
5+
use Symfony\Component\Validator\Constraints as Assert;
6+
use Symfony\Component\Validator\Constraints\NotBlank;
7+
8+
class ValidatorAnnotationError
9+
{
10+
/**
11+
* @foobar This should be an Error
12+
*/
13+
private $foo;
14+
}

0 commit comments

Comments
 (0)