Skip to content

Instantly share code, notes, and snippets.

@motebaya
Created January 21, 2025 15:24
Show Gist options
  • Save motebaya/c3e85694802332e3bc799340a12d487a to your computer and use it in GitHub Desktop.
Save motebaya/c3e85694802332e3bc799340a12d487a to your computer and use it in GitHub Desktop.
php-parser 4.x to format/beautify php code without any installed composer.
<?php
/**
* no need composer
* @github.com/motebaya - 2025-01-19 19:36:32.659806400 +0700
* @reff: https://github.com/pk-fr/yakpro-po/blob/master/include/classes/parser_extensions/my_autoloader.php
*/
namespace PhpParser;
ini_set('memory_limit', -1);
define("PHP_PARSER_DIRECTORY", "PHP-Parser");
/**
* @codeCoverageIgnore
*/
class Autoloader
{
/** @var bool Whether the autoloader has been registered. */
private static $registered = false;
/**
* Registers PhpParser\Autoloader as an SPL autoloader.
*
* @param bool $prepend Whether to prepend the autoloader instead of appending
*/
public static function register(bool $prepend = false)
{
if (self::$registered === true) {
return;
}
spl_autoload_register([__CLASS__, 'autoload'], true, $prepend);
self::$registered = true;
}
/**
* Handles autoloading of classes.
*
* @param string $class A class name.
*/
public static function autoload(string $class)
{
if (0 === strpos($class, 'PhpParser\\')) {
$fileName = __DIR__ . '/' . PHP_PARSER_DIRECTORY . '/lib/' . strtr($class, '\\', '/') . '.php';
if (file_exists($fileName)) {
require $fileName;
}
}
}
/**
* install php parser 4.x
*
* @return void
*/
public static function get_parserdir(): void
{
exec(sprintf(
"git --version %s",
strtolower(PHP_OS) === 'winnt' ?
'2>NUL' : '2>/dev/null'
), $version, $status_code);
if ($status_code != 0) {
echo " git isn’t installed on your device.
please install it first or manually download it from the following repo link:
- https://github.com/nikic/PHP-Parser/tree/4.x/.
unzip the folder and place it in the same directory as your project." . PHP_EOL . PHP_EOL;
die;
} else {
echo 'installed ' . $version[0] . PHP_EOL;
exec(
"git clone https://github.com/nikic/PHP-Parser.git --branch 4.x ",
$outp,
$status_code
);
if ($status_code === 0) {
echo "Successfully installed php-parser 4.x" . PHP_EOL;
return;
} else {
echo 'Failed cloning php-parser repo' . PHP_EOL;
echo implode("\n", $outp);
die;
}
}
}
/**
* start
*
* @return void
*/
public static function start(): void
{
$parserDir = __DIR__ . '/' . PHP_PARSER_DIRECTORY;
if (!is_dir($parserDir)) {
self::get_parserdir();
}
self::register();
}
}
Autoloader::start();
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter;
function prettify(string $cofile): ?string
{
$prettyPrinter = new PrettyPrinter\Standard;
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$code = (is_file($cofile)) ? file_get_contents($cofile) : $cofile;
if (!preg_match('/^\<\?php/i', $code)) {
$code = "<?php\n" . $code;
}
$p = $parser->parse($code);
return $prettyPrinter->prettyPrintFile($p);
}
// $code = prettify('testooo.php');
// echo $code;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment