UpgradeOnlyCheckYMLClassNames   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 31
eloc 104
c 3
b 0
f 0
dl 0
loc 163
rs 9.92

3 Methods

Rating   Name   Duplication   Size   Complexity  
D performMigration() 0 106 26
A getYMLFiles() 0 9 2
A getSubDirectories() 0 16 3
1
<?php
2
3
namespace Sunnysideup\MigrateData\Tasks;
4
5
use SilverStripe\Control\Director;
6
7
/**
8
 * Reorganizing the SiteTree led to records being in the _Live table but not in the
9
 * draft table. This tasks should be run once to get rid of them.
10
 */
11
class UpgradeOnlyCheckYMLClassNames extends MigrateDataTaskBase
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $title = 'Load your list of class names';
17
18
    protected $description = 'Upgrade ONLY!';
19
20
    protected $enabled = true;
21
22
    private static $folders_to_ignore = [
23
        'public',
24
        'resources',
25
        'themes',
26
        'vendor',
27
    ];
28
29
    private static $files_to_ignore = [
30
        'database.legacy.yml',
31
    ];
32
33
    /**
34
     * Implement this method in the task subclass to
35
     * execute via the TaskRunner.
36
     */
37
    protected function performMigration()
38
    {
39
        $count = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $count is dead and can be removed.
Loading history...
40
        $subDirs = $this->getSubDirectories(Director::baseFolder() . '/');
41
        $ymlFiles = [];
42
        foreach ($subDirs as $subDir) {
43
            $subDir = rtrim($subDir, '/') . '/';
44
            $fullSubDir = $subDir . '_config/';
45
            if (file_exists($fullSubDir)) {
46
                $toAdds = $this->getYMLFiles($fullSubDir);
47
                foreach ($toAdds as $toAdd) {
48
                    if (! in_array(basename($toAdd), $this->Config()->get('files_to_ignore'), true)) {
49
                        $ymlFiles[] = $toAdd;
50
                    }
51
                }
52
            }
53
        }
54
        foreach ($ymlFiles as $fileName) {
55
            $this->flushNowLine();
56
            $this->flushNow('STARTING TESTING: ' . $fileName . '');
57
            $this->flushNowLine();
58
            $count = 0;
59
            $alreadySet = [];
60
            if (! file_exists($fileName)) {
61
                die(' Could not find ' . $fileName);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
62
            }
63
            $fp = fopen($fileName, 'r');
64
            $hasLine = true;
65
            while ($hasLine) {
66
                $line = stream_get_line($fp, 1024 * 1024, "\n");
67
                if (! $line) {
68
                    break;
69
                }
70
                ++$count;
71
                $isProperty = false;
72
                // $this->flushNow( '...';
73
                //skip lines that are indented
74
                if (' ' === substr((string) $line, 0, 1)) {
75
                    $isProperty = true;
76
                }
77
78
                if ($isProperty) {
79
                    $className = end($alreadySet);
80
                    if ($className && class_exists($className)) {
81
                        if (strpos($line, ':')) {
82
                            $myItems = explode(':', $line);
83
                            if (2 === count($myItems) && $myItems[0] && $myItems[1]) {
84
                                $property = trim($myItems[0]);
0 ignored issues
show
Unused Code introduced by
The assignment to $property is dead and can be removed.
Loading history...
85
                                $property = trim($myItems[0], "'");
86
                                $property = trim($myItems[0], '"');
87
                                if (strpos($property, '\\')) {
88
                                    if (! class_exists($property)) {
89
                                        $this->flushNow('
90
ERROR ' . $className . '.' . $property . ' may not exist as class name but looks like one.<br>');
91
                                    }
92
                                } elseif (strpos($property, '*')) {
93
                                    $this->flushNow('
94
ERROR ' . $className . '.' . $property . ' contains *.<br>');
95
                                } elseif (! property_exists($className, $property)) {
96
                                    $this->flushNow('
97
ERROR ' . $className . '.' . $property . ' property could not be found<br>');
98
                                } else {
99
                                    $this->flushNow('
100
SUCCESS ' . $className . '.' . $property . '');
101
                                }
102
                            } else {
103
                                $this->flushNow('
104
    ' . $line . ' ... not two items<br>');
105
                            }
106
                        } else {
107
                            $this->flushNow('
108
' . $line . ' ... no colon<br>');
109
                        }
110
                    } elseif ($className) {
111
                        $this->flushNow('
112
COULD NOT FIND ' . $className . '<br>');
113
                    }
114
                } else {
115
                    if (! strpos($line, '\\')) {
116
                        continue;
117
                    }
118
                    if (strpos($line, '*')) {
119
                        continue;
120
                    }
121
                    $line = str_replace(':', '', (string) $line);
122
                    $line = trim($line);
123
                    if (isset($alreadySet[$line])) {
124
                        $this->flushNow('
125
126
ERROR: Two mentions of ' . $line . '<br>');
127
                    } else {
128
                        $alreadySet[$line] = $line;
129
                        if (! class_exists($line)) {
130
                            $this->flushNow('
131
132
ERROR: Could not find class ' . $line . '<br>');
133
                        }
134
                    }
135
                }
136
                //not a class name
137
            }
138
            fclose($fp);
139
            $this->flushNowLine();
140
            $this->flushNow('' . $count . ' lines');
141
            $this->flushNowLine();
142
            $count = 0;
143
        }
144
    }
145
146
    private function getSubDirectories($dir)
147
    {
148
        $subDirs = [];
149
        $ignore = $this->Config()->folders_to_ignore;
150
        $dir = rtrim($dir, '/') . '/';
151
        // Get and add directories of $dir
152
        $directories = array_filter(glob($dir . '*'), 'is_dir');
153
        // $subDir = array_merge($subDir, $directories);
154
        // Foreach directory, recursively get and add sub directories
155
        foreach ($directories as $directory) {
156
            if (! in_array(basename($directory), $ignore, true)) {
157
                $subDirs[] = $directory;
158
            }
159
        }
160
161
        return $subDirs;
162
        // Return list of sub directories
163
    }
164
165
    private function getYMLFiles($dir)
166
    {
167
        $dir = rtrim($dir, '/') . '/';
168
        $files = glob($dir . '*.yaml');
169
        foreach (glob($dir . '*.yml') as $file) {
170
            $files[] = $file;
171
        }
172
173
        return $files;
174
    }
175
}
176