Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.
<?php
require_once __DIR__.'/relative/path/to/PrettierPHPFixer/File';
return PhpCsFixer\Config::create()
->registerCustomFixers([
(new PrettierPHPFixer()),
])
->setRules([
'Prettier/php' => true,
]);
<?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;
}
}
@Billz95

This comment has been minimized.

Copy link
Owner Author

@Billz95 Billz95 commented Jan 31, 2019

@Billz95

This comment has been minimized.

Copy link
Owner Author

@Billz95 Billz95 commented Feb 1, 2019

Prettier integration to php-cs-fixer

This recipe uses prettier/plugin-php as a Fixer for php-cs-fixer.

prettier will be executed at the very beginning before the other fixers are
applied, such that the php-cs-fixer user's configurations is respected.

Useful Configurations

Priority

If you would like prettier to execute last, which means you prefer to use
php-cs-fixer to complement the current missing features of prettier, you
can decrease the priority value of this fixer by decreasing the value returned
by getPriority function to something like -999

Prettier's native configuration

If you would like to add configuration settings for prettier to this Fixer,
you can modify the exec line in applyFix function.

For example,

  - exec("yarn exec -- prettier --write --brace-style=1tbs $tmpFile");
  + exec("yarn exec -- prettier --write --brace-style=psr-2 $tmpFile");

will allow you to change the braceSytle for this fixer

Possible Improvement

  • the configuration can be modified from php-cs-fixer configuration
  • autoloading
@piyush1104

This comment has been minimized.

Copy link

@piyush1104 piyush1104 commented May 17, 2020

Thanks for this. I had to make few changes to make it work. For those having some problems can look at my fork - https://gist.github.com/piyush1104/5eba790d07834b2efbb5ac7e690e6555. It might work for you then.

@Billz95

This comment has been minimized.

Copy link
Owner Author

@Billz95 Billz95 commented Jun 18, 2020

Thank you for your interest, also if you have any improvement that will make it work better, feel free to create a pull request for prettier directly as this recipe is included in prettier repo at https://github.com/prettier/plugin-php/blob/master/docs/recipes/php-cs-fixer/, cheers.

@hansgrinwis

This comment has been minimized.

Copy link

@hansgrinwis hansgrinwis commented Jun 18, 2020

Thank you for your interest, also if you have any improvement that will make it work better, feel free to create a pull request for prettier directly as this recipe is included in prettier repo at https://github.com/prettier/plugin-php/blob/master/docs/recipes/php-cs-fixer/, cheers.

Sorry, I already saw that my comments were already incorporated in the repo you are mentioning. I deleted my comments just before you posted your reply. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment