Completed
Push — master ( c5d58c...e2df10 )
by Olivier
02:35 queued 13s
created

FormTypeTitle::enterNode()   C

Complexity

Conditions 17
Paths 42

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 17.0097

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 30
cts 31
cp 0.9677
rs 5.2166
c 0
b 0
f 0
cc 17
nc 42
nop 1
crap 17.0097

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 FormTypeTitle extends AbstractFormType implements NodeVisitor
21
{
22
    use FormTrait;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 5
    public function enterNode(Node $node): ?Node
28
    {
29 5
        if (!$this->isFormType($node)) {
30 5
            return null;
31
        }
32
33 4
        parent::enterNode($node);
34
35 4
        if (!$node instanceof Node\Expr\Array_) {
36 4
            return null;
37
        }
38
39 4
        $titleNode = null;
40 4
        $domain = null;
41 4
        foreach ($node->items as $item) {
42 4
            if (!$item->key instanceof Node\Scalar\String_) {
43 1
                continue;
44
            }
45 4
            if ('translation_domain' === $item->key->value) {
46
                // Try to find translation domain
47 2
                if ($item->value instanceof Node\Scalar\String_) {
48 2
                    $domain = $item->value->value;
49 2
                } elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) {
50 2
                    $domain = false;
51
                }
52 4
            } elseif ('attr' === $item->key->value && $item->value instanceof Node\Expr\Array_) {
53 3
                foreach ($item->value->items as $attrValue) {
54 3
                    if (!$attrValue->key instanceof Node\Scalar\String_) {
55
                        continue;
56
                    }
57 3
                    if ('title' === $attrValue->key->value) {
58 3
                        $titleNode = $attrValue;
59
60 3
                        break;
61
                    }
62
                }
63
            }
64
        }
65
66 4
        if (null === $titleNode) {
67 4
            return null;
68
        }
69
70 3
        if ($titleNode->value instanceof Node\Scalar\String_) {
71 2
            $line = $titleNode->value->getAttribute('startLine');
72 2
            if (null !== $location = $this->getLocation($titleNode->value->value, $line, $titleNode, ['domain' => $domain])) {
73 2
                $this->lateCollect($location);
74
            }
75
        } else {
76 2
            $this->addError($titleNode, 'Form field title is not a scalar string');
77
        }
78
79 3
        return null;
80
    }
81
}
82