Skip to content

Commit

Permalink
Assert-and-throws-are-side-effects
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-worman committed Dec 14, 2024
1 parent 0edf18d commit 5335a05
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 1 deletion.
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();
}
}
}

0 comments on commit 5335a05

Please sign in to comment.