diff --git a/src/Visitor/Php/Symfony/ValidationAnnotation.php b/src/Visitor/Php/Symfony/ValidationAnnotation.php index a8000cf..de44554 100644 --- a/src/Visitor/Php/Symfony/ValidationAnnotation.php +++ b/src/Visitor/Php/Symfony/ValidationAnnotation.php @@ -13,10 +13,10 @@ use PhpParser\Node; use PhpParser\NodeVisitor; +use Symfony\Component\Validator\Mapping\ClassMetadata; use Translation\Extractor\Model\SourceLocation; use Translation\Extractor\Visitor\Php\BasePHPVisitor; use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; -use Symfony\Component\Validator\MetadataFactoryInterface as LegacyMetadataFactoryInterface; /** * @author Tobias Nyholm @@ -24,7 +24,7 @@ final class ValidationAnnotation extends BasePHPVisitor implements NodeVisitor { /** - * @var MetadataFactoryInterface|LegacyMetadataFactoryInterface + * @var MetadataFactoryInterface */ private $metadataFactory; @@ -36,16 +36,10 @@ final class ValidationAnnotation extends BasePHPVisitor implements NodeVisitor /** * ValidationExtractor constructor. * - * @param MetadataFactoryInterface|LegacyMetadataFactoryInterface $metadataFactory + * @param MetadataFactoryInterface $metadataFactory */ - public function __construct($metadataFactory) + public function __construct(MetadataFactoryInterface $metadataFactory) { - if (!( - $metadataFactory instanceof MetadataFactoryInterface - || $metadataFactory instanceof LegacyMetadataFactoryInterface - )) { - throw new \InvalidArgumentException(sprintf('%s expects an instance of MetadataFactoryInterface', get_class($this))); - } $this->metadataFactory = $metadataFactory; } @@ -75,6 +69,7 @@ public function enterNode(Node $node) return; } + /** @var ClassMetadata $metadata */ $metadata = $this->metadataFactory->getMetadataFor($name); if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) { return; diff --git a/tests/Functional/Visitor/Php/Symfony/ValidationAnnotationTest.php b/tests/Functional/Visitor/Php/Symfony/ValidationAnnotationTest.php index cb4888a..8af0ba4 100644 --- a/tests/Functional/Visitor/Php/Symfony/ValidationAnnotationTest.php +++ b/tests/Functional/Visitor/Php/Symfony/ValidationAnnotationTest.php @@ -11,6 +11,7 @@ namespace Translation\Extractor\Tests\Functional\Visitor\Php\Symfony; +use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory; use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest; use Translation\Extractor\Tests\Resources; use Translation\Extractor\Visitor\Php\Symfony\ValidationAnnotation; @@ -24,14 +25,7 @@ final class ValidationAnnotationTest extends BasePHPVisitorTest { public function testExtractAnnotation() { - //use correct factory class depending on whether using Symfony 2 or 3 - if (class_exists('Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory')) { - $metadataFactoryClass = 'Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory'; - } else { - $metadataFactoryClass = 'Symfony\Component\Validator\Mapping\ClassMetadataFactory'; - } - - $factory = new $metadataFactoryClass(new AnnotationLoader(new AnnotationReader())); + $factory = new LazyLoadingMetadataFactory(new AnnotationLoader(new AnnotationReader())); $extractor = new ValidationAnnotation($factory); $collection = $this->getSourceLocations($extractor, Resources\Php\Symfony\ValidatorAnnotation::class);