Skip to content

Commit b116d25

Browse files
committed
CallToConstructorStatementWithoutSideEffectsRule - report class with no constructor
1 parent f280c25 commit b116d25

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Diff for: src/Rules/Methods/CallToConstructorStatementWithoutSideEffectsRule.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ public function processNode(Node $node, Scope $scope): array
4343

4444
$classReflection = $this->reflectionProvider->getClass($className);
4545
if (!$classReflection->hasConstructor()) {
46-
return [];
46+
return [
47+
RuleErrorBuilder::message(sprintf(
48+
'Call to new %s() on a separate line has no effect.',
49+
$classReflection->getDisplayName(),
50+
))->identifier('new.resultUnused')->build(),
51+
];
4752
}
4853

4954
$constructor = $classReflection->getConstructor();

Diff for: tests/PHPStan/Rules/Methods/CallToConstructorStatementWithoutSideEffectsRuleTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public function testRule(): void
2323
'Call to Exception::__construct() on a separate line has no effect.',
2424
6,
2525
],
26+
[
27+
'Call to new PDOStatement() on a separate line has no effect.',
28+
11,
29+
],
30+
[
31+
'Call to new stdClass() on a separate line has no effect.',
32+
12,
33+
],
2634
[
2735
'Call to ConstructorStatementNoSideEffects\ConstructorWithPure::__construct() on a separate line has no effect.',
2836
57,
@@ -31,6 +39,10 @@ public function testRule(): void
3139
'Call to ConstructorStatementNoSideEffects\ConstructorWithPureAndThrowsVoid::__construct() on a separate line has no effect.',
3240
58,
3341
],
42+
[
43+
'Call to new ConstructorStatementNoSideEffects\NoConstructor() on a separate line has no effect.',
44+
68,
45+
],
3446
]);
3547
}
3648

Diff for: tests/PHPStan/Rules/Methods/data/constructor-statement-no-side-effects.php

+9
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,12 @@ function(): void {
5858
new ConstructorWithPureAndThrowsVoid();
5959
new ConstructorWithPureAndThrowsException();
6060
};
61+
62+
class NoConstructor
63+
{
64+
65+
}
66+
67+
function (): void {
68+
new NoConstructor();
69+
};

0 commit comments

Comments
 (0)