Skip to content

Commit

Permalink
Added Package Version Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ObliviousHarmony committed Jan 29, 2024
1 parent 9336f6e commit 71e80e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
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 VSCode implements Report
class VSCodeIntegration implements Report
{
/**
* Constructor.
Expand Down Expand Up @@ -45,6 +50,15 @@ public function generateFileReport($report, BaseFile $phpcsFile, $showSources =
// 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);
Expand Down Expand Up @@ -90,7 +104,7 @@ public function generate(
protected function getHandler($reportType)
{
// Find the handler class file that should power this report.
$report = '\\VSCode\\PHP_CodeSniffer\\Handlers\\' . $reportType;
$report = '\\ObliviousHarmony\\VSCodePHPCSIntegration\\Handlers\\' . $reportType;
if (!\class_exists($report)) {
throw new \InvalidArgumentException('Handler "' . $report . '" could be found');
}
Expand Down
15 changes: 14 additions & 1 deletion src/phpcs-report/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { resolve as resolvePath } from 'path';
import { CancellationError, CancellationToken, Disposable } from 'vscode';
import { Request } from './request';
import { ReportType, Response } from './response';
import { PHPCS_INTEGRATION_VERSION } from '../services/configuration';

/**
* A callback to execute when the worker has completed its task.
Expand Down Expand Up @@ -37,6 +38,14 @@ export class PHPCSError extends Error {

this.output = output;
this.errorOutput = errorOutput;

// Depending on the type of error we may want to perform some processing.
const match = this.errorOutput.match(
/Uncaught InvalidArgumentException: (The extension[^)]+)/
);
if (match) {
this.errorOutput = match[1];
}
}
}

Expand Down Expand Up @@ -166,7 +175,10 @@ export class Worker {
const processArguments = [
'-q', // Make sure custom configs never break our output.
'--report=' +
resolvePath(request.options.phpcsIntegrationPath, 'VSCode.php'),
resolvePath(
request.options.phpcsIntegrationPath,
'VSCodeIntegration.php'
),
// We want to reserve error exit codes for actual errors in the PHPCS execution since errors/warnings are expected.
'--runtime-set',
'ignore_warnings_on_exit',
Expand Down Expand Up @@ -208,6 +220,7 @@ export class Worker {
...process.env,
// Pass the request data using environment vars.
PHPCS_VSCODE_INPUT: JSON.stringify({
version: PHPCS_INTEGRATION_VERSION,
type: request.type,
data: request.data,
}),
Expand Down
7 changes: 7 additions & 0 deletions src/services/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import {
import { UriMap } from '../common/uri-map';
import { WorkspaceLocator } from './workspace-locator';

/**
* A constant for the version of the PHPCS integration files.
* This should be incremented if the files are changed so
* that we can provide a clear error message.
*/
export const PHPCS_INTEGRATION_VERSION = '1.0.0';

/**
* An enum describing the special parsing values in the `phpCodeSniffer.standard` configuration.
*/
Expand Down

0 comments on commit 71e80e8

Please sign in to comment.