Skip to content

Generic/Syntax: add support for inspecting code passed via STDIN #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/Standards/Generic/Sniffs/PHP/SyntaxSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ public function process(File $phpcsFile, $stackPtr)
$this->phpPath = Config::getExecutablePath('php');
}

$fileName = escapeshellarg($phpcsFile->getFilename());
$cmd = Common::escapeshellcmd($this->phpPath)." -l -d display_errors=1 -d error_prepend_string='' $fileName 2>&1";
$output = shell_exec($cmd);
$matches = [];
$cmd = $this->getPhpLintCommand($phpcsFile);
$output = shell_exec($cmd);
$matches = [];
if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/m', trim($output), $matches) === 1) {
$error = trim($matches[1]);
$line = (int) $matches[2];
Expand All @@ -72,4 +71,25 @@ public function process(File $phpcsFile, $stackPtr)
}//end process()


/**
* Returns the command used to lint PHP code. Uses a different command when the content is
* provided via STDIN.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The File object.
*
* @return string The command used to lint PHP code.
*/
private function getPhpLintCommand(File $phpcsFile)
{
if ($phpcsFile->getFilename() === 'STDIN') {
$content = $phpcsFile->getTokensAsString(0, $phpcsFile->numTokens);
return "echo ".escapeshellarg($content)." | ".Common::escapeshellcmd($this->phpPath)." -l -d display_errors=1 -d error_prepend_string='' 2>&1";
}

$fileName = escapeshellarg($phpcsFile->getFilename());
return Common::escapeshellcmd($this->phpPath)." -l -d display_errors=1 -d error_prepend_string='' $fileName 2>&1";

}//end getPhpLintCommand()


}//end class
106 changes: 106 additions & 0 deletions src/Standards/Generic/Tests/PHP/SyntaxUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace PHP_CodeSniffer\Standards\Generic\Tests\PHP;

use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
Expand Down Expand Up @@ -60,4 +63,107 @@ public function getWarningList()
}//end getWarningList()


/**
* Test the sniff checks syntax when file contents are passed via STDIN. Doesn't run on Windows
* as PHPCS currently doesn't support STDIN on this OS.
*
* @param string $content The content to test.
* @param int $errorCount The expected number of errors.
* @param array $expectedErrors The expected errors.
*
* @dataProvider dataStdIn
* @requires OS ^(?!WIN).*
*
* @return void
*/
public function testStdIn($content, $errorCount, $expectedErrors)
{
$config = new ConfigDouble();
$config->standards = ['Generic'];
$config->sniffs = ['Generic.PHP.Syntax'];

$ruleset = new Ruleset($config);

$file = new DummyFile($content, $ruleset, $config);
$file->process();

$this->assertSame(
$errorCount,
$file->getErrorCount(),
'Error count does not match expected value'
);
$this->assertSame(
0,
$file->getWarningCount(),
'Warning count does not match expected value'
);
$this->assertSame(
$expectedErrors,
$file->getErrors(),
'Error list does not match expected errors'
);

}//end testStdIn()


/**
* Data provider for testStdIn().
*
* @return array[]
*/
public function dataStdIn()
{
// The error message changed in PHP 8+.
if (PHP_VERSION_ID >= 80000) {
$errorMessage = 'PHP syntax error: syntax error, unexpected token ";", expecting "]"';
} else {
$errorMessage = 'PHP syntax error: syntax error, unexpected \';\', expecting \']\'';
}

return [
'No syntax errors' => [
'<?php $array = [1, 2, 3];',
0,
[],
],
'One syntax error' => [
'<?php $array = [1, 2, 3; // Missing closing bracket.',
1,
[
1 => [
1 => [
0 => [
'message' => $errorMessage,
'source' => 'Generic.PHP.Syntax.PHPSyntax',
'listener' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\SyntaxSniff',
'severity' => 5,
'fixable' => false,
],
],
],
],
],
'Single error reported even when there is more than syntax error in the file' => [
'<?php $array = [1, 2, 3; // Missing closing bracket.
$anotherArray = [4, 5, 6; // Another missing closing bracket.',
1,
[
1 => [
1 => [
0 => [
'message' => $errorMessage,
'source' => 'Generic.PHP.Syntax.PHPSyntax',
'listener' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\SyntaxSniff',
'severity' => 5,
'fixable' => false,
],
],
],
],
],
];

}//end dataStdIn()


}//end class