From 71e80e8b4a8988dd7e79789c38f683b53ae6a14a Mon Sep 17 00:00:00 2001 From: Christopher Allford <6451942+ObliviousHarmony@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:17:33 -0800 Subject: [PATCH] Added Package Version Validation --- .../{VSCode.php => VSCodeIntegration.php} | 18 ++++++++++++++++-- src/phpcs-report/worker.ts | 15 ++++++++++++++- src/services/configuration.ts | 7 +++++++ 3 files changed, 37 insertions(+), 3 deletions(-) rename assets/phpcs-integration/{VSCode.php => VSCodeIntegration.php} (84%) diff --git a/assets/phpcs-integration/VSCode.php b/assets/phpcs-integration/VSCodeIntegration.php similarity index 84% rename from assets/phpcs-integration/VSCode.php rename to assets/phpcs-integration/VSCodeIntegration.php index ee17b5b..ab12c8d 100644 --- a/assets/phpcs-integration/VSCode.php +++ b/assets/phpcs-integration/VSCodeIntegration.php @@ -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. @@ -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); @@ -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'); } diff --git a/src/phpcs-report/worker.ts b/src/phpcs-report/worker.ts index ca9d151..77eb22c 100644 --- a/src/phpcs-report/worker.ts +++ b/src/phpcs-report/worker.ts @@ -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. @@ -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]; + } } } @@ -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', @@ -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, }), diff --git a/src/services/configuration.ts b/src/services/configuration.ts index 80cbfac..b8c8b22 100644 --- a/src/services/configuration.ts +++ b/src/services/configuration.ts @@ -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. */