Completed
Push — master ( 0a4015...907089 )
by Tobias
01:30
created

src/Visitor/Php/Symfony/FormTypeChoices.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\DocParser;
15
use PhpParser\Node;
16
use PhpParser\NodeVisitor;
17
use Translation\Extractor\Annotation\Ignore;
18
use Translation\Extractor\Model\SourceLocation;
19
20
/**
21
 * @author Rein Baarsma <[email protected]>
22
 */
23
final class FormTypeChoices extends AbstractFormType implements NodeVisitor
24
{
25
    use FormTrait;
26
27
    /**
28
     * @var int defaults to major version 3
29
     */
30
    protected $symfonyMajorVersion = 3;
31
32
    private $variables = [];
33
34
    private $state;
35
36
    /**
37
     * @param int $sfMajorVersion
38
     */
39 2
    public function setSymfonyMajorVersion($sfMajorVersion)
40
    {
41 2
        $this->symfonyMajorVersion = $sfMajorVersion;
42 2
    }
43
44 8
    public function enterNode(Node $node)
45
    {
46 8
        if (!$this->isFormType($node)) {
47 8
            return;
48
        }
49
50 8
        parent::enterNode($node);
51
52 8
        if (null === $this->state && $node instanceof Node\Expr\Assign) {
53 4
            $this->state = 'variable';
54 8
        } elseif ('variable' === $this->state && $node instanceof Node\Expr\Variable) {
55 4
            $this->variables['__variable-name'] = $node->name;
56 4
            $this->state = 'value';
57 8
        } elseif ('value' === $this->state && $node instanceof Node\Expr\Array_) {
58 2
            $this->variables[$this->variables['__variable-name']] = $node;
59 2
            $this->state = null;
60
        } else {
61 8
            $this->state = null;
62
        }
63
64
        // symfony 3 or 4 displays key by default, where symfony 2 displays value
65 8
        $useKey = 2 !== $this->symfonyMajorVersion;
66
67
        // remember choices in this node
68 8
        $choicesNodes = [];
69
70
        // loop through array
71 8
        if (!$node instanceof Node\Expr\Array_) {
72 8
            return;
73
        }
74
75 8
        $domain = null;
76 8
        foreach ($node->items as $item) {
77 8
            if (!$item->key instanceof Node\Scalar\String_) {
78 3
                continue;
79
            }
80
81 8
            if ('choices_as_values' === $item->key->value) {
82 2
                $useKey = true;
83
84 2
                continue;
85
            }
86
87 8 View Code Duplication
            if ('choice_translation_domain' === $item->key->value) {
0 ignored issues
show
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...
88 2
                if ($item->value instanceof Node\Scalar\String_) {
89 2
                    $domain = $item->value->value;
90 2
                } elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
91 2
                    $domain = false;
92
                }
93
94 2
                continue;
95
            }
96
97 8
            if ('choices' !== $item->key->value) {
98 7
                continue;
99
            }
100
101
            //do not parse choices if the @Ignore annotation is attached
102 7
            if ($this->isIgnored($item->key)) {
103 1
                continue;
104
            }
105
106 7
            $choicesNodes[] = $item->value;
107
        }
108
109 8
        if (0 === count($choicesNodes) || false === $domain) {
110 8
            return;
111
        }
112
113
        // probably will be only 1, but who knows
114 7
        foreach ($choicesNodes as $choices) {
115 7
            if ($choices instanceof Node\Expr\Variable && isset($this->variables[$choices->name])) {
116 2
                $choices = $this->variables[$choices->name];
117 6
            } elseif (!$choices instanceof Node\Expr\Array_) {
118
                $this->addError($choices, 'Form choice is not an array');
119
120
                continue;
121
            }
122
123 7
            foreach ($choices->items as $citem) {
124 7
                $labelNode = $useKey ? $citem->key : $citem->value;
125 7
                if (!$labelNode instanceof Node\Scalar\String_) {
126 3
                    $this->addError($citem, 'Choice label is not a scalar string');
127
128 3
                    continue;
129
                }
130
131 6
                $this->lateCollect(new SourceLocation($labelNode->value, $this->getAbsoluteFilePath(), $choices->getAttribute('startLine'), ['domain' => $domain]));
132
            }
133
        }
134 7
    }
135
136
    /**
137
     * @param Node $node
138
     *
139
     * @return bool
140
     */
141 7
    protected function isIgnored(Node $node)
142
    {
143
        //because of getDocParser method is private, we have to create a new custom instance
144 7
        $docParser = new DocParser();
145 7
        $docParser->setImports([
146 7
            'ignore' => Ignore::class,
147
        ]);
148 7
        $docParser->setIgnoreNotImportedAnnotations(true);
149 7
        if (null !== $docComment = $node->getDocComment()) {
150 1
            foreach ($docParser->parse($docComment->getText()) as $annotation) {
151 1
                if ($annotation instanceof Ignore) {
152 1
                    return true;
153
                }
154
            }
155
        }
156
157 7
        return false;
158
    }
159
}
160