1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ModuleRatings\Check; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\ModuleRatings\Check; |
7
|
|
|
|
8
|
|
|
class CodingStandardCheck extends Check |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The path to the PHPCS standards file |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $standardsFile = ''; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
$this->setStandardsFile(realpath(__DIR__) . '/CodingStandardCheck/phpcs.xml.dist'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getKey() |
23
|
|
|
{ |
24
|
|
|
return 'coding_standards'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getDescription() |
28
|
|
|
{ |
29
|
|
|
return 'The PHP code in this module passes the SilverStripe lint rules (mostly PSR-2)'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get PHP CodeSniffer and run it over the current module. Assigns a successful result if the codebase passes |
34
|
|
|
* the linting check with no errors. |
35
|
|
|
*/ |
36
|
|
|
public function run() |
37
|
|
|
{ |
38
|
|
|
$arguments = []; |
39
|
|
|
if (file_exists($this->getSuite()->getModuleRoot() . '/phpcs.xml.dist')) { |
40
|
|
|
$includePaths = []; |
41
|
|
|
$ignorePaths = []; |
42
|
|
|
$moduleXml = file_get_contents($this->getSuite()->getModuleRoot() . '/phpcs.xml.dist'); |
43
|
|
|
|
44
|
|
|
preg_match_all('/<exclude-pattern>(.*)<\/exclude-pattern>/', $moduleXml, $ignorePaths); |
45
|
|
|
|
46
|
|
|
if (count($ignorePaths) > 0 && count($ignorePaths[1]) > 0) { |
47
|
|
|
foreach ($ignorePaths[1] as $ignorePath) { |
48
|
|
|
$arguments[] = '--ignore="' . $ignorePath . '"'; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
preg_match_all('/<file>(.*)<\/file>/', $moduleXml, $includePaths); |
53
|
|
|
|
54
|
|
|
if (count($includePaths) > 0 && count($includePaths[1]) > 0) { |
55
|
|
|
foreach ($includePaths[1] as $includePath) { |
56
|
|
|
$arguments[] = $this->getSuite()->getModuleRoot() . DIRECTORY_SEPARATOR . $includePath; |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$arguments[] = $this->getSuite()->getModuleRoot(); |
60
|
|
|
} |
61
|
|
|
} else { |
62
|
|
|
$arguments[] = $this->getSuite()->getModuleRoot(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$standard = '--standard=' . escapeshellarg($this->getStandardsFile()); |
66
|
|
|
|
67
|
|
|
$output = null; |
68
|
|
|
exec( |
69
|
|
|
'cd ' . $this->getProjectRoot() . ' && vendor/bin/phpcs -q ' |
70
|
|
|
. $standard . ' ' . implode(' ', $arguments), |
71
|
|
|
$output, |
72
|
|
|
$exitCode |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
if ($exitCode == 0) { |
76
|
|
|
$this->setSuccessful(true); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function setStandardsFile($standardsFile) |
81
|
|
|
{ |
82
|
|
|
$this->standardsFile = $standardsFile; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getStandardsFile() |
87
|
|
|
{ |
88
|
|
|
return $this->standardsFile; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getProjectRoot() |
92
|
|
|
{ |
93
|
|
|
$base = dirname(__FILE__); |
94
|
|
|
if (file_exists($base . '/../../../../../vendor/autoload.php')) { |
95
|
|
|
// Installed in the vendor folder |
96
|
|
|
return $base . '/../../../../../'; |
97
|
|
|
} elseif (file_exists($base . '/../../vendor/autoload.php')) { |
98
|
|
|
// Installed as the project |
99
|
|
|
return $base . '/../../'; |
100
|
|
|
} |
101
|
|
|
throw new Exception('Could not find the project root folder!'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|