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

Worker   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 88
Duplicated Lines 7.95 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 74.47%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 6
dl 7
loc 88
ccs 35
cts 47
cp 0.7447
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D work() 0 43 10
A getReadDomainFromArguments() 7 12 3
A getReadDomainFromNode() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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