Skip to content

Consider assert as side effect #4227

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

Open
wants to merge 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use function count;
use function in_array;
use function sprintf;

Expand Down Expand Up @@ -65,6 +66,10 @@ public function processNode(Node $node, Scope $scope): array
}

$function = $this->reflectionProvider->getFunction($funcCall->name, $scope);
if (count($function->getAsserts()->getAsserts()) > 0) {
return [];
}

$functionName = $function->getName();
$functionHasSideEffects = !$function->hasSideEffects()->no();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Type\ErrorType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use function count;
use function sprintf;

/**
Expand Down Expand Up @@ -69,6 +70,9 @@ public function processNode(Node $node, Scope $scope): array
}

$method = $calledOnType->getMethod($methodName, $scope);
if (count($method->getAsserts()->getAsserts()) > 0) {
return [];
}

return [
RuleErrorBuilder::message(sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use function count;
use function sprintf;
use function strtolower;

Expand Down Expand Up @@ -87,6 +88,10 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (count($method->getAsserts()->getAsserts()) > 0) {
return [];
}

$methodResult = $scope->getType($staticCall);
if ($methodResult instanceof NeverType && $methodResult->isExplicit()) {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public function testPhpDoc(): void
]);
}

public function testBug12224(): void
{
$this->analyse([__DIR__ . '/data/bug-12224.php'], []);
}

public function testBug4455(): void
{
require_once __DIR__ . '/data/bug-4455.php';
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-12224.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Bug12224Function;

/**
* @phpstan-pure
* @phpstan-assert string $value
*/
function string(mixed $value): string
{
if (\is_string($value)) {
return $value;
}
throw new \RuntimeException();
}

/** @var string|null $a */
$a = '';
string($a);
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public function testBug11503(): void
$this->analyse([__DIR__ . '/data/bug-11503.php'], $errors);
}

public function testBug12224(): void
{
$this->analyse([__DIR__ . '/data/bug-12224.php'], []);
}

public function testFirstClassCallables(): void
{
$this->analyse([__DIR__ . '/data/first-class-callable-method-without-side-effect.php'], [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public function testBug4455(): void
$this->analyse([__DIR__ . '/data/bug-4455-static.php'], []);
}

public function testBug12224(): void
{
$this->analyse([__DIR__ . '/data/bug-12224.php'], []);
}

public function testFirstClassCallables(): void
{
$this->analyse([__DIR__ . '/data/first-class-callable-static-method-without-side-effect.php'], [
Expand Down
35 changes: 35 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-12224.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace Bug12224Method;

class Assert
{
/**
* @phpstan-pure
* @phpstan-assert string $value
*/
public static function staticString(mixed $value): string
{
if (\is_string($value)) {
return $value;
}
throw new \RuntimeException();
}

/**
* @phpstan-pure
* @phpstan-assert string $value
*/
public function string(mixed $value): string
{
if (\is_string($value)) {
return $value;
}
throw new \RuntimeException();
}
}

/** @var string|null $a */
$a = '';
Assert::staticString($a);
(new Assert())->string($a);
Loading