tzmfreedom/phpstan-extensions

PHPStan extensions

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

This package is auto-updated.

Last update: 2024-05-07 12:32:31 UTC


README

VisibleForTestingRule

PHPStan custom rules to ensure that VisibleForTesting annotated public methods are called in private/protected scopes outside of the test environment, inspired by @VisibleForTesting annotation on Flutter, Java (Guava)

In following code, this extension report error outside of the test environment.

<?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

Installation

$ composer require --dev tzmfreedom/phpstan-extensions

phpstan.neon

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