Skip to content

Commit

Permalink
CallToConstructorStatementWithoutSideEffectsRule - report class with …
Browse files Browse the repository at this point in the history
…no constructor
  • Loading branch information
ondrejmirtes committed Apr 16, 2024
1 parent f280c25 commit b116d25
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public function processNode(Node $node, Scope $scope): array

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

$constructor = $classReflection->getConstructor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public function testRule(): void
'Call to Exception::__construct() on a separate line has no effect.',
6,
],
[
'Call to new PDOStatement() on a separate line has no effect.',
11,
],
[
'Call to new stdClass() on a separate line has no effect.',
12,
],
[
'Call to ConstructorStatementNoSideEffects\ConstructorWithPure::__construct() on a separate line has no effect.',
57,
Expand All @@ -31,6 +39,10 @@ public function testRule(): void
'Call to ConstructorStatementNoSideEffects\ConstructorWithPureAndThrowsVoid::__construct() on a separate line has no effect.',
58,
],
[
'Call to new ConstructorStatementNoSideEffects\NoConstructor() on a separate line has no effect.',
68,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ function(): void {
new ConstructorWithPureAndThrowsVoid();
new ConstructorWithPureAndThrowsException();
};

class NoConstructor
{

}

function (): void {
new NoConstructor();
};

0 comments on commit b116d25

Please sign in to comment.