memio / spec-gen
用于更好的代码生成的phpspec扩展
v0.10.0
2019-12-02 16:22 UTC
Requires
- php: ^7.2
- gnugat/redaktilo: ^2.0
- memio/linter: ^2.0
- memio/memio: ^2.0
- memio/model: ^2.0.1
- memio/pretty-printer: ^2.0
- memio/twig-template-engine: ^2.0.1
- memio/validator: ^2.0
- phpspec/phpspec: ^6.1
- symfony/event-dispatcher: ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^8.4
README
此扩展为phpspec提供了一个强大的代码生成器
- 方法生成
- 它在类的末尾插入方法
- 它使用类型提示参数(尽可能使用接口)
- 它根据对象的类型命名参数(从名称中删除
Interface
后缀) - 它根据通用名称命名标量参数(例如
argument
) - 它为可能发生冲突的名称添加数字(例如
$argument1, $argument2
)
- 构造函数生成,与方法的生成相同,除了
- 它将构造函数插入类的开头
- 它为每个构造函数参数插入具有初始化的属性
注意:目前无法为SpecGen提供自定义模板(它与phpspec模板不兼容)。
安装
首先使用Composer安装它
$ composer require --dev memio/spec-gen:^0.10
然后在phpspec.yml
中启用它
extensions: Memio\SpecGen\MemioSpecGenExtension: ~
版本指南:
- 使用phpspec 5?则使用spec-gen v0.9
- 使用phpspec 4?则使用spec-gen v0.8
- 使用phpspec 3和PHP 7?则使用spec-gen v0.7
- 使用phpspec 3和PHP 5.6?则使用spec-gen v0.6
- 使用phpspec 2?则使用spec-gen v0.4
完整示例
让我们编写以下规范
<?php namespace spec\Vendor\Project; use Vendor\Project\Service\Filesystem; use Vendor\Project\File; use PhpSpec\ObjectBehavior; class TextEditorSpec extends ObjectBehavior { const FILENAME = '/tmp/file.txt'; const FORCE_FILE_CREATION = true; function let(Filesystem $filesystem) { $this->beConstructedWith($filesystem); } function it_creates_new_files(File $file, Filesystem $filesystem) { $filesystem->exists(self::FILENAME)->willReturn(false); $filesystem->create(self::FILENAME)->willReturn($file); $this->open(self::FILENAME, self::FORCE_FILE_CREATION)->shouldBe($file); } }
执行规范(phpspec run
)将生成以下类
<?php namespace Vendor\Project; use Vendor\Project\Service\Filesystem; class TextEditor { private $filesystem; public function __construct(Filesytem $filesystem) { $this->filesystem = $filesystem; } public function open(string $argument1, bool $argument2) { } }
现在我们可以开始命名这些通用参数并编写代码。
想要了解更多?
您可以使用以下方式查看当前版本和历史版本:
git tag
命令- GitHub上的发布页面
- 版本之间变更的文件列表
最后是一些元文档
路线图
- 构造函数属性提升
- 返回类型提示
- 方法体(测试方法体的镜像)
- 更好的标量参数命名(基于测试中使用的名称)