-
-
Save alialobidm/a271e9a0a7f26adf6a1cf970d6bc8ee0 to your computer and use it in GitHub Desktop.
A Customised fixer for PHP-CS-Fixer to use `prettier`'s php plugin to pre-process the code before getting analysed by other fixers. This would enable `php-cs-fixer` to take advantage of `prettier`'s ability of managing long line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once __DIR__.'/relative/path/to/PrettierPHPFixer/File'; | |
| return PhpCsFixer\Config::create() | |
| ->registerCustomFixers([ | |
| (new PrettierPHPFixer()), | |
| ]) | |
| ->setRules([ | |
| 'Prettier/php' => true, | |
| ]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use PhpCsFixer\Fixer\FixerInterface; | |
| use PhpCsFixer\Tokenizer\Tokens; | |
| use Symfony\Component\Filesystem\Filesystem; | |
| /** | |
| * Fixer for using prettier-php to fix. | |
| */ | |
| final class PrettierPHPFixer implements FixerInterface { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getPriority() { | |
| // should be absolute first | |
| return 999; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function isCandidate(Tokens $tokens) { | |
| return true; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function isRisky() { | |
| return false; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function fix(SplFileInfo $file, Tokens $tokens) { | |
| if ( | |
| 0 < $tokens->count() && | |
| $this->isCandidate($tokens) && | |
| $this->supports($file) | |
| ) { | |
| $this->applyFix($file, $tokens); | |
| } | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getName() { | |
| return 'Prettier/php'; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function supports(SplFileInfo $file) { | |
| return true; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| private function applyFix(SplFileInfo $file, Tokens $tokens) { | |
| $tmpFile = $this->getTmpFile($file); | |
| exec("yarn exec -- prettier --write --brace-style=1tbs $tmpFile"); | |
| $content = file_get_contents($tmpFile); | |
| $tokens->setCode($content); | |
| (new Filesystem())->remove($tmpFile); | |
| } | |
| /** | |
| * Create a Temp file with the same content as given file. | |
| * | |
| * @param SplFileInfo $file file to be copied | |
| * | |
| * @return string tmp file name | |
| */ | |
| private function getTmpFile(SplFileInfo $file): string { | |
| $fileSys = new Filesystem(); | |
| $tmpFolderPath = __DIR__.'/tmp'; | |
| $fileSys->mkdir($tmpFolderPath); | |
| $tmpFileName = str_replace( | |
| DIRECTORY_SEPARATOR, | |
| '_', | |
| $file->getRealPath() | |
| ); | |
| $tmpFilePath = $tmpFolderPath.'/__'.$tmpFileName; | |
| $fileSys->copy($file->getRealPath(), $tmpFilePath, true); | |
| return $tmpFilePath; | |
| } | |
| } |
Author
alialobidm
commented
Sep 28, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment