定制 ebln/phpstan-factory-rule 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-11-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固