定制 apie/apie-phpstan-rules 二次开发

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

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

apie/apie-phpstan-rules

最新稳定版本:1.0.0-RC2

Composer 安装命令:

composer require apie/apie-phpstan-rules

包简介

Composer package of the apie library: apie phpstan rules

README 文档

README

apie-phpstan-rules

Latest Stable Version Total Downloads Latest Unstable Version License PHP Composer

PHP Composer

This package is part of the Apie library. The code is maintained in a monorepo, so PR's need to be sent to the monorepo

Documentation

Adds phpstan rules specifically for Apie.

Usage:

composer require --dev apie/apie-phpstan-rules

and in your phpstan.neon the include to the neon file.

includes
    - './vendor/apie/apie-phpstan-rules/apie-phpstan-rules.neon'

Added phpstan rules

Entity getId() should be specific

The interface of an entity requires you to have a getId() method with IdentifierInterface as return type. For better reflection it is better if you specify the class it returns instead.

<?php
class ExampleEntity implements EntityInterface
{
    public function __construct(private readonly ExampleEntityIdentifier $id)
    {
    }

    public function getId(): IdentifierInterface
    {
        return $this->id;
    }
}

Should be fixed as:

<?php
class ExampleEntity implements EntityInterface
{
    public function __construct(private readonly ExampleEntityIdentifier $id)
    {
    }

    public function getId(): ExampleEntityIdentifier
    {
        return $this->id;
    }
}

Object should not have conflicting interfaces

Do not make objects that are a value object and entity at the same time for example :)

Value object without constructor

This one is easy to miss when making a value object often when used in combination with composite value objects.

<?php
class ExampleValueObject implements ValueObjectInterface
{
    use CompositeValueObject;

    private string $property;
}

Should be one of these:

<?php
class ExampleValueObject implements ValueObjectInterface
{
    use CompositeValueObject;

    private string $property;

    private function __construct() {
    }
}

Or:

<?php
class ExampleValueObject implements ValueObjectInterface
{
    use CompositeValueObject;

    public function __construct(private string $property)
    {
    }
}

Value Object with array should be composite

Imagine this value object:

<?php
class ExampleValueObject implements ValueObjectInterface
{
    public function __construct(private string $property)
    {
    }

    public static function fromNative(mixed $input): self
    {
        $input = Utils::toArray($input);
        return new self($input['property'] ?? 'missing');
    }

    public function toNative(): array
    {
        return [
            'property' => $this->property
        ];
    }
}

An array typehint is unknown what OpenAPI schema can be created. It can be solved by using CompositeValueObject trait or use the SchemaMethod attribute:

<?php
#[SchemaMethod('provideSchema')]
class ExampleValueObject implements ValueObjectInterface
{
    public function __construct(private string $property)
    {
    }

    public static function fromNative(mixed $input): self
    {
        $input = Utils::toArray($input);
        return new self($input['property'] ?? 'missing');
    }

    public function toNative(): array
    {
        return [
            'property' => $this->property
        ];
    }

    public static function provideSchema(): array
    {
        return [
            'type' => 'object',
            'properties' => [
                'property' => ['type' => 'string', 'default' => 'missing']
            ],
            'required' => ['property']
        ]
    }
}

统计信息

  • 总下载量: 446
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-04