Skip to content

Commit

Permalink
Restructured PHPCS Integration (#39)
Browse files Browse the repository at this point in the history
Since PHPCS can't reliably detect the FQN of the report class, I've
restructured the PHPCS integration so that it will ensure only a
single class file is ever loaded when including the report.
This will ensure we always load the correct report file.

This commit also comes with some changes to the build process.
We don't need to reference the report files anymore, since we aren't
going to require Webpack to compile the assets to find the reports.
  • Loading branch information
ObliviousHarmony authored Dec 10, 2021
1 parent a3d0ab3 commit 4d2ca5d
Show file tree
Hide file tree
Showing 25 changed files with 1,265 additions and 429 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"plugin:prettier/recommended"
],
"ignorePatterns": [
"/dist/",
"/node_modules/",
"webpack.config.js"
"webpack.config.js",
"extension.js"
],
"rules": {
"@typescript-eslint/no-unused-vars": "off",
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
!.vscode/tasks.json

# Build Artifacts
out/
dist/
*.vsix
extension.js
extension.js.map

# Dependencies
node_modules/
Expand Down
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
"${workspaceFolder}/extension.js",
"${workspaceFolder}/assets/**/*.php"
],
"preLaunchTask": "${defaultBuildTask}"
}
Expand Down
4 changes: 1 addition & 3 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
.github/
node_modules/
vendor/
out/
src/
phpcs-reports/
.eslintrc.json
jest.config.js
jest.config.json
tsconfig.json
webpack.config.js
composer.json
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Broken PHPCS integration due to inconsistent class loading behavior.

## [1.4.0] - 2021-08-13
### Added
Expand Down
File renamed without changes
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

namespace VSCode\PHP_CodeSniffer;
namespace VSCode\PHP_CodeSniffer\Extension;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Files\File as BaseFile;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* A class that supports targeting specific tokens for fixes to allow for
* tracking the edits that should be created by a single
*
* @property Fixer|VSCodeFixer $fixer
* @property \VSCode\PHP_CodeSniffer\Extension\Fixer $fixer
*/
class VSCodeFile extends File
class File extends BaseFile
{
/**
* A map containing the token pointers indexed by [line][column].
Expand Down Expand Up @@ -51,9 +51,9 @@ class VSCodeFile extends File
/**
* Constructs an instance from an existing file.
*
* @param File $phpcsFile The file to use.
* @param BaseFile $phpcsFile The file to use.
*/
public function __construct(File $phpcsFile)
public function __construct(BaseFile $phpcsFile)
{
// Populate from the original file.
$this->content = $phpcsFile->content;
Expand All @@ -78,9 +78,9 @@ public function parse()
/**
* Gets the stack pointer for a position.
*
* @param int $line The line to check.
* @param int $column The column to check.
* @param bool $useRangeFormat Indicates we should find the VS Code range format.
* @param int $line The line to check.
* @param int $column The column to check.
* @param bool $useRangeFormat Indicates we should find the VS Code range format.
* @return int|null
*/
public function getStackPtrForPosition($line, $column, $useRangeFormat = false)
Expand All @@ -103,7 +103,7 @@ public function getStackPtrForPosition($line, $column, $useRangeFormat = false)
/**
* Gets a specific token.
*
* @param int $stackPtr The token pointer to fetch.
* @param int $stackPtr The token pointer to fetch.
* @return array
*/
public function getToken($stackPtr)
Expand All @@ -114,8 +114,8 @@ public function getToken($stackPtr)
/**
* Formats a document range and returns the changed content as a result.
*
* @param int|null $startToken The token to start formatting from.
* @param int|null $endToken The token to end formatting on.
* @param int|null $startToken The token to start formatting from.
* @param int|null $endToken The token to end formatting on.
* @return string
*/
public function formatRange($startToken, $endToken)
Expand All @@ -138,8 +138,8 @@ public function formatRange($startToken, $endToken)
/**
* Fixes a single code action and returns the content changed as a result.
*
* @param int $sourceStackPtr The position we want to fix.
* @param source $source The problem we want to fix.
* @param int $sourceStackPtr The position we want to fix.
* @param string $source The problem we want to fix.
* @return array
*/
public function fixCodeAction($sourceStackPtr, $source)
Expand All @@ -156,7 +156,7 @@ public function fixCodeAction($sourceStackPtr, $source)
// Replace the fixer with a custom one that can give us insight into
// the specific tokens that have been replaced.
$fixer = $this->fixer;
$this->fixer = new VSCodeFixer();
$this->fixer = new Fixer();
$this->fixer->enabled = true;
$this->fixer->startFile($this);

Expand Down Expand Up @@ -297,7 +297,7 @@ private function prepareTokensForVSCode()
/**
* Fetches the sniff class instance for the given message source.
*
* @param string $source
* @param string $source
* @return Sniff|null
*/
private function getSniffFromMessageSource($source)
Expand All @@ -316,7 +316,7 @@ private function getSniffFromMessageSource($source)
/**
* Takes an array of changed tokens and merged contiguous blocks into text edits.
*
* @param array $changedTokens All of the tokens that changed.
* @param array $changedTokens All of the tokens that changed.
* @return array
*/
private function getTextEdits($changedTokens)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace VSCode\PHP_CodeSniffer;
namespace VSCode\PHP_CodeSniffer\Extension;

use PHP_CodeSniffer\Fixer;
use PHP_CodeSniffer\Fixer as BaseFixer;

/**
* An override of the fixer that we can use to track the changes that sniffs make to the file.
* An extension of the fixer that we can use to track the changes that sniffs make to the file.
*/
class VSCodeFixer extends Fixer
class Fixer extends BaseFixer
{
/**
* Indicates whether or not the text edit is in a changeset.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
<?php

namespace VSCode\PHP_CodeSniffer\Reports;
namespace VSCode\PHP_CodeSniffer\Handlers;

use VSCode\PHP_CodeSniffer\VSCodeFile;

// @phpcs:disable
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'VSCodeReport.php';
// @phpcs:enable
use VSCode\PHP_CodeSniffer\Extension\File;

/**
* A custom report for returning information about edits from PHPCS in a way
* A handler for returning information about edits from PHPCS in a way
* that the extension can easily consume.
*/
class CodeAction extends VSCodeReport
class CodeAction implements Handler
{
/**
* Executes the actual PHPCS report.
*
* @param array $report The PHPCS report.
* @param VSCodeFile $file The file we're reporting on.
* @param stdClass|null $data The data object passed from VS Code.
* @param array $report The PHPCS report.
* @param File $file The file we're reporting on.
* @param stdClass|null $data The data object passed from VS Code.
* @return bool True if we have processed the file, otherwise false.
*/
protected function executeReport($report, VSCodeFile $file, $data)
public function execute($report, File $file, $data)
{
// Get information about the problem we want to fix.
$problemSource = $data->code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
<?php

namespace VSCode\PHP_CodeSniffer\Reports;
namespace VSCode\PHP_CodeSniffer\Handlers;

use VSCode\PHP_CodeSniffer\VSCodeFile;

// @phpcs:disable
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'VSCodeReport.php';
// @phpcs:enable
use VSCode\PHP_CodeSniffer\Extension\File;

/**
* A custom report for returning information from PHPCS in a way that the
* A handler for returning information from PHPCS in a way that the
* extension can more easily consume.
*/
class Diagnostic extends VSCodeReport
class Diagnostic implements Handler
{
/**
* Executes the actual PHPCS report.
*
* @param array $report The PHPCS report.
* @param VSCodeFile $file The file we're reporting on.
* @param stdClass|null $data The data object passed from VS Code.
* @param array $report The PHPCS report.
* @param File $file The file we're reporting on.
* @param stdClass|null $data The data object passed from VS Code.
* @return bool True if we have processed the file, otherwise false.
*/
protected function executeReport($report, VSCodeFile $file, $data)
public function execute($report, File $file, $data)
{
$diagnostics = array();
$codeActions = array();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
<?php

namespace VSCode\PHP_CodeSniffer\Reports;
namespace VSCode\PHP_CodeSniffer\Handlers;

use VSCode\PHP_CodeSniffer\VSCodeFile;

// @phpcs:disable
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'VSCodeReport.php';
// @phpcs:enable
use VSCode\PHP_CodeSniffer\Extension\File;

/**
* A custom report for formatting a document or range within a document.
* A handler for formatting a document or range within a document.
*/
class Format extends VSCodeReport
class Format implements Handler
{
/**
* Executes the actual PHPCS report.
*
* @param array $report The PHPCS report.
* @param VSCodeFile $file The file we're reporting on.
* @param stdClass|null $data The data object passed from VS Code.
* @param array $report The PHPCS report.
* @param File $file The file we're reporting on.
* @param stdClass|null $data The data object passed from VS Code.
* @return bool True if we have processed the file, otherwise false.
*/
protected function executeReport($report, VSCodeFile $file, $data)
public function execute($report, File $file, $data)
{
// Apply the formatting restriction if one is given.
$startToken = null;
Expand Down
21 changes: 21 additions & 0 deletions assets/phpcs-integration/Handlers/Handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace VSCode\PHP_CodeSniffer\Handlers;

use VSCode\PHP_CodeSniffer\Extension\File;

/**
* The interface for all PHPCS handlers.
*/
interface Handler
{
/**
* Executes a handler.
*
* @param array $report The PHPCS report we're processing.
* @param File $file The file we're reporting on.
* @param array|null $data The data object passed from VS Code.
* @return bool True if we have processed the file, otherwise false.
*/
public function execute($report, File $file, $data);
}
Loading

0 comments on commit 4d2ca5d

Please sign in to comment.