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

src/Visitor/Php/Symfony/FormTypePlaceholder.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 PhpParser\Node;
15
use PhpParser\NodeVisitor;
16
17
/**
18
 * @author Tobias Nyholm <[email protected]>
19
 */
20
final class FormTypePlaceholder extends AbstractFormType implements NodeVisitor
21
{
22
    use FormTrait;
23
24
    private $arrayNodeVisited = [];
25
26 5
    public function enterNode(Node $node)
27
    {
28 5
        if (!$this->isFormType($node)) {
29 5
            return;
30
        }
31 4
        parent::enterNode($node);
32
33 4
        if (!$node instanceof Node\Expr\Array_) {
34 4
            return;
35
        }
36
37 4
        $placeholderNode = null;
38 4
        $domain = null;
39 4
        foreach ($node->items as $item) {
40 4
            if (!$item->key instanceof Node\Scalar\String_) {
41 1
                continue;
42
            }
43 4
            if ('translation_domain' === $item->key->value) {
44
                // Try to find translation domain
45 2
                if ($item->value instanceof Node\Scalar\String_) {
46 2
                    $domain = $item->value->value;
47 2
                } elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
48 2
                    $domain = false;
49
                }
50 4
            } elseif ('placeholder' === $item->key->value) {
51 3
                $placeholderNode = $item;
52 4
            } elseif ('attr' === $item->key->value && $item->value instanceof Node\Expr\Array_) {
53 3
                foreach ($item->value->items as $attrValue) {
54 3
                    if ('placeholder' === $attrValue->key->value) {
55 3
                        $placeholderNode = $attrValue;
56
57 3
                        break;
58
                    }
59
                }
60
            }
61
        }
62
63 4
        if (null === $placeholderNode) {
64 2
            return;
65
        }
66
67
        /**
68
         * Make sure we do not visit the same placeholder node twice.
69
         *
70
         * The placeholder information is not always in the same place:
71
         * * it can be in Type options (for example when using `ChoiceType`)
72
         * * it can be in `attr` (for example when using `TextType`)
73
         *
74
         * @see https://github.com/php-translation/extractor/pull/114#issuecomment-400329507
75
         */
76 3
        $hash = spl_object_hash($placeholderNode);
77 3
        if (isset($this->arrayNodeVisited[$hash])) {
78 3
            return;
79
        }
80 3
        $this->arrayNodeVisited[$hash] = true;
81
82 3
        if ($placeholderNode->value instanceof Node\Scalar\String_) {
83 2
            $line = $placeholderNode->value->getAttribute('startLine');
84 2 View Code Duplication
            if (null !== $location = $this->getLocation($placeholderNode->value->value, $line, $placeholderNode, ['domain' => $domain])) {
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...
85 2
                $this->lateCollect($location);
86
            }
87 2
        } elseif ($placeholderNode->value instanceof Node\Expr\ConstFetch && 'false' === $placeholderNode->value->name->toString()) {
88
            // 'placeholder' => false,
89
            // Do noting
90
        } else {
91 2
            $this->addError($placeholderNode, 'Form placeholder is not a scalar string');
92
        }
93 3
    }
94
}
95