Skip to content

Instantly share code, notes, and snippets.

@motebaya
Created August 1, 2022 11:52
Show Gist options
  • Save motebaya/5bacab9216e6226eaa0107439aa066fc to your computer and use it in GitHub Desktop.
Save motebaya/5bacab9216e6226eaa0107439aa066fc to your computer and use it in GitHub Desktop.
snippet php for format php code using php parser
<?php
/*
https://github.com/nikic/PHP-Parser/blob/master/doc/2_Usage_of_basic_components.markdown
https://github.com/nikic/PHP-Parser/blob/master/doc/component/Pretty_printing.markdown
*/
require "vendor/autoload.php";
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter;
if (count($argv) > 1) {
$prettyPrinter = new PrettyPrinter\Standard;
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$p = $parser->parse(file_get_contents($argv[1]));
echo str_replace(" ","\t",$prettyPrinter->prettyPrint($p));
} else {
die(" usage : prettify.php <file.php>");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment