Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect asserts and throws on pure functions that return void #3719

Open
wants to merge 3 commits into
base: 2.1.x
Choose a base branch
from
Open
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 @@ -66,7 +66,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
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Pure/PureFunctionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,11 @@ 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 @@ -194,4 +194,12 @@ public function dataBug11207(): array
];
}

public function testBug12224(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-12224.php'], [
['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