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
14 changes: 1 addition & 13 deletions src/Type/Pest/HigherOrderExpectationTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,6 @@ private function isKnownExpectationMethod(string $methodName): bool

$classReflection = $this->reflectionProvider->getClass(Expectation::class);

if ($classReflection->hasNativeMethod($methodName)) {
return true;
}

foreach ($classReflection->getResolvedMixinTypes() as $mixinType) {
foreach ($mixinType->getObjectClassReflections() as $mixinClassReflection) {
if ($mixinClassReflection->hasNativeMethod($methodName)) {
return true;
}
}
}

return false;
return $classReflection->hasMethod($methodName);
}
}
21 changes: 21 additions & 0 deletions tests/Rules/CustomExpectationMethodCallTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tests\Rules;

use PHPStan\Rules\Methods\CallMethodsRule;
use Tests\RuleTestCase;

beforeAll(function (): void {
RuleTestCase::$additionalConfigFiles = [
__DIR__.'/custom-expectations.neon',
];
RuleTestCase::$rule = RuleTestCase::resolveRule(CallMethodsRule::class);
});

test('custom expectations from a methods class reflection extension are not higher order method calls', function (): void {
$this->analyse([
__DIR__.'/data/custom-expectation-method-call.php',
], []);
});
15 changes: 15 additions & 0 deletions tests/Rules/Fixtures/CustomExpectations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tests\Rules\Fixtures;

use Pest\Expectation;

interface CustomExpectations
{
/**
* @return Expectation<mixed>
*/
public function toBeCustom(): Expectation;
}
32 changes: 32 additions & 0 deletions tests/Rules/Fixtures/CustomExpectationsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Tests\Rules\Fixtures;

use Pest\Expectation;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\MethodsClassReflectionExtension;
use PHPStan\Reflection\ReflectionProvider;

final readonly class CustomExpectationsExtension implements MethodsClassReflectionExtension
{
public function __construct(private ReflectionProvider $reflectionProvider) {}

public function hasMethod(ClassReflection $classReflection, string $methodName): bool
{
return $classReflection->getName() === Expectation::class
&& $this->customExpectations()->hasNativeMethod($methodName);
}

public function getMethod(ClassReflection $classReflection, string $methodName): MethodReflection
{
return $this->customExpectations()->getNativeMethod($methodName);
}

private function customExpectations(): ClassReflection
{
return $this->reflectionProvider->getClass(CustomExpectations::class);
}
}
8 changes: 8 additions & 0 deletions tests/Rules/custom-expectations.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
includes:
- ../extension.neon

services:
-
class: Tests\Rules\Fixtures\CustomExpectationsExtension
tags:
- phpstan.broker.methodsClassReflectionExtension
13 changes: 13 additions & 0 deletions tests/Rules/data/custom-expectation-method-call.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Tests\Type\Fixtures\Post;

it('calls a custom expectation provided by a methods class reflection extension', function (): void {
expect(Post::class)->toBeCustom();

expect(new Post)->toBeCustom();

expect('Hello')->toBeCustom();
});
Loading