Passed
Push — master ( f01d00...2f7647 )
by Nicolaas
03:27
created

FindFilesWithSimpleUseStatements::runActualTask()   C

Complexity

Conditions 16
Paths 12

Size

Total Lines 55
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 42
c 3
b 0
f 0
dl 0
loc 55
rs 5.5666
cc 16
nc 12
nop 1

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 Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Api\FindFiles;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
7
8
class FindFilesWithSimpleUseStatements extends Task
9
{
10
    protected $taskStep = 's60';
11
12
    protected $listOfOKOnes = [
13
        'Page',
14
        'PageController',
15
    ];
16
17
    public function getTitle()
18
    {
19
        return 'Finds files simple use statements (may indicate error!)';
20
    }
21
22
    public function getDescription()
23
    {
24
        return '
25
            Goes through all the PHP files and makes sure that there are no simple use statements, apart from things like use \\page;. ';
26
    }
27
28
    public function runActualTask($params = [])
29
    {
30
        $errors = [];
31
        foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) {
32
            $this->mu()->colourPrint('Searching ' . $moduleDir, 'grey');
33
            $fileFinder = new FindFiles($moduleDir);
34
            $searchPath = $this->mu()->findMyCodeDir($moduleDir);
35
            if (file_exists($searchPath)) {
36
                $flatArray = $fileFinder
37
                    ->setSearchPath($searchPath)
38
                    ->setExtensions(['php'])
39
                    ->getFlatFileArray();
40
                if (is_array($flatArray) && count($flatArray)) {
41
                    foreach ($flatArray as $path) {
42
                        $this->mu()->colourPrint('Searching ' . $path, 'grey');
43
                        $className = basename($path, '.php');
0 ignored issues
show
Unused Code introduced by
The assignment to $className is dead and can be removed.
Loading history...
44
                        $classNames = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $classNames is dead and can be removed.
Loading history...
45
                        $content = file_get_contents($path);
46
                        $tokens = token_get_all($content);
47
                        $namespace = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $namespace is dead and can be removed.
Loading history...
48
                        for ($index = 0; isset($tokens[$index]); $index++) {
49
                            if (! isset($tokens[$index][0])) {
50
                                continue;
51
                            }
52
                            if ($tokens[$index][0] === T_USE &&
53
                                $tokens[$index + 1][0] === T_WHITESPACE &&
54
                                $tokens[$index + 2][0] === T_STRING &&
55
                                $tokens[$index + 3] === ';'
56
                            ) {
57
                                $string = $tokens[$index + 2][1];
58
                                if (! in_array($string, $this->listOfOKOnes, true)) {
59
                                    $testPhrase = ltrim($string, '\\');
60
                                    if (! strpos($testPhrase, '\\')) {
61
                                        $errors[] = $path . ': ' . $tokens[$index][1] . $tokens[$index + 1][1] . $tokens[$index + 2][1] . ';';
62
                                    }
63
                                }
64
                                $index += 3; // Skip checked ones ...
65
                            }
66
                        }
67
                    }
68
                } else {
69
                    $this->mu()->colourPrint('Could not find any files in ' . $searchPath, 'red');
70
                }
71
            } else {
72
                $this->mu()->colourPrint('Could not find ' . $searchPath, 'blue');
73
            }
74
        }
75
        if (count($errors)) {
76
            $error = 'Found errors in use statements: ' . "\n---\n---\n---\n" . implode("\n ---\n", $errors);
77
            if (count($errors) > 10) {
78
                return $error;
79
            }
80
            $this->mu()->colourPrint($error, 'red');
81
        } else {
82
            $this->mu()->colourPrint('Clean bill of health in terms of use statements.', 'green');
83
        }
84
    }
85
86
    protected function hasCommitAndPush()
87
    {
88
        return false;
89
    }
90
91
    protected function testme()
92
    {
93
        // $string = "<?php
94
        // echo 'xxx';";
95
        // /* Use tab and newline as tokenizing characters as well  */
96
        // $tok = token_get_all($string);
97
        //
98
        // for ($index = 0; isset($tok[$index]); $index++) {
99
        //     print_r($tok[$index]);
100
        //     echo '-----';
101
        // }
102
        // die('xxx');
103
    }
104
}
105