Skip to content

Commit 995d6e9

Browse files
committed
Fixer now uses temporary system files for diffing rather than creating them in the current working dir
1 parent dd8246c commit 995d6e9

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

CodeSniffer/Fixer.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,31 @@ public function generateDiff($filePath=null)
223223
$filePath = $this->_currentFile->getFilename();
224224
}
225225

226-
$cwd = getcwd().DIRECTORY_SEPARATOR;
227-
$filename = str_replace($cwd, '', $filePath);
228-
$fixedFile = $cwd.'phpcs-fixed.tmp';
229-
$contents = $this->getContents();
226+
$cwd = getcwd().DIRECTORY_SEPARATOR;
227+
$filename = str_replace($cwd, '', $filePath);
228+
$contents = $this->getContents();
229+
230+
if (function_exists('sys_get_temp_dir') === true) {
231+
// This is needed for HHVM support, but only available from 5.2.1.
232+
$tempName = tempnam(sys_get_temp_dir(), 'phpcs-fixer');
233+
$fixedFile = fopen($tempName, 'w');
234+
} else {
235+
$fixedFile = tmpfile();
236+
$data = stream_get_meta_data($fixedFile);
237+
$tempName = $data['uri'];
238+
}
230239

231-
file_put_contents($fixedFile, $contents);
240+
fwrite($fixedFile, $contents);
232241

233242
// We must use something like shell_exec() because whitespace at the end
234243
// of lines is critical to diff files.
235-
$cmd = "diff -u -L\"$filename\" -LPHP_CodeSniffer \"$filename\" \"$fixedFile\"";
244+
$cmd = "diff -u -L\"$filename\" -LPHP_CodeSniffer \"$filename\" \"$tempName\"";
236245
$diff = shell_exec($cmd);
237-
unlink($fixedFile);
246+
247+
fclose($fixedFile);
248+
if (is_file($tempName) === true) {
249+
unlink($tempName);
250+
}
238251

239252
return $diff;
240253

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
2727
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
2828
<notes>
2929
- Improved default output for PHPCBF and removed the options to print verbose and progress output
30+
- Fixer now uses temporary system files for diffing rather than creating them in the current working dir
3031
- If a .fixed file is supplied for a unit test file, the auto fixes will be checked against it during testing
3132
-- See Generic ScopeIndentUnitTest.inc and ScopeIndentUnitTest.inc.fixed for an example
3233
- Fixer token replacement methods now return TRUE if the change was accepted and FALSE if rejected

0 commit comments

Comments
 (0)