Skip to content

Commit b73e3fc

Browse files
committed
Fix allow class const fetch when on App namespace
1 parent c72d8e7 commit b73e3fc

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ public function processNode(Node $node, Scope $scope): array
106106
return [];
107107
}
108108

109+
$fileNamespace = $scope->getNamespace();
110+
111+
if (
112+
$fileNamespace !== null
113+
&& ((defined('APP_NAMESPACE') && str_starts_with($fileNamespace, APP_NAMESPACE))
114+
|| str_starts_with($fileNamespace, 'App\\'))
115+
) {
116+
return [];
117+
}
118+
109119
return [
110120
RuleErrorBuilder::message(sprintf(
111121
'Call to function %s with %s::class is discouraged.',
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
namespace App\Controllers;
15+
16+
use CodeIgniter\Shield\Models\RememberModel;
17+
18+
class HelloWorld
19+
{
20+
public function touch(): void
21+
{
22+
model(RememberModel::class)
23+
->allowCallbacks(false)
24+
->update();
25+
}
26+
}

tests/Rules/Functions/NoClassConstFetchOnFactoriesFunctionsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ public function testRule(): void
6262
],
6363
]);
6464
}
65+
66+
public function testOnAppNamespaceWithNonAppCall(): void
67+
{
68+
$this->analyse([__DIR__ . '/../../Fixtures/Rules/Functions/bug-9.php'], []);
69+
}
6570
}

0 commit comments

Comments
 (0)