Skip to content

Commit

Permalink
Playground rule - PromoteParameterRule
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 5, 2025
1 parent 7f90fec commit 59ccf55
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Rules/Playground/PromoteParameterRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Playground;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;

/**
* @template TNodeType of Node
* @implements Rule<TNodeType>
*/
final class PromoteParameterRule implements Rule
{

/**
* @param Rule<TNodeType> $rule
* @param class-string<TNodeType> $nodeType
*/
public function __construct(
private Rule $rule,
private string $nodeType,
private bool $parameterValue,
private string $parameterName,
)
{
}

public function getNodeType(): string
{
return $this->nodeType;
}

public function processNode(Node $node, Scope $scope): array
{
if ($this->parameterValue) {
return [];
}

if ($this->nodeType !== $this->rule->getNodeType()) {
return [];
}

$errors = [];
foreach ($this->rule->processNode($node, $scope) as $error) {
$errors[] = RuleErrorBuilder::message($error->getMessage())
->identifier('phpstanPlayground.configParameter')
->tip(sprintf('This error would be reported if the <fg=cyan>%s: true</> parameter was enabled in your <fg=cyan>%%configurationFile%%</>.', $this->parameterName))
->build();
}

return $errors;
}

}
41 changes: 41 additions & 0 deletions tests/PHPStan/Rules/Playground/PromoteParameterRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Playground;

use PHPStan\Node\ClassPropertiesNode;
use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Rules\Properties\UninitializedPropertyRule;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<PromoteParameterRule<ClassPropertiesNode>>
*/
class PromoteParameterRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new PromoteParameterRule(
new UninitializedPropertyRule(new ConstructorsHelper(
self::getContainer(),
[],
)),
ClassPropertiesNode::class,
false,
'checkUninitializedProperties',
);
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/promote-parameter.php'], [
[
'Class PromoteParameter\Foo has an uninitialized property $test. Give it default value or assign it in the constructor.',
5,
'This error would be reported if the <fg=cyan>checkUninitializedProperties: true</> parameter was enabled in your <fg=cyan>%configurationFile%</>.',
],
]);
}

}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Playground/data/promote-parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace PromoteParameter;

class Foo
{

public int $test;

}

0 comments on commit 59ccf55

Please sign in to comment.