-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVSCodeIntegration.php
133 lines (120 loc) · 4.86 KB
/
VSCodeIntegration.php
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
<?php
namespace ObliviousHarmony\VSCodePHPCSIntegration;
use PHP_CodeSniffer\Files\File as BaseFile;
use PHP_CodeSniffer\Reports\Report;
use ObliviousHarmony\VSCodePHPCSIntegration\Extension\File;
use ObliviousHarmony\VSCodePHPCSIntegration\Handlers\Handler;
// This should match the version in the `configuration.ts` file so that
// we can provide error messaging that tells them to update the
// Composer package containing these files.
define('PHPCS_INTEGRATION_VERSION', '1.0.0');
/**
* The custom report for our PHPCS integration.
*/
class VSCodeIntegration implements Report
{
/**
* Constructor.
*/
public function __construct()
{
// Dependencies must be loaded during class construction, as otherwise,
// we can't guarantee that PHPCS won't execute the wrong class file.
include_once __DIR__ . '/Extension/File.php';
include_once __DIR__ . '/Extension/Fixer.php';
include_once __DIR__ . '/Handlers/Handler.php';
include_once __DIR__ . '/Handlers/Diagnostic.php';
include_once __DIR__ . '/Handlers/CodeAction.php';
include_once __DIR__ . '/Handlers/Format.php';
}
/**
* Generates a diagnostic report for a processed file.
*
* @param array $report The prepared report data.
* @param BaseFile $phpcsFile The file being reported on.
* @param bool $showSources Whether or not we should show sources in the report.
* @param int $width The maximum allowed line width for the report.
*
* @return true
*/
public function generateFileReport($report, BaseFile $phpcsFile, $showSources = false, $width = 80)
{
// Convert the file into our custom class so that we can extend PHPCS.
$phpcsFile = new File($phpcsFile);
// We use an environment variable to pass input to the reports.
$input = $this->getVSCodeInput();
// Make sure that we are running the correct version of the integration package.
if ($input->version !== PHPCS_INTEGRATION_VERSION) {
$errorMessage = 'The extension expected version '
. PHPCS_INTEGRATION_VERSION
. ' of the integration files. Current Version: '
. $input->version . PHP_EOL;
throw new \InvalidArgumentException($errorMessage);
}
// Use the handler to process the report.
$handler = $this->getHandler($input->type);
return $handler->execute($report, $phpcsFile, $input->data);
}
/**
* Generates the final report.
*
* @param string $cachedData The result from running generateFileReport on each file in the request.
* @param int $totalFiles Total nunber of files checked.
* @param int $totalErrors Total number of errors.
* @param int $totalWarnings Total number of warnings.
* @param int $totalFixable Total number of fixable problems.
* @param bool $showSources Whether or not we should show sources in the report.
* @param int $width The maximum allowed line width for the report.
* @param bool $interactive Indicates whether or not the report is being generated interactively.
* @param bool $toScreen Indicates whether or not the report is being printed to the screen.
*/
public function generate(
$cachedData,
$totalFiles,
$totalErrors,
$totalWarnings,
$totalFixable,
$showSources = false,
$width = 80,
$interactive = false,
$toScreen = true
) {
// Remove the trailing comma that every file adds to the end of their report.
echo '{"files":[';
echo rtrim($cachedData, ",");
echo ']}' . PHP_EOL;
}
/**
* Gets the handler class for this report.
*
* @param string $reportType The type of the report we are running.
* @return Handler
* @throws \InvalidArgumentException The handler could not be found.
*/
protected function getHandler($reportType)
{
// Find the handler class file that should power this report.
$report = '\\ObliviousHarmony\\VSCodePHPCSIntegration\\Handlers\\' . $reportType;
if (!\class_exists($report)) {
throw new \InvalidArgumentException('Handler "' . $report . '" could be found');
}
return new $report();
}
/**
* Reads data from the VS Code environment variable.
*
* @return \stdClass|null
* @throws \InvalidArgumentException The environemnt variable is invalid.
*/
protected function getVSCodeInput()
{
if (empty($_SERVER['PHPCS_VSCODE_INPUT'])) {
return null;
}
$data = json_decode($_SERVER['PHPCS_VSCODE_INPUT']);
if (empty($data)) {
throw new \InvalidArgumentException('The "PHPCS_VSCODE_INPUT" environment variable is invalid.');
}
return $data;
}
}