Completed
Push — master ( 96bb84...2f170a )
by Tobias
04:46 queued 01:16
created

ValidationAnnotation::extractFromConstraints()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 1
crap 5
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 Translation\Extractor\Model\SourceLocation;
17
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
18
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
19
use Symfony\Component\Validator\MetadataFactoryInterface as LegacyMetadataFactoryInterface;
20
21
/**
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
final class ValidationAnnotation extends BasePHPVisitor implements NodeVisitor
25
{
26
    /**
27
     * @var MetadataFactoryInterface|LegacyMetadataFactoryInterface
28
     */
29
    private $metadataFactory;
30
31
    /**
32
     * @var string
33
     */
34
    private $namespace;
35
36
    /**
37
     * ValidationExtractor constructor.
38
     *
39
     * @param MetadataFactoryInterface|LegacyMetadataFactoryInterface $metadataFactory
40
     */
41 1
    public function __construct($metadataFactory)
42
    {
43
        if (!(
44
            $metadataFactory instanceof MetadataFactoryInterface
45 1
            || $metadataFactory instanceof LegacyMetadataFactoryInterface
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Valida...etadataFactoryInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
46 1
        )) {
47
            throw new \InvalidArgumentException(sprintf('%s expects an instance of MetadataFactoryInterface', get_class($this)));
48
        }
49 1
        $this->metadataFactory = $metadataFactory;
50 1
    }
51
52 1
    public function beforeTraverse(array $nodes)
53
    {
54 1
        $this->namespace = '';
55 1
    }
56
57 1
    public function enterNode(Node $node)
58
    {
59 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...
60 1
            if (isset($node->name)) {
61
                // save the namespace
62 1
                $this->namespace = implode('\\', $node->name->parts);
63 1
            }
64
65 1
            return;
66
        }
67
68 1
        if (!$node instanceof Node\Stmt\Class_) {
69 1
            return;
70
        }
71
72 1
        $name = '' === $this->namespace ? $node->name : $this->namespace.'\\'.$node->name;
73
74 1
        if (!class_exists($name)) {
75
            return;
76
        }
77
78 1
        $metadata = $this->metadataFactory->getMetadataFor($name);
79 1
        if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) {
80
            return;
81
        }
82
83 1
        $this->extractFromConstraints($metadata->constraints);
84 1
        foreach ($metadata->members as $members) {
85 1
            foreach ($members as $member) {
86 1
                $this->extractFromConstraints($member->constraints);
87 1
            }
88 1
        }
89 1
    }
90
91
    /**
92
     * @param array $constraints
93
     */
94 1
    private function extractFromConstraints(array $constraints)
95
    {
96 1
        foreach ($constraints as $constraint) {
97 1
            $ref = new \ReflectionClass($constraint);
98 1
            $defaultValues = $ref->getDefaultProperties();
99
100 1
            $properties = $ref->getProperties();
101
102 1
            foreach ($properties as $property) {
103 1
                $propName = $property->getName();
104
105
                // If the property ends with 'Message'
106 1
                if (strtolower(substr($propName, -1 * strlen('Message'))) === 'message') {
107
                    // If it is different from the default value
108 1
                    if ($defaultValues[$propName] !== $constraint->{$propName}) {
109 1
                        $this->collection->addLocation(new SourceLocation($constraint->{$propName}, $this->getAbsoluteFilePath(), 0, ['domain' => 'validators']));
110 1
                    }
111 1
                }
112 1
            }
113 1
        }
114 1
    }
115
116 1
    public function leaveNode(Node $node)
117
    {
118 1
    }
119
120 1
    public function afterTraverse(array $nodes)
121
    {
122 1
    }
123
}
124