diff --git a/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php b/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php index f9d8e79..3596732 100644 --- a/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php +++ b/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php @@ -19,6 +19,7 @@ use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; +use PHPUnit\Framework\TestCase; /** * @implements Rule @@ -71,6 +72,14 @@ public function processNode(Node $node, Scope $scope): array return []; } + if ($scope->isInClass()) { + $classRef = $scope->getClassReflection(); + + if ($this->reflectionProvider->hasClass(TestCase::class) && $classRef->isSubclassOf(TestCase::class)) { + return []; // skip uses in test classes as tests are internal + } + } + $returnType = $this->factoriesReturnTypeHelper->check($scope->getType($classConstFetch), $function); if ($returnType->isNull()->yes()) { diff --git a/tests/Fixtures/Type/factories-in-tests.php b/tests/Fixtures/Type/factories-in-tests.php new file mode 100644 index 0000000..d0af059 --- /dev/null +++ b/tests/Fixtures/Type/factories-in-tests.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +use Config\Cache; +use PHPUnit\Framework\TestCase; + +use function PHPStan\Testing\assertType; + +assertType('Config\Cache', config(Cache::class)); + +/** + * @internal + */ +final class ConfigTest extends TestCase +{ + public function testConfig(): void + { + self::assertContains('file', config(Cache::class)->validHandlers); + } +} diff --git a/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php b/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php index 0cdf799..cceec21 100644 --- a/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php +++ b/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php @@ -42,6 +42,7 @@ public function testRule(): void { $this->analyse([ __DIR__ . '/../../Fixtures/Type/config.php', + __DIR__ . '/../../Fixtures/Type/factories-in-tests.php', __DIR__ . '/../../Fixtures/Type/model.php', ], [ [ @@ -49,6 +50,10 @@ public function testRule(): void 26, 'Use config(\'App\') instead to allow overriding.', ], + [ + 'Call to function config with Config\Cache::class is discouraged.', + 19, + 'Use config(\'Cache\') instead to allow overriding.', ], [ 'Call to function model with stdClass::class is discouraged.', 19,