Skip to content
Merged
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
8 changes: 7 additions & 1 deletion src/Rules/Pure/FunctionPurityCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ public function check(
))->identifier(sprintf('pure%s.parameterByRef', $identifier))->build();
}

if ($returnType->isVoid()->yes() && !$isConstructor) {
$throwType = $functionReflection->getThrowType();
if (
$returnType->isVoid()->yes()
&& !$isConstructor
&& ($throwType === null || $throwType->isVoid()->yes())
&& $functionReflection->getAsserts()->getAll() === []
) {
$errors[] = RuleErrorBuilder::message(sprintf(
'%s is marked as pure but returns void.',
$functionDescription,
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,14 @@ public function testBug11361(): void
]);
}

public function testBug12224(): void
{
$this->analyse([__DIR__ . '/data/bug-12224.php'], [
[
'Function PHPStan\Rules\Pure\data\pureWithThrowsVoid() is marked as pure but returns void.',
18,
],
]);
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Pure/PureMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,12 @@ public function testBug12048(): void
$this->analyse([__DIR__ . '/data/bug-12048.php'], []);
}

public function testBug12224(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-12224.php'], [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get it, why did you delete this test? You should only add a new test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why i did that. Reverted the delete

['Method PHPStan\Rules\Pure\data\A::pureWithThrowsVoid() is marked as pure but returns void.', 47],
]);
}

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

namespace PHPStan\Rules\Pure\data;

/**
* @phpstan-pure
* @throws \Exception
*/
function pureWithThrows(): void
{
throw new \Exception();
}

/**
* @phpstan-pure
* @throws void
*/
function pureWithThrowsVoid(): void
{
}

/**
* @phpstan-pure
* @phpstan-assert int $a
*/
function pureWithAssert(mixed $a): void
{
if (!is_int($a)) {
throw new \Exception();
}
}

class A {
/**
* @phpstan-pure
* @throws \Exception
*/
public function pureWithThrows(): void
{
throw new \Exception();
}

/**
* @phpstan-pure
* @throws void
*/
public function pureWithThrowsVoid(): void
{
}

/**
* @phpstan-pure
* @phpstan-assert int $a
*/
public function pureWithAssert(mixed $a): void
{
if (!is_int($a)) {
throw new \Exception();
}
}
}

class B {
/**
* @phpstan-pure
* @throws \Exception
*/
public static function pureWithThrows(): void
{
throw new \Exception();
}

/**
* @phpstan-pure
* @throws \Exception
*/
public static function pureWithThrowsVoid(): void
{
}

/**
* @phpstan-pure
* @phpstan-assert int $a
*/
public static function pureWithAssert(mixed $a): void
{
if (!is_int($a)) {
throw new \Exception();
}
}
}
Loading