ebln/phpstan-factory-rule
Composer 安装命令:
composer require ebln/phpstan-factory-rule
包简介
PHPStan rule to enforce instanciation by factories
README 文档
README
Enforce that your classes get only instantiated by the factories you define!
Usage with support for attributes
Require this package: composer require --dev ebln/phpstan-factory-rule
Add the ForceFactory attribute to your class, and supply all class names as arguments,
which shall be allowed to instanciate your object.
<?php // […] #[\Ebln\Attrib\ForceFactory(GoodFactory::class, BetterFactory::class)] class OnlyViaFactory { }
Now lean back and rely on PHPStan in CI pipelines, git hooks or IDE integrations; If somebody introduces a rogue factory:
<?php // […] class FailingFactory { public function create(): OnlyViaFactory { return new OnlyViaFactory(); } }
…that is supposed to fail, when you run PHPStan.
Deprecated usage with ebln/phpstan-factory-mark
Require this extention and the package containing the marking interface via Composer alongside with PHPStan:
composer require ebln/phpstan-factory-mark composer require --dev ebln/phpstan-factory-rule
Implement \Ebln\PHPStan\EnforceFactory\ForceFactoryInterface with the DTO you want to protect.
<?php // […] use Ebln\PHPStan\EnforceFactory\ForceFactoryInterface; class OnlyViaFactory implements ForceFactoryInterface { // […] public static function getFactories(): array { // Return a list of classes that are allowed to // create new OnlyViaFactory instances… return [TheOnlyTrueFactory::class]; } }
Now rely on PHPStan in CI pipelines, git hooks or IDE integrations.
If somebody introduces a rogue factory:
<?php // […] class FailingFactory { public function create(): OnlyViaFactory { return new OnlyViaFactory(); } }
…that is supposed to fail, when you run PHPStan.
Installation
Require this extention via Composer:
composer require --dev ebln/phpstan-factory-rule
If you also install phpstan/extension-installer then you're all set!
Manual installation
If you don't want to use phpstan/extension-installer, just specify the rule in your project's PHPStan config:
rules:
- \Ebln\PHPStan\EnforceFactory\ForceFactoryRule
统计信息
- 总下载量: 19.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-12