Completed
Pull Request — master (#56)
by Robin
07:16
created

FixersGenerator::makeFixersTab()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 5.3846
cc 8
eloc 18
nc 6
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 8.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace SLLH\StyleCIBridge\StyleCI;
4
5
use Symfony\CS\Tokenizer\Token;
6
use Symfony\CS\Tokenizer\Tokens;
7
8
@trigger_error('The '.__NAMESPACE__.'\FixersGenerator class is deprecated since 1.4 and will be removed in 2.0.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
9
10
/**
11
 * @author Sullivan Senechal <[email protected]>
12
 *
13
 * @deprecated since 1.4, will be removed in 2.0.
14
 */
15
final class FixersGenerator
16
{
17
    const STYLE_CI_CLASS_FILE = 'https://github.com/StyleCI/Config/raw/master/src/Config.php';
18
19
    /**
20
     * @var array
21
     */
22
    private $fixersTab = array();
23
24
    /**
25
     * Generate Fixers.php file.
26
     */
27
    public function generate()
28
    {
29
        file_put_contents(__DIR__.'/../StyleCI/Fixers.php', $this->getFixersClass());
30
    }
31
32
    /**
33
     * Generate Fixers.php content.
34
     *
35
     * @return string
36
     */
37
    public function getFixersClass()
38
    {
39
        $twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__.'/..'));
40
41
        $fixersTab = $this->getFixersTab();
42
        $presets = array();
43
        foreach ($fixersTab as $group => $fixers) {
44
            if (strstr($group, '_fixers')) {
45
                array_push($presets, str_replace('_fixers', '', $group));
46
            }
47
        }
48
49
        return $twig->render('StyleCI/Fixers.php.twig', array('fixersTab' => $fixersTab, 'presets' => $presets));
50
    }
51
52
    /**
53
     * Returns fixers tab from StyleCI Config ckass.
54
     *
55
     * @return array
56
     */
57
    public function getFixersTab()
58
    {
59
        $this->makeFixersTab();
60
61
        return $this->fixersTab;
62
    }
63
64
    private function makeFixersTab()
65
    {
66
        $configClass = file_get_contents('https://github.com/StyleCI/Config/raw/master/src/Config.php');
67
68
        /** @var Tokens|Token[] $tokens */
69
        $tokens = Tokens::fromCode($configClass);
70
        /*
71
         * @var int
72
         * @var Token
73
         */
74
        foreach ($tokens->findGivenKind(T_CONST) as $index => $token) {
75
            if ('[' === $tokens[$index + 6]->getContent()) {
76
                $name = strtolower($tokens[$index + 2]->getContent());
77
                $fixers = array();
78
                for ($i = $index + 7; ']' !== $tokens[$i]->getContent(); ++$i) {
0 ignored issues
show
Comprehensibility Bug introduced by
Loop incrementor ($i) jumbling with inner loop
Loading history...
79
                    if ($tokens[$i]->isGivenKind(T_CONSTANT_ENCAPSED_STRING) && ',' === $tokens[$i + 1]->getContent()) {
80
                        // Simple array management
81
                        array_push($fixers, array('name' => $this->getString($tokens[$i]->getContent())));
82
                    } elseif ($tokens[$i]->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) {
83
                        // Double arrow management
84
                        $key = $this->getString($tokens[$i]->getContent());
85
                        for (++$i; $tokens[$i]->isGivenKind(T_DOUBLE_ARROW); ++$i) {
0 ignored issues
show
Unused Code introduced by
This for loop is empty and can be removed.

This check looks for for loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
86
                        }
87
                        $i += 3;
88
                        array_push($fixers, array(
89
                            'key'  => $key,
90
                            'name' => $this->getString($tokens[$i]->getContent()),
91
                        ));
92
                    }
93
                }
94
                $this->fixersTab[$name] = $fixers;
95
            }
96
        }
97
    }
98
99
    /**
100
     * @param string $tokenContent
101
     *
102
     * @return string
103
     */
104
    private function getString($tokenContent)
105
    {
106
        return str_replace(array('"', "'"), '', $tokenContent);
107
    }
108
}
109