Completed
Push — master ( 04f40e...f278b5 )
by Tobias
05:43
created

ValidationAnnotation   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 102
Duplicated Lines 7.84 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 7
dl 8
loc 102
c 0
b 0
f 0
ccs 40
cts 42
cp 0.9524
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A beforeTraverse() 0 4 1
C enterNode() 8 41 11
B extractFromConstraints() 0 21 5
A leaveNode() 0 3 1
A afterTraverse() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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, '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...
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) {
0 ignored issues
show
Bug introduced by
The expression $members of type object<Symfony\Component...Mapping\MemberMetadata> is not traversable.
Loading history...
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