Skip to content

Commit 542275a

Browse files
committed
Skip NoClassConstFetchOnFactoriesFunction rule inside test classes
1 parent c0da2f4 commit 542275a

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\Reflection\ReflectionProvider;
2020
use PHPStan\Rules\Rule;
2121
use PHPStan\Rules\RuleErrorBuilder;
22+
use PHPUnit\Framework\TestCase;
2223

2324
/**
2425
* @implements Rule<Node\Expr\FuncCall>
@@ -71,6 +72,14 @@ public function processNode(Node $node, Scope $scope): array
7172
return [];
7273
}
7374

75+
if ($scope->isInClass()) {
76+
$classRef = $scope->getClassReflection();
77+
78+
if ($this->reflectionProvider->hasClass(TestCase::class) && $classRef->isSubclassOf(TestCase::class)) {
79+
return []; // skip uses in test classes as tests are internal
80+
}
81+
}
82+
7483
$returnType = $this->factoriesReturnTypeHelper->check($scope->getType($classConstFetch), $function);
7584

7685
if ($returnType->isNull()->yes()) {
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) 2023 CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Config\Cache;
15+
use PHPUnit\Framework\TestCase;
16+
17+
use function PHPStan\Testing\assertType;
18+
19+
assertType('Config\Cache', config(Cache::class));
20+
21+
/**
22+
* @internal
23+
*/
24+
final class ConfigTest extends TestCase
25+
{
26+
public function testConfig(): void
27+
{
28+
self::assertContains('file', config(Cache::class)->validHandlers);
29+
}
30+
}

tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ public function testRule(): void
4242
{
4343
$this->analyse([
4444
__DIR__ . '/../../Fixtures/Type/config.php',
45+
__DIR__ . '/../../Fixtures/Type/factories-in-tests.php',
4546
__DIR__ . '/../../Fixtures/Type/model.php',
4647
], [
4748
[
4849
'Call to function config with Config\App::class is discouraged.',
4950
26,
5051
'Use config(\'App\') instead to allow overriding.',
5152
],
53+
[
54+
'Call to function config with Config\Cache::class is discouraged.',
55+
19,
56+
'Use config(\'Cache\') instead to allow overriding.', ],
5257
[
5358
'Call to function model with stdClass::class is discouraged.',
5459
19,

0 commit comments

Comments
 (0)