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

src/Visitor/Php/Symfony/FormTypeLabelExplicit.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 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) {
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) {
84 4
            if (null !== $location = $this->getLocation($label, $node->getAttribute('startLine'), $labelNode, ['domain' => $domain])) {
0 ignored issues
show
The variable $label does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
85 4
                $this->lateCollect($location);
86
            }
87
        }
88 5
    }
89
}
90