Completed
Push — master ( ba40ee...261a9c )
by Tobias
02:47
created

ValidationAnnotation::enterNode()   B

Complexity

Conditions 11
Paths 15

Size

Total Lines 41

Duplication

Lines 8
Ratio 19.51 %

Code Coverage

Tests 19
CRAP Score 11.1043

Importance

Changes 0
Metric Value
dl 8
loc 41
ccs 19
cts 21
cp 0.9048
rs 7.3166
c 0
b 0
f 0
cc 11
nc 15
nop 1
crap 11.1043

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Doctrine\Common\Annotations\AnnotationException;
15
use PhpParser\Node;
16
use PhpParser\NodeVisitor;
17
use Symfony\Component\Validator\Mapping\ClassMetadata;
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 2
    public function __construct(MetadataFactoryInterface $metadataFactory)
42
    {
43 2
        $this->metadataFactory = $metadataFactory;
44 2
    }
45
46 2
    public function beforeTraverse(array $nodes)
47
    {
48 2
        $this->namespace = '';
49 2
    }
50
51 2
    public function enterNode(Node $node)
52
    {
53 2 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 2
            if (isset($node->name)) {
55
                // save the namespace
56 2
                $this->namespace = implode('\\', $node->name->parts);
57
            }
58
59 2
            return;
60
        }
61
62 2
        if (!$node instanceof Node\Stmt\Class_) {
63 2
            return;
64
        }
65
66 2
        $name = '' === $this->namespace ? $node->name : $this->namespace.'\\'.$node->name;
67
68 2
        if (!class_exists($name)) {
69
            return;
70
        }
71
72
        try {
73
            /** @var ClassMetadata $metadata */
74 2
            $metadata = $this->metadataFactory->getMetadataFor($name);
75 2
        } catch (AnnotationException $e) {
76 2
            $this->addError($node, sprintf('Could not parse class "%s" for annotations. %s', $this->namespace, $e->getMessage()));
77
78 2
            return;
79
        }
80
81 1
        if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) {
82
            return;
83
        }
84
85 1
        $this->extractFromConstraints($metadata->constraints);
86 1
        foreach ($metadata->members as $members) {
87 1
            foreach ($members as $member) {
88 1
                $this->extractFromConstraints($member->constraints);
89
            }
90
        }
91 1
    }
92
93
    /**
94
     * @param array $constraints
95
     */
96 1
    private function extractFromConstraints(array $constraints)
97
    {
98 1
        foreach ($constraints as $constraint) {
99 1
            $ref = new \ReflectionClass($constraint);
100 1
            $defaultValues = $ref->getDefaultProperties();
101
102 1
            $properties = $ref->getProperties();
103
104 1
            foreach ($properties as $property) {
105 1
                $propName = $property->getName();
106
107
                // If the property ends with 'Message'
108 1
                if ('message' === strtolower(substr($propName, -1 * strlen('Message')))) {
109
                    // If it is different from the default value
110 1
                    if ($defaultValues[$propName] !== $constraint->{$propName}) {
111 1
                        $this->addLocation($constraint->{$propName}, 0, null, ['domain' => 'validators']);
112
                    }
113
                }
114
            }
115
        }
116 1
    }
117
118 2
    public function leaveNode(Node $node)
119
    {
120 2
    }
121
122 2
    public function afterTraverse(array $nodes)
123
    {
124 2
    }
125
}
126