A PHP library to query and transform PHP source code via tree operations.
- Tests with 100% code coverage
- Integration with a third party library (suggestions welcomed) for extracting PHPDoc comments
- API to ease querying and transforming of the syntax tree
- Build a PHP source code formatter using the library
- Build a PHP code check using the library
Below is an example of how the API might look once its more developed:
// Add use declaration if it does not already exist. Use UtilityString alias if conflict
$alias = $tree->ensureUseDeclaration('Drupal\Component\Utility\String', 'UtilityString');
// Find all calls to check_plain and rename them to use String::checkPlain
$function_calls = $tree->find(Filter::functionCall('check_plain'));
foreach ($function_calls as $call) {
$class_method_call = ClassMethodCallNode::create($alias, 'check_plain', $call->getArgumentList());
$call->replaceWith($class_method_call);
}
require_once 'vendor/autoload.php';
use Pharborist\Parser;
use Pharborist\NamespaceNode;
$tree = Parser::parseFile($filename);
// check there only one namespace declaration
$namespaces = $tree->children(Filter::isInstanceOf(NamespaceNode::class));
if ($namespace->count() > 1) {
die('More then one namespace at line ' . $namespaces[1]->getSourcePosition());
}
Pharborist's API is documented at http://www.phenaproxima.net/pharborist. Pharborist is still under heavy development, so it changes fairly frequently.