Skip to content

Commit 6d4945c

Browse files
authored
PrettierPHPFixer - Use output of exec directly instead of writing temporary file (#1483)
1 parent c2b53d7 commit 6d4945c

2 files changed

Lines changed: 10 additions & 34 deletions

File tree

docs/recipes/php-cs-fixer/PrettierPHPFixer.php

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,13 @@ public function supports(SplFileInfo $file) {
5858
}
5959

6060
/**
61-
* {@inheritdoc}
62-
*/
63-
private function applyFix(SplFileInfo $file, Tokens $tokens) {
64-
$tmpFile = $this->getTmpFile($file);
65-
exec("yarn exec -- prettier --write $tmpFile");
66-
67-
$content = file_get_contents($tmpFile);
68-
$tokens->setCode($content);
69-
70-
(new Filesystem())->remove($tmpFile);
71-
}
72-
73-
/**
74-
* Create a Temp file with the same content as given file.
75-
*
76-
* @param SplFileInfo $file file to be copied
77-
*
78-
* @return string tmp file name
61+
* @param SplFileInfo $file
62+
* @param Tokens $tokens
7963
*/
80-
private function getTmpFile(SplFileInfo $file): string {
81-
$fileSys = new Filesystem();
82-
$tmpFolderPath = __DIR__.DIRECTORY_SEPARATOR.'tmp';
83-
$fileSys->mkdir($tmpFolderPath);
84-
85-
$tmpFileName = str_replace(
86-
array(DIRECTORY_SEPARATOR, ':'),
87-
'_',
88-
$file->getRealPath()
89-
);
90-
$tmpFilePath = $tmpFolderPath.DIRECTORY_SEPARATOR.'__'.$tmpFileName;
91-
$fileSys->copy($file->getRealPath(), $tmpFilePath, true);
92-
return $tmpFilePath;
64+
private function applyFix(SplFileInfo $file, Tokens $tokens): void
65+
{
66+
exec("yarn exec -- prettier $file", $prettierOutput);
67+
$code = implode(PHP_EOL, $prettierOutput);
68+
$tokens->setCode($code);
9369
}
9470
}

docs/recipes/php-cs-fixer/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
applied, such that the `php-cs-fixer` user's configurations is respected.
77

88
## Useful Configurations
9-
9+
1010
### Priority
1111

1212
If you would like `prettier` to execute last, which means you prefer to use
@@ -21,8 +21,8 @@
2121

2222
For example,
2323
```diff
24-
- exec("yarn exec -- prettier --write $tmpFile");
25-
+ exec("yarn exec -- prettier --write --brace-style=1tbs $tmpFile");
24+
- exec("yarn exec -- prettier $file");
25+
+ exec("yarn exec -- prettier --brace-style=1tbs $file");
2626
```
2727
will allow you to change the `braceStyle` for this fixer
2828

0 commit comments

Comments
 (0)