tzmfreedom/phpstan-extensions

PHPStan 扩展

安装: 7

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:phpstan-extension

v0.1.7 2024-04-07 12:18 UTC

This package is auto-updated.

Last update: 2024-09-07 13:24:55 UTC


README

VisibleForTestingRule

PHPStan 自定义规则,确保在测试环境外部的私有/受保护作用域中调用带有 VisibleForTesting 注释的公共方法,灵感来源于 Flutter、Java (Guava) 的 @VisibleForTesting 注释

在以下代码中,此扩展在测试环境外部报告错误。

<?php

use Tzmfreedom\Attributes\VisibleForTesting;

class Foo
{
    #[VisibleForTesting]
    public function exampleWithAttribute()
    {}

    /**
     * @visibleForTesting
     */
    public function exampleWithPhpdoc()
    {}
}

(new Foo)->exampleWithAttribute();
// error: VisibleForTesting annotated method Foo::visibleForTestingWithAttribute should be called in private scope outside of the test environment

UnusedReturnRule

<?php

class Foo
{
    public function getString(): string
    {
        return '';
    }
}

(new Foo)->getString(); // error: Return value on Method Foo::getString() is unused
$_ = (new Foo)->getString(); // OK

OverwriteVariableRule

<?php

$var = null;
$var = 'hoge'; // OK, changing from null to any
$var = 'fuga'; // NG

OverwriteDifferentTypeVariableRule

<?php

$var = null;
$var = 'hoge'; // OK, changing from null to any
$var = 1; // NG, changing from string to integer
$var = 1.0; // OK, changing from integer to float
$var = 1; // OK, changeing from float to integer
$var = new \stdClass(); // NG
$var = new class extends \stdClass{}; // OK
$var = new \stdClass(); // OK

安装

$ composer require --dev tzmfreedom/phpstan-extensions

phpstan.neon

rules:
	- Tzmfreedom\PHPStan\Rules\VisibleForTestingRule
	- Tzmfreedom\PHPStan\Rules\UnusedReturnRule