Completed
Push — master ( b11a4b...fca11f )
by Tobias
02:04
created

ValidationAnnotation::enterNode()   C

Complexity

Conditions 11
Paths 15

Size

Total Lines 41
Code Lines 21

Duplication

Lines 8
Ratio 19.51 %

Code Coverage

Tests 22
CRAP Score 11.0699

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 41
ccs 22
cts 24
cp 0.9167
rs 5.2653
cc 11
eloc 21
nc 15
nop 1
crap 11.0699

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\Model\SourceLocation;
19
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
20
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
21
22
/**
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
final class ValidationAnnotation extends BasePHPVisitor implements NodeVisitor
26
{
27
    /**
28
     * @var MetadataFactoryInterface
29
     */
30
    private $metadataFactory;
31
32
    /**
33
     * @var string
34
     */
35
    private $namespace;
36
37
    /**
38
     * ValidationExtractor constructor.
39
     *
40
     * @param MetadataFactoryInterface $metadataFactory
41
     */
42 2
    public function __construct(MetadataFactoryInterface $metadataFactory)
43
    {
44 2
        $this->metadataFactory = $metadataFactory;
45 2
    }
46
47 2
    public function beforeTraverse(array $nodes)
48
    {
49 2
        $this->namespace = '';
50 2
    }
51
52 2
    public function enterNode(Node $node)
53
    {
54 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...
55 2
            if (isset($node->name)) {
56
                // save the namespace
57 2
                $this->namespace = implode('\\', $node->name->parts);
58 2
            }
59
60 2
            return;
61
        }
62
63 2
        if (!$node instanceof Node\Stmt\Class_) {
64 2
            return;
65
        }
66
67 2
        $name = '' === $this->namespace ? $node->name : $this->namespace.'\\'.$node->name;
68
69 2
        if (!class_exists($name)) {
70
            return;
71
        }
72
73
        try {
74
            /** @var ClassMetadata $metadata */
75 2
            $metadata = $this->metadataFactory->getMetadataFor($name);
76 2
        } catch (AnnotationException $e) {
77 2
            $this->addError($node, 'Could not parse class "%s" for annotations. %s', $this->namespace, $e->getMessage());
0 ignored issues
show
Unused Code introduced by
The call to ValidationAnnotation::addError() has too many arguments starting with $this->namespace.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
78
79 2
            return;
80
        }
81
82 1
        if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) {
83
            return;
84
        }
85
86 1
        $this->extractFromConstraints($metadata->constraints);
87 1
        foreach ($metadata->members as $members) {
88 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...
89 1
                $this->extractFromConstraints($member->constraints);
90 1
            }
91 1
        }
92 1
    }
93
94
    /**
95
     * @param array $constraints
96
     */
97 1
    private function extractFromConstraints(array $constraints)
98
    {
99 1
        foreach ($constraints as $constraint) {
100 1
            $ref = new \ReflectionClass($constraint);
101 1
            $defaultValues = $ref->getDefaultProperties();
102
103 1
            $properties = $ref->getProperties();
104
105 1
            foreach ($properties as $property) {
106 1
                $propName = $property->getName();
107
108
                // If the property ends with 'Message'
109 1
                if (strtolower(substr($propName, -1 * strlen('Message'))) === 'message') {
110
                    // If it is different from the default value
111 1
                    if ($defaultValues[$propName] !== $constraint->{$propName}) {
112 1
                        $this->collection->addLocation(new SourceLocation($constraint->{$propName}, $this->getAbsoluteFilePath(), 0, ['domain' => 'validators']));
113 1
                    }
114 1
                }
115 1
            }
116 1
        }
117 1
    }
118
119 2
    public function leaveNode(Node $node)
120
    {
121 2
    }
122
123 2
    public function afterTraverse(array $nodes)
124
    {
125 2
    }
126
}
127