Completed
Pull Request — master (#93)
by Michele
02:10
created

PhpCode   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 14.47 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 3
dl 11
loc 76
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 11 11 2
D convertString() 0 42 9

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
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
use Gettext\Utils\PhpFunctionsScanner;
7
8
/**
9
 * Class to get gettext strings from php files returning arrays.
10
 */
11
class PhpCode extends Extractor implements ExtractorInterface
12
{
13
    public static $functions = array(
14
        '__' => '__',
15
        '__e' => '__',
16
        'n__' => 'n__',
17
        'n__e' => 'n__',
18
        'p__' => 'p__',
19
        'p__e' => 'p__',
20
    );
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 View Code Duplication
    public static function fromString($string, Translations $translations = null, $file = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
26
    {
27
        if ($translations === null) {
28
            $translations = new Translations();
29
        }
30
31
        $functions = new PhpFunctionsScanner($string);
32
        $functions->saveGettextFunctions(self::$functions, $translations, $file);
33
34
        return $translations;
35
    }
36
37
    /**
38
     * Decodes a T_CONSTANT_ENCAPSED_STRING string.
39
     *
40
     * @param string $value
41
     *
42
     * @return string
43
     */
44
    public static function convertString($value)
45
    {
46
        if ($value[0] === "'" || strpos($value, '$') === false) {
47
            if (strpos($value, '\\') === false) {
48
                return substr($value, 1, -1);
49
            }
50
51
            return eval("return $value;");
52
        }
53
54
        $result = '';
55
        $value = substr($value, 1, -1);
56
57
        while (($p = strpos($value, '\\')) !== false) {
58
            if (!isset($value[$p + 1])) {
59
                break;
60
            }
61
62
            if ($p > 0) {
63
                $result .= substr($value, 0, $p);
64
            }
65
66
            $value = substr($value, $p + 1);
67
            $p = strpos($value, '$');
68
69
            if ($p === false) {
70
                $result .= eval('return "\\'.$value.'";');
71
                $value = '';
72
                break;
73
            }
74
75
            if ($p === 0) {
76
                $result .= '$';
77
                $value = substr($value, 1);
78
            } else {
79
                $result .= eval('return "\\'.substr($value, 0, $p).'";');
80
                $value = substr($value, $p);
81
            }
82
        }
83
84
        return $result.$value;
85
    }
86
}
87