Completed
Push — master ( ba40ee...261a9c )
by Tobias
02:47
created

FormTypeLabelImplicit::enterNode()   D

Complexity

Conditions 28
Paths 65

Size

Total Lines 67

Duplication

Lines 12
Ratio 17.91 %

Code Coverage

Tests 33
CRAP Score 28

Importance

Changes 0
Metric Value
dl 12
loc 67
ccs 33
cts 33
cp 1
rs 4.1666
c 0
b 0
f 0
cc 28
nc 65
nop 1
crap 28

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 FormTypeLabelImplicit extends AbstractFormType implements NodeVisitor
21
{
22
    use FormTrait;
23
24 3
    public function enterNode(Node $node)
25
    {
26 3
        if (!$this->isFormType($node)) {
27 3
            return;
28
        }
29
30 3
        parent::enterNode($node);
31
32 3
        $domain = null;
33
        // use add() function and look at first argument and if that's a string
34 3
        if ($node instanceof Node\Expr\MethodCall
35 3
            && (!is_object($node->name) || method_exists($node->name, '__toString'))
36 3
            && ('add' === (string) $node->name || 'create' === (string) $node->name)
37 3
            && $node->args[0]->value instanceof Node\Scalar\String_) {
38 3
            $skipLabel = false;
39
            // Check if the form type is "hidden"
40 3
            if (count($node->args) >= 2) {
41 3
                $type = $node->args[1]->value;
42 3
                if ($type instanceof Node\Scalar\String_ && 'Symfony\Component\Form\Extension\Core\Type\HiddenType' === $type->value
43 3
                    || $type instanceof Node\Expr\ClassConstFetch && 'HiddenType' === $type->class->parts[0]) {
44 1
                    $skipLabel = true;
45
                }
46
            }
47
48
            // now make sure we don't have 'label' in the array of options
49 3
            if (count($node->args) >= 3) {
50 3
                if ($node->args[2]->value instanceof Node\Expr\Array_) {
51 3
                    foreach ($node->args[2]->value->items as $item) {
0 ignored issues
show
Bug introduced by
The property items does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
52 3
                        if (isset($item->key) && 'label' === $item->key->value) {
53 3
                            $skipLabel = true;
54
                        }
55
56 3 View Code Duplication
                        if (isset($item->key) && 'translation_domain' === $item->key->value) {
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...
57 2
                            if ($item->value instanceof Node\Scalar\String_) {
58 2
                                $domain = $item->value->value;
59 2
                            } elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
60 3
                                $domain = false;
61
                            }
62
                        }
63
                    }
64
                }
65
                /*
66
                 * Actually there's another case here.. if the 3rd argument is anything else, it could well be
67
                 * that label is set through a static array. This will not be a common use-case so yeah in this case
68
                 * it may be the translation is double.
69
                 */
70
            }
71
72
            // only if no custom label was found, proceed
73 3
            if (false === $skipLabel && false !== $domain) {
74
                /*
75
                 * Pass DocComment (if available) from first argument (name of Form field) allowing usage of Ignore
76
                 * annotation to disable implicit add; use case: when form options are generated by external method.
77
                 */
78 3
                if ($node->args[0]->getDocComment()) {
79 2
                    $node->setDocComment($node->args[0]->getDocComment());
0 ignored issues
show
Bug introduced by
It seems like $node->args[0]->getDocComment() can be null; however, setDocComment() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
80
                }
81
82 3
                $label = $node->args[0]->value->value;
0 ignored issues
show
Bug introduced by
The property value does not seem to exist in PhpParser\Node\Expr.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
83 3 View Code Duplication
                if (!empty($label)) {
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...
84 3
                    if (null !== $location = $this->getLocation($label, $node->getAttribute('startLine'), $node, ['domain' => $domain])) {
85 3
                        $this->lateCollect($location);
86
                    }
87
                }
88
            }
89
        }
90 3
    }
91
}
92