jcnventura / pharborist
Pharborist为PHP构建一个可以遍历和操作的语法树。
dev-master
2023-05-04 07:21 UTC
Requires
- php: >=5.4
- phpdocumentor/reflection-docblock: ^2.0|^3.0.2|^4.0|^5.0
Requires (Dev)
- apigen/apigen: 2.8.*
- phpunit/phpunit: 4.2.*
This package is not auto-updated.
Last update: 2024-09-19 13:42:59 UTC
README
一个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); }