diff --git a/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php b/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php index db6bdb4..6a987fb 100644 --- a/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php +++ b/src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php @@ -106,6 +106,16 @@ public function processNode(Node $node, Scope $scope): array return []; } + $fileNamespace = $scope->getNamespace(); + + if ( + $fileNamespace !== null + && ((defined('APP_NAMESPACE') && str_starts_with($fileNamespace, APP_NAMESPACE)) + || str_starts_with($fileNamespace, 'App\\')) + ) { + return []; + } + return [ RuleErrorBuilder::message(sprintf( 'Call to function %s with %s::class is discouraged.', diff --git a/tests/Fixtures/Rules/Functions/bug-9.php b/tests/Fixtures/Rules/Functions/bug-9.php new file mode 100644 index 0000000..5bed94e --- /dev/null +++ b/tests/Fixtures/Rules/Functions/bug-9.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace App\Controllers; + +use CodeIgniter\Shield\Models\RememberModel; + +class HelloWorld +{ + public function touch(): void + { + model(RememberModel::class) + ->allowCallbacks(false) + ->update(); + } +} diff --git a/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php b/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php index 6bca8cd..cc5cbb6 100644 --- a/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php +++ b/tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php @@ -62,4 +62,9 @@ public function testRule(): void ], ]); } + + public function testOnAppNamespaceWithNonAppCall(): void + { + $this->analyse([__DIR__ . '/../../Fixtures/Rules/Functions/bug-9.php'], []); + } }