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

src/Visitor/Php/Symfony/FormTypeLabelExplicit.php (2 issues)

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 Rein Baarsma <[email protected]>
19
 */
20
final class FormTypeLabelExplicit extends AbstractFormType implements NodeVisitor
21
{
22
    use FormTrait;
23
24 6
    public function enterNode(Node $node)
25
    {
26 6
        if (!$this->isFormType($node)) {
27 6
            return;
28
        }
29 5
        parent::enterNode($node);
30
31
        /*
32
         * We could have chosen to traverse specifically the buildForm function or ->add()
33
         * we will probably miss some easy to catch instances when the actual array of options
34
         * is provided statically or through another function.
35
         *
36
         * I don't see any disadvantages now to simply parsing arrays and JMSTranslationBundle has
37
         * been doing it like this for quite some time without major problems.
38
         */
39 5
        if (!$node instanceof Node\Expr\Array_) {
40 5
            return;
41
        }
42
43 5
        $labelNode = null;
44 5
        $domain = null;
45 5
        foreach ($node->items as $item) {
46 5
            if (!$item->key instanceof Node\Scalar\String_) {
47 2
                continue;
48
            }
49
50 5 View Code Duplication
            if ('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...
51 2
                if ($item->value instanceof Node\Scalar\String_) {
52 2
                    $domain = $item->value->value;
53 2
                } elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
54 2
                    $domain = false;
55
                }
56
            }
57
58 5
            if ('label' !== $item->key->value) {
59 3
                continue;
60
            }
61
62 5
            if ($item->value instanceof Node\Expr\ConstFetch) {
63
                // This might be boolean "false"
64 3
                if ('false' === $item->value->name->toString()) {
65 2
                    continue;
66
                }
67
            }
68
69 4
            if (!$item->value instanceof Node\Scalar\String_) {
70 2
                $this->addError($item, 'Form label is not a scalar string');
71
72 2
                continue;
73
            }
74
75 4
            $label = $item->value->value;
76 4
            if (empty($label)) {
77 2
                continue;
78
            }
79
80 4
            $labelNode = $item;
81
        }
82
83 5 View Code Duplication
        if ($labelNode && false !== $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...
84 4
            if (null !== $location = $this->getLocation($label, $node->getAttribute('startLine'), $labelNode, ['domain' => $domain])) {
85 4
                $this->lateCollect($location);
86
            }
87
        }
88 5
    }
89
}
90