Completed
Push — master ( fe9fab...88aa52 )
by Tobias
02:48
created

FormTypePlaceholder::enterNode()   C

Complexity

Conditions 19
Paths 62

Size

Total Lines 60
Code Lines 35

Duplication

Lines 5
Ratio 8.33 %

Code Coverage

Tests 35
CRAP Score 19

Importance

Changes 0
Metric Value
dl 5
loc 60
ccs 35
cts 35
cp 1
rs 6.2284
c 0
b 0
f 0
cc 19
eloc 35
nc 62
nop 1
crap 19

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 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 View Code Duplication
                if ($item->value instanceof Node\Scalar\String_) {
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...
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) {
53 3
                foreach ($item->value->items as $attrValue) {
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...
54 3
                    if ('placeholder' === $attrValue->key->value) {
55 3
                        $placeholderNode = $attrValue;
56
57 4
                        break;
58
                    }
59
                }
60
            }
61
        }
62
63 4
        if (null !== $placeholderNode) {
64
            /**
65
             * Make sure we do not visit the same placeholder node twice.
66
             */
67 3
            $hash = spl_object_hash($placeholderNode);
68 3
            if (isset($this->arrayNodeVisited[$hash])) {
69 3
                return;
70
            }
71 3
            $this->arrayNodeVisited[$hash] = true;
72
73 3
            if ($placeholderNode->value instanceof Node\Scalar\String_) {
74 2
                $line = $placeholderNode->value->getAttribute('startLine');
75 2
                if (null !== $location = $this->getLocation($placeholderNode->value->value, $line, $placeholderNode, ['domain' => $domain])) {
76 2
                    $this->lateCollect($location);
77
                }
78 2
            } elseif ($placeholderNode->value instanceof Node\Expr\ConstFetch && 'false' === $placeholderNode->value->name->toString()) {
0 ignored issues
show
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
79
                // 'placeholder' => false,
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
                // Do noting
81
            } else {
82 2
                $this->addError($placeholderNode, 'Form placeholder is not a scalar string');
83
            }
84
        }
85 4
    }
86
}
87