Skip to content

Commit b2b0d85

Browse files
committed
Create a temporary file when processing STDIN on Windows
1 parent 1938222 commit b2b0d85

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

src/Standards/Generic/Sniffs/PHP/SyntaxSniff.php

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function process(File $phpcsFile, $stackPtr)
5656
$this->phpPath = Config::getExecutablePath('php');
5757
}
5858

59-
$cmd = $this->getPhpLintCommand($phpcsFile);
59+
$cmd = $this->getLintCommand($phpcsFile);
6060
$output = shell_exec($cmd);
6161
$matches = [];
6262
if (preg_match('/^.*error:(.*) in .* on line ([0-9]+)/m', trim($output), $matches) === 1) {
@@ -72,24 +72,74 @@ public function process(File $phpcsFile, $stackPtr)
7272

7373

7474
/**
75-
* Returns the command used to lint PHP code. Uses a different command when the content is
76-
* provided via STDIN.
75+
* Returns the command used to lint PHP code.
76+
*
77+
* This method handles different scenarios based on the input source:
78+
* - For STDIN input on Windows: Creates a temporary file and uses direct file linting.
79+
* - For STDIN input on non-Windows: Pipes the content through echo to PHP linter.
80+
* - For regular files: Uses direct file linting.
7781
*
7882
* @param \PHP_CodeSniffer\Files\File $phpcsFile The File object.
7983
*
8084
* @return string The command used to lint PHP code.
8185
*/
82-
private function getPhpLintCommand(File $phpcsFile)
86+
private function getLintCommand(File $phpcsFile)
8387
{
8488
if ($phpcsFile->getFilename() === 'STDIN') {
8589
$content = $phpcsFile->getTokensAsString(0, $phpcsFile->numTokens);
90+
91+
if (stripos(PHP_OS, 'WIN') === 0) {
92+
$tempFile = $this->createTempFile($content);
93+
return $this->getFileLintCommand($tempFile);
94+
}
95+
8696
return "echo ".escapeshellarg($content)." | ".Common::escapeshellcmd($this->phpPath)." -l -d display_errors=1 -d error_prepend_string='' 2>&1";
87-
}
97+
}//end if
98+
99+
return $this->getFileLintCommand($phpcsFile->getFilename());
100+
101+
}//end getLintCommand()
102+
103+
104+
/**
105+
* Creates a temporary file with the given content and registers a shutdown function to delete
106+
* the file when the script ends.
107+
*
108+
* @param string $content The content to write to the temporary file.
109+
*
110+
* @return string The path to the created temporary file.
111+
*/
112+
private function createTempFile($content)
113+
{
114+
$tempFile = tempnam(sys_get_temp_dir(), 'phpcs-syntax-sniff');
115+
file_put_contents($tempFile, $content);
116+
117+
register_shutdown_function(
118+
function () use ($tempFile) {
119+
if (file_exists($tempFile) === true) {
120+
unlink($tempFile);
121+
}
122+
}
123+
);
124+
125+
return $tempFile;
126+
127+
}//end createTempFile()
128+
88129

89-
$fileName = escapeshellarg($phpcsFile->getFilename());
90-
return Common::escapeshellcmd($this->phpPath)." -l -d display_errors=1 -d error_prepend_string='' $fileName 2>&1";
130+
/**
131+
* Returns the command used to lint a specific PHP file.
132+
*
133+
* @param string $fileName The name of the file to be checked.
134+
*
135+
* @return string The command used to lint the specified PHP file.
136+
*/
137+
private function getFileLintCommand($fileName)
138+
{
139+
$escapedFileName = escapeshellarg($fileName);
140+
return Common::escapeshellcmd($this->phpPath)." -l -d display_errors=1 -d error_prepend_string='' $escapedFileName 2>&1";
91141

92-
}//end getPhpLintCommand()
142+
}//end getFileLintCommand()
93143

94144

95145
}//end class

0 commit comments

Comments
 (0)