Sample code: rewriting php-ast(Abstract Syntax Trees) into a form that is easier to analyze
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples
src
tests
LICENSE
README.md
composer.json
simplify.php

README.md

PHP AST Normalizer

Overview

This is a program applying a set of rules to rewrite php ASTs(Abstract Syntax Trees) into a form that is easier to analyze. The currently use AST format is from https://github.com/nikic/php-ast/issues (version 35).

It can be used as a step prior to static analysis, as described in https://github.com/etsy/phan/issues/405

Running

Prerequisites: php-ast must be installed.

This can be passed either an expression or a path to a file

php simplify.php -e '<?php if (!is_string($a)) { return; } bar();'
php simplify.php examples/if_and.php

Supported Rules

// Applied whether or not there is an else. $var must be a variable, not a property or a field.
if ($var = A) {X} -> $var = A; if ($var){B}
// Do this, unless there is a else statement that can be executed
if (A && B) {X} -> if (A) { if (B) { X }}
// Remove double negation, whether or not there is an else statement
if (!!A) {X} -> if (A) {X}
// The below rule also applies when B_ALWAYS_RETURNS performs a break/continue in a for/while loop.
if (A} {Y_ALWAYS_RETURNS} X_REMAINING -> if (A) {Y_ALWAYS_RETURNS} else {X_REMAINING}
if (A} {Y_ALWAYS_RETURNS} else {Z} X_REMAINING -> if (A) {Y_ALWAYS_RETURNS} else {Z; X_REMAINING}
// If one of the branches never returns, both can go into the other branch in the normalized form
// (Other forms of this exist)
if (!A) {X} else {Y} -> if(A) {Y} else { X }