Skip to content

Commit

Permalink
Skip NoClassConstFetchOnFactoriesFunction rule inside test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Oct 12, 2023
1 parent c0da2f4 commit 542275a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPUnit\Framework\TestCase;

/**
* @implements Rule<Node\Expr\FuncCall>
Expand Down Expand Up @@ -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()) {
Expand Down
30 changes: 30 additions & 0 deletions tests/Fixtures/Type/factories-in-tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) 2023 CodeIgniter Foundation <[email protected]>
*
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ public function testRule(): void
{
$this->analyse([
__DIR__ . '/../../Fixtures/Type/config.php',
__DIR__ . '/../../Fixtures/Type/factories-in-tests.php',
__DIR__ . '/../../Fixtures/Type/model.php',
], [
[
'Call to function config with Config\App::class is discouraged.',
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,
Expand Down

0 comments on commit 542275a

Please sign in to comment.