Skip to content

Commit 8e11b1a

Browse files
committed
Make sure AssertFunctionTypeSpecifyingExtension works with namespaced functions
1 parent 151d71b commit 8e11b1a

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

src/Type/PHPUnit/Assert/AssertFunctionTypeSpecifyingExtension.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function isFunctionSupported(
2929
): bool
3030
{
3131
return AssertTypeSpecifyingExtensionHelper::isSupported(
32-
$functionReflection->getName(),
32+
$this->trimName($functionReflection->getName()),
3333
$node->args
3434
);
3535
}
@@ -44,9 +44,19 @@ public function specifyTypes(
4444
return AssertTypeSpecifyingExtensionHelper::specifyTypes(
4545
$this->typeSpecifier,
4646
$scope,
47-
$functionReflection->getName(),
47+
$this->trimName($functionReflection->getName()),
4848
$node->args
4949
);
5050
}
5151

52+
private function trimName(string $functionName): string
53+
{
54+
$prefix = 'PHPUnit\\Framework\\';
55+
if (strpos($functionName, $prefix) === 0) {
56+
return substr($functionName, strlen($prefix));
57+
}
58+
59+
return $functionName;
60+
}
61+
5262
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\PHPUnit;
4+
5+
use PHPStan\Testing\TypeInferenceTestCase;
6+
7+
class AssertFunctionTypeSpecifyingExtensionTest extends TypeInferenceTestCase
8+
{
9+
10+
/** @return mixed[] */
11+
public function dataFileAsserts(): iterable
12+
{
13+
yield from $this->gatherAssertTypes(__DIR__ . '/data/assert-function.php');
14+
}
15+
16+
/**
17+
* @dataProvider dataFileAsserts
18+
* @param string $assertType
19+
* @param string $file
20+
* @param mixed ...$args
21+
*/
22+
public function testFileAsserts(
23+
string $assertType,
24+
string $file,
25+
...$args
26+
): void
27+
{
28+
$this->assertFileAsserts($assertType, $file, ...$args);
29+
}
30+
31+
public static function getAdditionalConfigFiles(): array
32+
{
33+
return [__DIR__ . '/../../../extension.neon'];
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace AssertFunction;
4+
5+
use function PHPStan\Testing\assertType;
6+
use function PHPUnit\Framework\assertInstanceOf;
7+
8+
class Foo
9+
{
10+
11+
/**
12+
* @param object $o
13+
*/
14+
public function doFoo($o): void
15+
{
16+
assertInstanceOf(self::class, $o);
17+
assertType(self::class, $o);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)