giacocorsiglia/stubs-generator
Composer 安装命令:
composer require --dev giacocorsiglia/stubs-generator
包简介
Generate stubs from any PHP code for IDE completion and static analysis.
关键字:
README 文档
README
You should use the fork at php-stubs/generator, which is thanks to @szepeviktor.
PHP Stubs Generator
Use this tool to generate stub declarations for functions, classes, interfaces, and global variables defined in any PHP code. The stubs can subsequently be used to facilitate IDE completion or static analysis via Psalm or potentially other tools. Stub generation is particularly useful for code which mixes definitions with side-effects.
The generator is based on nikic's PHP-Parser, and the code also relies on several Symfony components.
Contributions in the form of issue reports or Pull Requests are welcome!
Example
The idea is to turn this:
// source-file-1.php /** * @param string $bar * @return int */ function foo($bar) { return (int) $bar; } /** @var string */ $something = '123abc'; if ($something) { echo foo($something); } // source-file-2.php namespace MyNamespace; class MyClass extends MyParentClass { public function method(): string { return ''; } }
Into this:
// stubs.php namespace { /** * @param string $bar * @return int */ function foo($bar) { } /** @var string */ $something = '123abc'; } namespace MyNamespace { class MyClass extends MyParentClass { public function method(): string { } } }
Command Line Usage
To install:
composer global require giacocorsiglia/stubs-generator
To get the pretty-printed stubs for all the PHP files in a directory:
generate-stubs /path/to/my-library
You may also pass multiple directories, or filenames, separated by spaces. All stubs will be concatenated in the output.
To write the stubs to a file (and see a few statistics in the stdout):
generate-stubs /path/to/my-library --out=/path/to/output.php
For the complete set of command line options:
generate-stubs --help
Usage in PHP
To install:
composer require giacocorsiglia/stubs-generator
Simple Example
// You'll need the Composer Autoloader. require 'vendor/autoload.php'; // You may alias the classnames for convenience. use StubsGenerator\{StubsGenerator, Finder}; // First, instantiate a `StubsGenerator\StubsGenerator`. $generator = new StubsGenerator(); // Then, create a `StubsGenerator\Finder` which contains // the set of files you wish to generate stubs for. $finder = Finder::create()->in('/path/to/my-library/'); // Now you may use the `StubsGenerator::generate()` method, // which will return a `StubsGenerator\Result` instance. $result = $generator->generate($finder); // You can use the `Result` instance to pretty-print the stubs. echo $result->prettyPrint(); // You can also use it to retrieve the PHP-Parser nodes // that represent the generated stub declarations. $stmts = $result->getStubStmts();
Additional Features
You can restrict the set of symbol types for which stubs are generated:
// This will only generate stubs for function declarations. $generator = new StubsGenerator(StubsGenerator::FUNCTIONS); // This will only generate stubs for class or interface declarations. $generator = new StubsGenerator(StubsGenerator::CLASSES | StubsGenerator::INTERFACES);
The set of symbol types are:
StubsGenerator::FUNCTIONS: Function declarations.StubsGenerator::CLASSES: Class declarations.StubsGenerator::TRAITS: Trait declarations.StubsGenerator::INTERFACES: Interface declarations.StubsGenerator::DOCUMENTED_GLOBALS: Global variables, but only those with a doc comment.StubsGenerator::UNDOCUMENTED_GLOBALS: Global variable, but only those without a doc comment.StubsGenerator::GLOBALS: Shortcut to include both documented and undocumented global variables.StubsGenerator::DEFAULT: Shortcut to include everything except undocumented global variables.StubsGenerator::ALL: Shortcut to include everything.
TODO
- Add support for constants declared with
const. - Add support for constants declared with
define().- Consider parsing function and method bodies for these declarations.
giacocorsiglia/stubs-generator 适用场景与选型建议
giacocorsiglia/stubs-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 127.39k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2018 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「static analysis」 「ide」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 giacocorsiglia/stubs-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 giacocorsiglia/stubs-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 giacocorsiglia/stubs-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The Cassandra php driver library IDE stubs which enables autocompletion in modern IDEs.
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
FFI API generator for PHP
Sentiment analysis library for PHP.
Generate stubs from any PHP code for IDE completion and static analysis.
统计信息
- 总下载量: 127.39k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 26
- 点击次数: 14
- 依赖项目数: 13
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-12