Completed
Push — master ( 0fdb96...3393fb )
by Tobias
03:01
created

Worker::work()   D

Complexity

Conditions 10
Paths 5

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 10.7998

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 28
cts 35
cp 0.8
c 0
b 0
f 0
rs 4.8196
cc 10
eloc 29
nc 5
nop 3
crap 10.7998

How to fix   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\Twig;
13
14
use Symfony\Bridge\Twig\Node\TransNode;
15
use Translation\Extractor\Model\SourceCollection;
16
use Translation\Extractor\Model\SourceLocation;
17
18
/**
19
 * The Worker tha actually extract the translations.
20
 *
21
 * @author Tobias Nyholm <[email protected]>
22
 * @author Fabien Potencier <[email protected]>
23
 */
24
final class Worker
25
{
26
    const UNDEFINED_DOMAIN = 'messages';
27
28
    /**
29
     * @param \Twig_Node|\Twig_NodeInterface $node
30
     * @param SourceCollection               $collection
31
     * @param callable                       $getAbsoluteFilePath
32
     *
33
     * @return \Twig_Node|\Twig_NodeInterface
34
     */
35 4
    public function work($node, SourceCollection $collection, callable $getAbsoluteFilePath)
36
    {
37
        if (
38 4
            $node instanceof \Twig_Node_Expression_Filter &&
39 4
            'trans' === $node->getNode('filter')->getAttribute('value') &&
40 2
            $node->getNode('node') instanceof \Twig_Node_Expression_Constant
41 4
        ) {
42
            // extract constant nodes with a trans filter
43 2
            $collection->addLocation(new SourceLocation(
44 2
                $node->getNode('node')->getAttribute('value'),
45 2
                $getAbsoluteFilePath(),
46 2
                $node->getTemplateLine(),
47 2
                ['domain' => $this->getReadDomainFromArguments($node->getNode('arguments'), 1)]
48 2
            ));
49 2
        } elseif (
50 4
            $node instanceof \Twig_Node_Expression_Filter &&
51 4
            'transchoice' === $node->getNode('filter')->getAttribute('value') &&
52
            $node->getNode('node') instanceof \Twig_Node_Expression_Constant
53 4
        ) {
54
            // extract constant nodes with a trans filter
55
            $collection->addLocation(new SourceLocation(
56
                $node->getNode('node')->getAttribute('value'),
57
                $getAbsoluteFilePath(),
58
                $node->getTemplateLine(),
59
                ['domain' => $this->getReadDomainFromArguments($node->getNode('arguments'), 2)]
60
            ));
61 4
        } elseif ($node instanceof TransNode) {
62
            // extract trans nodes
63 3
            $domain = self::UNDEFINED_DOMAIN;
64 3
            if ($node->hasNode('domain') && null !== $node->getNode('domain')) {
65 2
                $domain = $this->getReadDomainFromNode($node->getNode('domain'));
66 2
            }
67
68 3
            $collection->addLocation(new SourceLocation(
69 3
                $node->getNode('body')->getAttribute('data'),
70 3
                $getAbsoluteFilePath(),
71 3
                $node->getTemplateLine(),
72 3
                ['domain' => $domain]
73 3
            ));
74 3
        }
75
76 4
        return $node;
77
    }
78
79
    /**
80
     * @param \Twig_Node $arguments
81
     * @param int        $index
82
     *
83
     * @return string|null
84
     */
85 2
    private function getReadDomainFromArguments(\Twig_Node $arguments, $index)
86
    {
87 2 View Code Duplication
        if ($arguments->hasNode('domain')) {
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...
88
            $argument = $arguments->getNode('domain');
89 2
        } elseif ($arguments->hasNode($index)) {
90
            $argument = $arguments->getNode($index);
91
        } else {
92 2
            return self::UNDEFINED_DOMAIN;
93
        }
94
95
        return $this->getReadDomainFromNode($argument);
96
    }
97
98
    /**
99
     * @param \Twig_Node $node
100
     *
101
     * @return string|null
102
     */
103 2
    private function getReadDomainFromNode(\Twig_Node $node)
104
    {
105 2
        if ($node instanceof \Twig_Node_Expression_Constant) {
106 2
            return $node->getAttribute('value');
107
        }
108
109
        return self::UNDEFINED_DOMAIN;
110
    }
111
}
112