Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/Visitor/Php/Symfony/ValidationAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@

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 <tobias.nyholm@gmail.com>
*/
final class ValidationAnnotation extends BasePHPVisitor implements NodeVisitor
{
/**
* @var MetadataFactoryInterface|LegacyMetadataFactoryInterface
* @var MetadataFactoryInterface
*/
private $metadataFactory;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down