FindFilesWithMoreThanOneClass::runActualTask()   C
last analyzed

Complexity

Conditions 14
Paths 10

Size

Total Lines 59
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 42
c 5
b 1
f 0
dl 0
loc 59
rs 6.2666
cc 14
nc 10
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 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.
21
            If any are found than the code exits as you should fix this first!
22
        ';
23
    }
24
25
    public function runActualTask($params = []): ?string
26
    {
27
        $errors = [];
28
        foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) {
0 ignored issues
show
Bug introduced by
It seems like getExistingModuleDirLocations() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        foreach ($this->mu()->/** @scrutinizer ignore-call */ getExistingModuleDirLocations() as $moduleDir) {
Loading history...
29
            $searchPath = $this->mu()->findMyCodeDir($moduleDir);
0 ignored issues
show
Bug introduced by
It seems like findMyCodeDir() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
            $searchPath = $this->mu()->/** @scrutinizer ignore-call */ findMyCodeDir($moduleDir);
Loading history...
30
            if (file_exists($searchPath)) {
31
                $this->mu()->colourPrint(
0 ignored issues
show
Bug introduced by
It seems like colourPrint() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
                $this->mu()->/** @scrutinizer ignore-call */ colourPrint(
Loading history...
32
                    'Searching in ' . $searchPath . ' for files with more than one class.',
33
                    'blue'
34
                );
35
                $fileFinder = new FindFiles();
36
                $flatArray = $fileFinder
37
                    ->setSearchPath($searchPath)
38
                    ->setExtensions(['php'])
39
                    ->getFlatFileArray();
40
                if (is_array($flatArray) && count($flatArray)) {
41
                    foreach ($flatArray as $path) {
42
                        // $className = basename($path, '.php');
43
                        $classNames = [];
44
                        $content = file_get_contents($path);
45
                        $tokens = token_get_all($content);
46
                        for ($index = 0; isset($tokens[$index]); $index++) {
47
                            if (! isset($tokens[$index][0])) {
48
                                continue;
49
                            }
50
                            if ($tokens[$index][0] === T_CLASS &&
51
                                $tokens[$index + 1][0] === T_WHITESPACE &&
52
                                $tokens[$index + 2][0] === T_STRING
53
                            ) {
54
                                $index += 2; // Skip class keyword and whitespace
55
                                $classNames[] = $tokens[$index][1];
56
                            }
57
                        }
58
                        if (count($classNames) > 1) {
59
                            $errors[] = $path . ': ' . implode(', ', $classNames);
60
                        }
61
                    }
62
                } else {
63
                    $this->mu()->colourPrint(
64
                        'Could not find any files in ' . $searchPath,
65
                        'red'
66
                    );
67
                }
68
            } elseif ($searchPath) {
69
                $this->mu()->colourPrint(
70
                    'Could not find the following path: "' . $searchPath,
71
                    'blue'
72
                );
73
            } else {
74
                $this->mu()->colourPrint(
75
                    'empty search path',
76
                    'blue'
77
                );
78
            }
79
        }
80
        if (count($errors)) {
81
            return 'Found files with multiple classes: ' . implode("\n\n ---\n\n", $errors);
82
        }
83
        return null;
84
    }
85
86
    protected function hasCommitAndPush()
87
    {
88
        return false;
89
    }
90
}
91