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

FindFilesWithMoreThanOneClass::hasCommitAndPush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 FindFilesWithMoreThanOneClass extends Task
9
{
10
    protected $taskStep = 's10';
11
12
    public function getTitle()
13
    {
14
        return 'Finds files with more than one class';
15
    }
16
17
    public function getDescription()
18
    {
19
        return '
20
            Goes through all the PHP files and makes sure that only one class is defined.  If any are found than the code exits as you should fix this first!';
21
    }
22
23
    public function runActualTask($params = [])
24
    {
25
        foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) {
26
            $errors = [];
27
            $searchPath = $this->mu()->findMyCodeDir($moduleDir);
28
            if (file_exists($searchPath)) {
29
                $this->mu()->colourPrint('Searching in ' . $searchPath, 'blue for files with more than one class.');
30
                $fileFinder = new FindFiles($searchPath);
31
                $flatArray = $fileFinder
32
                    ->setSearchPath($searchPath)
33
                    ->setExtensions(['php'])
34
                    ->getFlatFileArray();
35
                if (is_array($flatArray) && count($flatArray)) {
36
                    foreach ($flatArray as $path) {
37
                        $className = basename($path, '.php');
0 ignored issues
show
Unused Code introduced by
The assignment to $className is dead and can be removed.
Loading history...
38
                        $classNames = [];
39
                        $content = file_get_contents($path);
40
                        $tokens = token_get_all($content);
41
                        $namespace = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $namespace is dead and can be removed.
Loading history...
42
                        for ($index = 0; isset($tokens[$index]); $index++) {
43
                            if (! isset($tokens[$index][0])) {
44
                                continue;
45
                            }
46
                            if ($tokens[$index][0] === T_CLASS && $tokens[$index + 1][0] === T_WHITESPACE && $tokens[$index + 2][0] === T_STRING) {
47
                                $index += 2; // Skip class keyword and whitespace
48
                                $classNames[] = $tokens[$index][1];
49
                            }
50
                        }
51
                        if (count($classNames) > 1) {
52
                            $errors[] = $path . ': ' . implode(', ', $classNames);
53
                        }
54
                    }
55
                } else {
56
                    $this->mu()->colourPrint('Could not find any files in ' . $searchPath, 'red');
57
                }
58
            } else {
59
                $this->mu()->colourPrint('Could not find ' . $searchPath, 'blue');
60
            }
61
        }
62
        if (count($errors)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $errors seems to be defined by a foreach iteration on line 25. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
63
            return 'Found files with multiple classes: ' . implode("\n\n ---\n\n", $errors);
64
        }
65
    }
66
67
    protected function hasCommitAndPush()
68
    {
69
        return false;
70
    }
71
}
72