Skip to content

Commit 93794c1

Browse files
committed
Catch errors better.
1 parent 2f170a1 commit 93794c1

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/Visitor/Php/Symfony/ValidationAnnotation.php

Lines changed: 8 additions & 1 deletion
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 Translation\Extractor\Model\SourceLocation;
@@ -75,7 +76,13 @@ public function enterNode(Node $node)
7576
return;
7677
}
7778

78-
$metadata = $this->metadataFactory->getMetadataFor($name);
79+
try {
80+
$metadata = $this->metadataFactory->getMetadataFor($name);
81+
} catch (AnnotationException $e) {
82+
$this->addError($node, 'Could not parse class "%s" for annotations. %s', $this->namespace, $e->getMessage());
83+
return;
84+
}
85+
7986
if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) {
8087
return;
8188
}

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

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

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

14+
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
1415
use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest;
1516
use Translation\Extractor\Tests\Resources;
1617
use Translation\Extractor\Visitor\Php\Symfony\ValidationAnnotation;
@@ -44,4 +45,14 @@ public function testExtractAnnotation()
4445
$this->assertEquals('end.blank', $source->getMessage());
4546
$this->assertEquals('validators', $source->getContext()['domain']);
4647
}
48+
49+
public function testExtractAnnotationError()
50+
{
51+
$factory = new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader()));
52+
$extractor = new ValidationAnnotation($factory);
53+
$collection = $this->getSourceLocations($extractor, Resources\Php\Symfony\ValidatorAnnotationError::class);
54+
55+
$errors = $collection->getErrors();
56+
$this->assertCount(1, $errors);
57+
}
4758
}
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)