Skip to content

Commit

Permalink
Make sure AssertFunctionTypeSpecifyingExtension works with namespaced…
Browse files Browse the repository at this point in the history
… functions
  • Loading branch information
ondrejmirtes committed Jun 17, 2021
1 parent 151d71b commit 8e11b1a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Type/PHPUnit/Assert/AssertFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function isFunctionSupported(
): bool
{
return AssertTypeSpecifyingExtensionHelper::isSupported(
$functionReflection->getName(),
$this->trimName($functionReflection->getName()),
$node->args
);
}
Expand All @@ -44,9 +44,19 @@ public function specifyTypes(
return AssertTypeSpecifyingExtensionHelper::specifyTypes(
$this->typeSpecifier,
$scope,
$functionReflection->getName(),
$this->trimName($functionReflection->getName()),
$node->args
);
}

private function trimName(string $functionName): string
{
$prefix = 'PHPUnit\\Framework\\';
if (strpos($functionName, $prefix) === 0) {
return substr($functionName, strlen($prefix));
}

return $functionName;
}

}
36 changes: 36 additions & 0 deletions tests/Type/PHPUnit/AssertFunctionTypeSpecifyingExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\PHPUnit;

use PHPStan\Testing\TypeInferenceTestCase;

class AssertFunctionTypeSpecifyingExtensionTest extends TypeInferenceTestCase
{

/** @return mixed[] */
public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/assert-function.php');
}

/**
* @dataProvider dataFileAsserts
* @param string $assertType
* @param string $file
* @param mixed ...$args
*/
public function testFileAsserts(
string $assertType,
string $file,
...$args
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [__DIR__ . '/../../../extension.neon'];
}

}
20 changes: 20 additions & 0 deletions tests/Type/PHPUnit/data/assert-function.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace AssertFunction;

use function PHPStan\Testing\assertType;
use function PHPUnit\Framework\assertInstanceOf;

class Foo
{

/**
* @param object $o
*/
public function doFoo($o): void
{
assertInstanceOf(self::class, $o);
assertType(self::class, $o);
}

}

0 comments on commit 8e11b1a

Please sign in to comment.