-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLint.php
More file actions
132 lines (111 loc) · 3.05 KB
/
Copy pathLint.php
File metadata and controls
132 lines (111 loc) · 3.05 KB
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
<?php
declare(strict_types=1);
namespace PHPCensor\Plugins\CodeQuality;
use PHPCensor\Common\Build\BuildInterface;
use PHPCensor\Common\Plugin\Plugin;
/**
* PHP Lint Plugin - Provides access to PHP lint functionality.
*
* @package PHP Censor
* @subpackage Plugins
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author Dan Cryer <dan@block8.co.uk>
*/
class Lint extends Plugin
{
private array $directories = [];
private bool $recursive = true;
/**
* {@inheritDoc}
*/
public static function getName(): string
{
return 'lint';
}
/**
* {@inheritDoc}
*/
public function execute(): bool
{
$success = true;
foreach ($this->directories as $dir) {
if (!$this->lintDirectory($dir)) {
$success = false;
}
}
return $success;
}
/**
* {@inheritDoc}
*/
public static function canExecute(string $stage, BuildInterface $build): bool
{
if (BuildInterface::STAGE_TEST === $stage) {
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
protected function initPluginSettings(): void
{
$this->directories = (array)$this->options->get('directories', $this->directories);
foreach ($this->directories as $index => $directory) {
$this->directories[$index] = $this->pathResolver->resolvePath($directory);
}
$this->directories[] = $this->directory;
$this->recursive = (bool)$this->options->get('recursive', $this->recursive);
}
/**
* Run php -l against a directory of files.
*/
private function lintDirectory(string $path): bool
{
$success = true;
$directory = new \DirectoryIterator($path);
foreach ($directory as $item) {
if ($item->isDot()) {
continue;
}
$itemPath = $path . $item->getFilename();
if (\in_array($itemPath, $this->ignores, true)) {
continue;
}
if (!$this->lintItem($item, $itemPath)) {
$success = false;
}
}
return $success;
}
/**
* Lint an item (file or directory) by calling the appropriate method.
*/
private function lintItem(\SplFileInfo $item, string $itemPath): bool
{
$success = true;
if ($item->isFile() && $item->getExtension() === 'php' && !$this->lintFile($itemPath)) {
$success = false;
} elseif (
$item->isDir() &&
$this->recursive &&
!$this->lintDirectory($itemPath . '/')
) {
$success = false;
}
return $success;
}
/**
* Run php -l against a specific file.
*/
private function lintFile(string $path): bool
{
$success = true;
if (!$this->commandExecutor->executeCommand('php -l "%s"', $path)) {
$this->buildLogger->logFailure($path);
$success = false;
}
return $success;
}
}