Completed
Push — master ( 2f170a...b11a4b )
by Tobias
04:50
created

ValidationAnnotation::leaveNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Extractor\Visitor\Php\Symfony;
13
14
use PhpParser\Node;
15
use PhpParser\NodeVisitor;
16
use Symfony\Component\Validator\Mapping\ClassMetadata;
17
use Translation\Extractor\Model\SourceLocation;
18
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
19
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
20
21
/**
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
final class ValidationAnnotation extends BasePHPVisitor implements NodeVisitor
25
{
26
    /**
27
     * @var MetadataFactoryInterface
28
     */
29
    private $metadataFactory;
30
31
    /**
32
     * @var string
33
     */
34
    private $namespace;
35
36
    /**
37
     * ValidationExtractor constructor.
38
     *
39
     * @param MetadataFactoryInterface $metadataFactory
40
     */
41 1
    public function __construct(MetadataFactoryInterface $metadataFactory)
42
    {
43 1
        $this->metadataFactory = $metadataFactory;
44 1
    }
45
46 1
    public function beforeTraverse(array $nodes)
47
    {
48 1
        $this->namespace = '';
49 1
    }
50
51 1
    public function enterNode(Node $node)
52
    {
53 1 View Code Duplication
        if ($node instanceof Node\Stmt\Namespace_) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54 1
            if (isset($node->name)) {
55
                // save the namespace
56 1
                $this->namespace = implode('\\', $node->name->parts);
57 1
            }
58
59 1
            return;
60
        }
61
62 1
        if (!$node instanceof Node\Stmt\Class_) {
63 1
            return;
64
        }
65
66 1
        $name = '' === $this->namespace ? $node->name : $this->namespace.'\\'.$node->name;
67
68 1
        if (!class_exists($name)) {
69
            return;
70
        }
71
72
        /** @var ClassMetadata $metadata */
73 1
        $metadata = $this->metadataFactory->getMetadataFor($name);
74 1
        if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) {
75
            return;
76
        }
77
78 1
        $this->extractFromConstraints($metadata->constraints);
79 1
        foreach ($metadata->members as $members) {
80 1
            foreach ($members as $member) {
0 ignored issues
show
Bug introduced by
The expression $members of type object<Symfony\Component...Mapping\MemberMetadata> is not traversable.
Loading history...
81 1
                $this->extractFromConstraints($member->constraints);
82 1
            }
83 1
        }
84 1
    }
85
86
    /**
87
     * @param array $constraints
88
     */
89 1
    private function extractFromConstraints(array $constraints)
90
    {
91 1
        foreach ($constraints as $constraint) {
92 1
            $ref = new \ReflectionClass($constraint);
93 1
            $defaultValues = $ref->getDefaultProperties();
94
95 1
            $properties = $ref->getProperties();
96
97 1
            foreach ($properties as $property) {
98 1
                $propName = $property->getName();
99
100
                // If the property ends with 'Message'
101 1
                if (strtolower(substr($propName, -1 * strlen('Message'))) === 'message') {
102
                    // If it is different from the default value
103 1
                    if ($defaultValues[$propName] !== $constraint->{$propName}) {
104 1
                        $this->collection->addLocation(new SourceLocation($constraint->{$propName}, $this->getAbsoluteFilePath(), 0, ['domain' => 'validators']));
105 1
                    }
106 1
                }
107 1
            }
108 1
        }
109 1
    }
110
111 1
    public function leaveNode(Node $node)
112 1
    {
113 1
    }
114
115 1
    public function afterTraverse(array $nodes)
116
    {
117 1
    }
118
}
119