grom358 / pharborist

此包已被废弃且不再维护。没有推荐替代包。

Pharborist 为 PHP 构建一个可以遍历和操作的语法树。

dev-master 2015-09-20 22:14 UTC

README

Pharborist logo

一个 PHP 库,通过树操作查询和转换 PHP 源代码。

路线图

  • 100% 代码覆盖率测试
  • 与第三方 PHPDoc 注释库集成
  • API 简化查询和转换语法树
  • 使用库构建 PHP 源代码格式化工具(部分完成)。
  • 使用库构建 PHP 代码检查工具

以下是 API 在更完善后可能的样子示例

// 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\Namespaces\NamespaceNode;
use Pharborist\Filter;

$filename = $argv[1];
$tree = Parser::parseFile($filename);

// check there only one namespace declaration
$namespaces = $tree->children(Filter::isInstanceOf('\Pharborist\Namespaces\NamespaceNode'));
if ($namespaces->count() > 1) {
  die('More then one namespace at line ' . $namespaces[1]->getLineNumber() . PHP_EOL);
}

Build Status