updated streamline-setup v2

This commit is contained in:
2025-01-15 08:53:49 -08:00
committed by alec.turner
parent a2ce9248f0
commit 4b569f81b0
20228 changed files with 2932048 additions and 63204 deletions
@@ -12,6 +12,7 @@
namespace PHP_CodeSniffer;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Common;
@@ -226,6 +227,8 @@ class Fixer
* @param boolean $colors Print coloured output or not.
*
* @return string
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException When the diff command fails.
*/
public function generateDiff($filePath=null, $colors=true)
{
@@ -246,19 +249,56 @@ class Fixer
$fixedFile = fopen($tempName, 'w');
fwrite($fixedFile, $contents);
// We must use something like shell_exec() because whitespace at the end
// We must use something like shell_exec() or proc_open() because whitespace at the end
// of lines is critical to diff files.
// Using proc_open() instead of shell_exec improves performance on Windows significantly,
// while the results are the same (though more code is needed to get the results).
// This is specifically due to proc_open allowing to set the "bypass_shell" option.
$filename = escapeshellarg($filename);
$cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\"";
$diff = shell_exec($cmd);
// Stream 0 = STDIN, 1 = STDOUT, 2 = STDERR.
$descriptorspec = [
0 => [
'pipe',
'r',
],
1 => [
'pipe',
'w',
],
2 => [
'pipe',
'w',
],
];
$options = null;
if (stripos(PHP_OS, 'WIN') === 0) {
$options = ['bypass_shell' => true];
}
$process = proc_open($cmd, $descriptorspec, $pipes, $cwd, null, $options);
if (is_resource($process) === false) {
throw new RuntimeException('Could not obtain a resource to execute the diff command.');
}
// We don't need these.
fclose($pipes[0]);
fclose($pipes[2]);
// Stdout will contain the actual diff.
$diff = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
fclose($fixedFile);
if (is_file($tempName) === true) {
unlink($tempName);
}
if ($diff === null) {
if ($diff === false || $diff === '') {
return '';
}
@@ -362,7 +402,7 @@ class Fixer
if ($bt[1]['class'] === __CLASS__) {
$sniff = 'Fixer';
} else {
$sniff = Util\Common::getSniffCode($bt[1]['class']);
$sniff = Common::getSniffCode($bt[1]['class']);
}
$line = $bt[0]['line'];
@@ -447,7 +487,7 @@ class Fixer
$line = $bt[0]['line'];
}
$sniff = Util\Common::getSniffCode($sniff);
$sniff = Common::getSniffCode($sniff);
$numChanges = count($this->changeset);
@@ -504,7 +544,7 @@ class Fixer
$line = $bt[0]['line'];
}
$sniff = Util\Common::getSniffCode($sniff);
$sniff = Common::getSniffCode($sniff);
$tokens = $this->currentFile->getTokens();
$type = $tokens[$stackPtr]['type'];
@@ -619,7 +659,7 @@ class Fixer
$line = $bt[0]['line'];
}
$sniff = Util\Common::getSniffCode($sniff);
$sniff = Common::getSniffCode($sniff);
$tokens = $this->currentFile->getTokens();
$type = $tokens[$stackPtr]['type'];