1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
<?php
namespace Studip\Cli\Commands\Checks;
use DirectoryIterator;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use RegexIterator;
use Studip\Cli\Commands\AbstractCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Compatibility extends AbstractCommand
{
protected static $defaultName = 'check:compatibility';
protected function configure(): void
{
$this->setDescription('Compatibility scanner');
$this->setHelp('Scans plugins for common issues (backward compatibility and the like)');
$this->addArgument(
'version',
InputArgument::OPTIONAL,
'Version to check against (if not suppied, all checks are performed)'
);
$this->addArgument(
'folder',
InputArgument::IS_ARRAY,
'Folder to scan (will default to the plugins_packages folder)'
);
$this->addOption('filenames', 'f', InputOption::VALUE_NONE, 'Display filenames only');
$this->addOption(
'recursive',
'r',
InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE,
'Do not scan recursively into subfolders'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->getFormatter()->setStyle('issue', new OutputFormatterStyle('red'));
$output->getFormatter()->setStyle('bold', new OutputFormatterStyle(null, null, ['bold']));
$rules = $this->getCompatibilityRules($input->getArgument('version'));
$folders = $input->getArgument('folder') ?: $this->getDefaultFolders();
$recursive = $input->getOption('recursive') ?? true;
foreach ($folders as $f) {
$folder = $this->validateFolder($f);
if (!$folder) {
$output->writeln("<info>Skipping invalid folder {$f}</info>", OutputInterface::VERBOSITY_VERBOSE);
continue;
}
$issues = [];
foreach ($this->getFolderIterator($folder, $recursive, ['php', 'tpl', 'inc', 'js']) as $file) {
$filename = $file->getPathName();
$output->writeln("<info>Checking {$filename}", OutputInterface::VERBOSITY_VERBOSE);
if ($errors = $this->checkFilecontentsAgainstRules($filename, $rules)) {
$issues[$filename] = $errors;
}
}
if (count($issues) === 0) {
continue;
}
if (!$input->getOption('filenames')) {
$issue_count = array_sum(array_map('count', $issues));
$message = count($issues) === 1
? '%u issue found in <bold>%s</bold>'
: '%u issues found in <bold>%s</bold>';
$output->writeln(sprintf(
"<issue>{$message}</issue>",
$issue_count,
$this->relativeFilePath($folder)
));
}
foreach ($issues as $filename => $errors) {
if ($input->getOption('filenames')) {
$output->writeln($filename);
} else {
$output->writeln(sprintf(
'> File <fg=green;options=bold>%s</>',
$this->relativeFilePath($filename)
));
foreach ($errors as $needle => $suggestion) {
$output->writeln(
sprintf('- <fg=cyan>%s</> -> %s', $needle, $suggestion ?: '<fg=red>No suggestion available')
);
}
}
}
}
return Command::SUCCESS;
}
private function getCompatibilityRules(?string $version): array
{
if ($version !== null) {
if (!file_exists(__DIR__ . "/compatibility-rules/studip-{$version}.php")) {
throw new \Exception("No rules defined for Stud.IP version {$version}");
}
return require __DIR__ . "/compatibility-rules/studip-{$version}.php";
}
$rules = [];
foreach (glob(__DIR__ . '/compatbility-rules/*.php') as $file) {
$version_rules = require $file;
$rules = array_merge($rules, $version_rules);
}
return $rules;
}
private function getDefaultFolders(): array
{
$folders = rtrim($GLOBALS['STUDIP_BASE_PATH'], '/') . '/public/plugins_packages';
$folders = glob($folders . '/*/*');
return $folders;
}
private function validateFolder(string $folder)
{
if (!file_exists($folder) || !is_dir($folder)) {
return false;
}
return $folder;
}
private function checkFilecontentsAgainstRules(string $filename, array $rules)
{
$errors = [];
$contents = strtolower(file_get_contents($filename));
foreach ($rules as $needle => $suggestion) {
if ($this->checkRule($contents, $needle)) {
$errors[$needle] = $suggestion;
}
}
return $errors;
}
private function checkRule(string $contents, string $rule)
{
if ($rule[0] === '/' && $rule[strlen($rule) - 1] === '/') {
return (bool) preg_match("{$rule}s", $contents);
}
return strpos($contents, strtolower($rule)) > 0;
}
}
|