Skip to content

Commit

Permalink
Only in bleeding edge
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 16, 2024
1 parent 4293f99 commit 6026869
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
8 changes: 7 additions & 1 deletion conf/config.level4.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ rules:
- PHPStan\Rules\DeadCode\UnusedPrivateMethodRule
- PHPStan\Rules\Exceptions\OverwrittenExitPointByFinallyRule
- PHPStan\Rules\Functions\CallToFunctionStatementWithoutSideEffectsRule
- PHPStan\Rules\Methods\CallToConstructorStatementWithoutSideEffectsRule
- PHPStan\Rules\Methods\CallToMethodStatementWithoutSideEffectsRule
- PHPStan\Rules\Methods\CallToStaticMethodStatementWithoutSideEffectsRule
- PHPStan\Rules\Methods\NullsafeMethodCallRule
Expand Down Expand Up @@ -217,6 +216,13 @@ services:
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\Methods\CallToConstructorStatementWithoutSideEffectsRule
arguments:
reportNoConstructor: %featureToggles.pure%
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\TooWideTypehints\TooWideMethodReturnTypehintRule
arguments:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
class CallToConstructorStatementWithoutSideEffectsRule implements Rule
{

public function __construct(private ReflectionProvider $reflectionProvider)
public function __construct(
private ReflectionProvider $reflectionProvider,
private bool $reportNoConstructor,
)
{
}

Expand All @@ -43,12 +46,16 @@ public function processNode(Node $node, Scope $scope): array

$classReflection = $this->reflectionProvider->getClass($className);
if (!$classReflection->hasConstructor()) {
return [
RuleErrorBuilder::message(sprintf(
'Call to new %s() on a separate line has no effect.',
$classReflection->getDisplayName(),
))->identifier('new.resultUnused')->build(),
];
if ($this->reportNoConstructor) {
return [
RuleErrorBuilder::message(sprintf(
'Call to new %s() on a separate line has no effect.',
$classReflection->getDisplayName(),
))->identifier('new.resultUnused')->build(),
];
}

return [];
}

$constructor = $classReflection->getConstructor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CallToConstructorStatementWithoutSideEffectsRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new CallToConstructorStatementWithoutSideEffectsRule($this->createReflectionProvider());
return new CallToConstructorStatementWithoutSideEffectsRule($this->createReflectionProvider(), true);
}

public function testRule(): void
Expand Down

0 comments on commit 6026869

Please sign in to comment.