Skip to content

Instantly share code, notes, and snippets.

@sbassett29
Created February 8, 2024 22:36
Show Gist options
  • Save sbassett29/4ec8caf090fc02bfe2506449c39e9ecf to your computer and use it in GitHub Desktop.
Save sbassett29/4ec8caf090fc02bfe2506449c39e9ecf to your computer and use it in GitHub Desktop.
php cli ast generation
#!/usr/bin/env php
<?php
declare(strict_types=1);
define( "AST_PHP_VERSION", 80 );
if( PHP_SAPI !== 'cli' ) {
exit("Please run as a PHP CLI!");
}
if( ! isset($argv[1]) ) {
exit("Please provide a file name or string of PHP code as an argument!");
}
if( ! in_array( "ast", get_loaded_extensions() ) ) {
exit("Please ensure the php/ast module is installed!");
}
$ast = null;
if( is_file( $argv[1] ) ) {
$ast = ast\parse_file( $argv[1], $version=AST_PHP_VERSION );
}
else if( is_string( $argv[1] ) ) {
$ast = ast\parse_code( $argv[1], $version=AST_PHP_VERSION );
}
else {
exit("A file name or valid PHP code string were not provided as an argument!");
}
if( $ast !== null ) {
echo $ast;
}
else {
exit("AST unable to be generated!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment