Skip to content

Instantly share code, notes, and snippets.

@piyush1104
Forked from Billz95/.php_cs.dist
Last active May 17, 2020
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__.'/PrettierPHPFixer.php';
$finder = PhpCsFixer\Finder::create()
->exclude('somedir')
->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php')
->in(__DIR__)
;
return PhpCsFixer\Config::create()
->registerCustomFixers([
(new PrettierPHPFixer()),
])
->setRules([
'Prettier/php' => true,
'@PSR2' => true,
])
->setFinder($finder);
<?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);
// If you want to give custom config (though if you name it prettierrc, it automatically uses it)
// $configFile = __DIR__ . '/.prettierrc';
// system("prettier --write --config {$configFile} $tmpFile");
// system command as I have installed prettier and exec was not working.
system("prettier --write $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()
);
// add php extension to enable prettier php plugin.
$tmpFilePath = $tmpFolderPath . '/__' . $tmpFileName . '.php';
$fileSys->copy($file->getRealPath(), $tmpFilePath, true);
return $tmpFilePath;
}
}
@piyush1104

This comment has been minimized.

Copy link
Owner Author

@piyush1104 piyush1104 commented May 17, 2020

I had installed prettier and prettier php plugin globally using npm. I did not know how to exec using npm and alse php exec was not working so I used system function. Also this php plugin only works on the .php extension, so it was actually not formatting the temp file. Hence I appended php extension.

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