AddTableNamePrivateStatic   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 44
c 2
b 0
f 0
dl 0
loc 71
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 3 1
A getDescription() 0 3 1
B runActualTask() 0 42 10
A hasCommitAndPush() 0 3 1
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 AddTableNamePrivateStatic extends Task
9
{
10
    protected $taskStep = 's10';
11
12
    protected $listToSearchFor = [
13
        'private static $db =',
14
        'private static $has_one =',
15
        'private static $belongs_to =',
16
        'private static $has_many =',
17
        'private static $many_many =',
18
        'private static $belongs_many_many =',
19
    ];
20
21
    public function getTitle()
22
    {
23
        return 'Add private static table_name';
24
    }
25
26
    public function getDescription()
27
    {
28
        return '
29
            Adds a private static variable called "table_name" to any class that looks like a data object.';
30
    }
31
32
    public function runActualTask($params = []): ?string
33
    {
34
        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

34
        foreach ($this->mu()->/** @scrutinizer ignore-call */ getExistingModuleDirLocations() as $moduleDir) {
Loading history...
35
            $fileFinder = new FindFiles();
36
            $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

36
            $searchPath = $this->mu()->/** @scrutinizer ignore-call */ findMyCodeDir($moduleDir);
Loading history...
37
            if (file_exists($searchPath)) {
38
                $flatArray = $fileFinder
39
                    ->setSearchPath($searchPath)
40
                    ->setExtensions(['php'])
41
                    ->getFlatFileArray();
42
                if (is_array($flatArray) && count($flatArray)) {
43
                    foreach ($flatArray as $path) {
44
                        $tableName = basename($path, '.php');
45
                        $newLine = 'private static $table_name = \'' . $tableName . '\';';
46
                        $filecontent = file_get_contents($path);
47
                        $hasNewLine = strpos($filecontent, 'private static $table_name');
48
                        if (! $hasNewLine) {
49
                            $positionOfPrivateStatic = 0;
50
                            foreach ($this->listToSearchFor as $item) {
51
                                $positionOfPrivateStatic = strpos($filecontent, $item);
52
                                if ($positionOfPrivateStatic) {
53
                                    break;
54
                                }
55
                            }
56
                            if ($positionOfPrivateStatic) {
57
                                $this->mu()->colourPrint('Adding  ' . $newLine . ' to ' . $path, 'green');
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

57
                                $this->mu()->/** @scrutinizer ignore-call */ colourPrint('Adding  ' . $newLine . ' to ' . $path, 'green');
Loading history...
58
                                $filecontent =
59
                                    substr($filecontent, 0, $positionOfPrivateStatic) .
60
                                    "\r\n" . "\r\n" . '    ' . $newLine . "\r\n" . "\r\n" .
61
                                    substr($filecontent, $positionOfPrivateStatic);
62
                                file_put_contents($path, $filecontent);
63
                            }
64
                        }
65
                    }
66
                } else {
67
                    $this->mu()->colourPrint('Could not find any files in ' . $searchPath, 'red');
68
                }
69
            } else {
70
                $this->mu()->colourPrint('Could not find ' . $searchPath, 'blue');
71
            }
72
        }
73
        return null;
74
    }
75
76
    protected function hasCommitAndPush()
77
    {
78
        return true;
79
    }
80
}
81