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
6 changes: 6 additions & 0 deletions src/Type/Pest/HigherOrderExpectationTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\ExpressionTypeResolverExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
Expand All @@ -35,6 +36,11 @@ public function __construct(

public function getType(Expr $expr, Scope $scope): ?Type
{
if (($expr instanceof PropertyFetch || $expr instanceof MethodCall)
&& (new NeverType)->isSuperTypeOf($scope->getType($expr->var))->yes()) {
return null;
}

if ($expr instanceof PropertyFetch) {
return $this->resolvePropertyFetch($expr, $scope);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Type/ExpectTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
yield from TestCase::gatherAssertTypes(__DIR__.'/data/higher-order-exhaustive.php');
});

test('never receiver is not an expectation', function (string $assertType, string $file, mixed ...$args): void {
$this->assertFileAsserts($assertType, $file, ...$args);
})->with(function (): Iterator {
yield from TestCase::gatherAssertTypes(__DIR__.'/data/never-receiver.php');
});

test('exhaustive test call types', function (string $assertType, string $file, mixed ...$args): void {
$this->assertFileAsserts($assertType, $file, ...$args);
})->with(function (): Iterator {
Expand Down
31 changes: 31 additions & 0 deletions tests/Type/data/never-receiver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace NeverReceiver;

use Tests\Type\Fixtures\Post;

use function PHPStan\Testing\assertType;

/**
* @param never $value
*/
function testPropertyFetchOnNever(mixed $value): void
{
assertType('*ERROR*', $value->title);
}

/**
* @param never $value
*/
function testMethodCallOnNever(mixed $value): void
{
assertType('*ERROR*', $value->getTitle());
}

function testRealExpectationIsUnaffected(): void
{
$post = new Post;
assertType('Pest\Expectations\HigherOrderExpectation<Pest\Expectation<Tests\Type\Fixtures\Post>, string>', expect($post)->title);
}