Completed
Push — v4.0-dev ( b9d887...6e38a6 )
by Oscar
02:13
created

FunctionsScanner::saveGettextFunctions()   D

Complexity

Conditions 22
Paths 38

Size

Total Lines 84
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 84
rs 4.8158
cc 22
eloc 50
nc 38
nop 3

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
namespace Gettext\Utils;
4
5
use Exception;
6
use Gettext\Translations;
7
8
abstract class FunctionsScanner
9
{
10
    /**
11
     * Scan and returns the functions and the arguments.
12
     *
13
     * @return array
14
     */
15
    abstract public function getFunctions();
16
17
    /**
18
     * Search for specific functions and create translations.
19
     *
20
     * @param array        $functions    The gettext functions to search
21
     * @param Translations $translations The translations instance where save the values
22
     * @param string       $file         The filename used to the reference
23
     */
24
    public function saveGettextFunctions(array $functions, Translations $translations, $file = '')
25
    {
26
        foreach ($this->getFunctions() as $function) {
27
            list($name, $line, $args) = $function;
28
29
            if (!isset($functions[$name])) {
30
                continue;
31
            }
32
33
            $domain = $context = $original = $plural = $translation = null;
0 ignored issues
show
Unused Code introduced by
$translation is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
34
35
            switch ($functions[$name]) {
36
                case 'gettext':
37
                    if (!isset($args[0])) {
38
                        continue 2;
39
                    }
40
41
                    $original = $args[0];
42
                    break;
43
44
                case 'ngettext':
45
                    if (!isset($args[1])) {
46
                        continue 2;
47
                    }
48
49
                    list($original, $plural) = $args;
50
                    break;
51
52
                case 'pgettext':
53
                    if (!isset($args[1])) {
54
                        continue 2;
55
                    }
56
57
                    list($context, $original) = $args;
58
                    break;
59
60
                case 'dgettext':
61
                    if (!isset($args[1])) {
62
                        continue 2;
63
                    }
64
65
                    list($domain, $original) = $args;
66
                    break;
67
68
                case 'dpgettext':
69
                    if (!isset($args[2])) {
70
                        continue 2;
71
                    }
72
73
                    list($domain, $context, $original) = $args;
74
                    break;
75
76
                case 'npgettext':
77
                    if (!isset($args[2])) {
78
                        continue 2;
79
                    }
80
81
                    list($context, $original, $plural) = $args;
82
                    break;
83
84
                case 'dnpgettext':
85
                    if (!isset($args[4])) {
86
                        continue 2;
87
                    }
88
89
                    list($domain, $context, $original, $plural) = $args;
90
                    break;
91
92
                default:
93
                    throw new Exception(sprintf('Not valid function %s', $functions[$name]));
94
            }
95
96
            if ((string) $original !== '' && ($domain === null || $domain === $translations->getDomain())) {
97
                $translation = $translations->insert($context, $original, $plural);
98
                $translation->addReference($file, $line);
99
100
                if (isset($function[3])) {
101
                    foreach ($function[3] as $extractedComment) {
102
                        $translation->addExtractedComment($extractedComment);
103
                    }
104
                }
105
            }
106
        }
107
    }
108
}
109