Skip to content

feat: add optional rule to report properties that should be promoted #4115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ parameters:
reportPossiblyNonexistentGeneralArrayOffset: false
reportPossiblyNonexistentConstantArrayOffset: false
checkMissingOverrideMethodAttribute: false
reportPropertiesThatShouldBePromoted: false
mixinExcludeClasses: []
scanFiles: []
scanDirectories: []
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ parametersSchema:
reportPossiblyNonexistentGeneralArrayOffset: bool()
reportPossiblyNonexistentConstantArrayOffset: bool()
checkMissingOverrideMethodAttribute: bool()
reportPropertiesThatShouldBePromoted: bool()
parallel: structure([
jobSize: int(),
processTimeout: float(),
Expand Down
107 changes: 107 additions & 0 deletions src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Properties;

use Override;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Error;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;

/**
* @implements Rule<ClassMethod>
*/
#[RegisteredRule(level: 0)]
final class ReportPropertiesThatShouldBePromotedRule implements Rule
{

public function __construct(
#[AutowiredParameter]
private bool $reportPropertiesThatShouldBePromoted,
)
{
}

public function getNodeType(): string
{
return ClassMethod::class;
}

#[Override]
public function processNode(Node $node, Scope $scope): array
{
if (
! $this->reportPropertiesThatShouldBePromoted
|| $node->name->toLowerString() !== '__construct'
|| $node->params === null

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 47 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.
) {

Check failure on line 48 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.

Check failure on line 48 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Strict comparison using === between array<PhpParser\Node\Param> and null will always evaluate to false.
return [];
}
$errors = [];
foreach ($node->params as $param) {
if (
$param->isPromoted()
|| $param->var instanceof Error
|| $param->var->name instanceof Expr
|| ! $this->assignsUnmodifiedVariableToProperty($param->var->name, $node)
) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf('Property [%s] should be promoted.', $param->var->name))
->identifier('property.shouldBePromoted')
->line($param->var->getStartLine())
->build();
}
return $errors;
}

private function assignsUnmodifiedVariableToProperty(string $variable, ClassMethod $node): bool
{
foreach ($node->getStmts() as $stmt) {

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 71 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.
if (! $stmt instanceof Expression || ! $stmt->expr instanceof Assign) {

Check failure on line 72 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.

Check failure on line 72 in src/Rules/Properties/ReportPropertiesThatShouldBePromotedRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Argument of an invalid type array<PhpParser\Node\Stmt>|null supplied for foreach, only iterables are supported.
continue;
}
$var = $stmt->expr->var;
$expr = $stmt->expr->expr;
if (! $var instanceof Variable && ! $var instanceof PropertyFetch) {
continue;
}
if ($var instanceof Variable) {
// The variable has been modified, so can't promote it.
if ($var->name === $variable) {
return false;
}
continue;
}
if (
! $var->var instanceof Variable
|| $var->var->name !== 'this'
|| ! $var->name instanceof Identifier
|| $var->name->toString() !== $variable
) {
continue;
}
if (! $expr instanceof Variable) {
continue;
}
// The variable is being assigned to a property
// of the same name, safe to promote it.
if ($expr->name === $variable) {
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Properties;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use function sprintf;

/**
* @extends RuleTestCase<ReportPropertiesThatShouldBePromoted>
*/
class ReportPropertiesThatShouldBePromotedRuleTest extends RuleTestCase

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted in generic type PHPStan\Testing\RuleTestCase<PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted> in PHPDoc tag @extends is not subtype of template type TRule of PHPStan\Rules\Rule of class PHPStan\Testing\RuleTestCase.

Check failure on line 13 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

PHPDoc tag @extends has invalid type PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.
{

protected function getRule(): Rule

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType

Check failure on line 16 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType
{
return new ReportPropertiesThatShouldBePromoted(true);

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Method PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromotedRuleTest::getRule() should return PHPStan\Rules\Rule but returns PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted.

Check failure on line 18 in tests/PHPStan/Rules/Properties/ReportPropertiesThatShouldBePromotedRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Instantiated class PHPStan\Rules\Properties\ReportPropertiesThatShouldBePromoted not found.
}

#[RequiresPhp('>= 8.0')]
public function testRule(): void
{
$error = static fn (string $property) => sprintf('Property [%s] should be promoted.', $property);

$this->analyse([__DIR__ . '/data/properties-that-should-be-promoted.php'], [
[$error('name'), 20],
]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace PropertiesThatShouldBePromoted;

class PromotedProperties
{
public string $name;

/** @var list<string> */
public array $tags;

/** @var array<string, mixed> */
public array $options;

public int $count;

public int $foo;

public function __construct(
int $count,
?string $name,
public ?string $email,
/** @var array<int, string> */
array $tags,
/** @var array<string, mixed> */
array $options,
int $bar,
) {
$this->count = $count;

$tags = array_values($tags);
$this->tags = $tags;
$this->options = array_filter($options);
$this->email ??= '[email protected]';
$this->name = $name ?? 'Default Name';
$this->foo = $bar;
}
}
Loading