Skip to content

Commit

Permalink
Fix allow class const fetch when on App namespace (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan authored Nov 13, 2023
1 parent 97bedd0 commit 4b44060
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Rules/Functions/NoClassConstFetchOnFactoriesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
26 changes: 26 additions & 0 deletions tests/Fixtures/Rules/Functions/bug-9.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) 2023 CodeIgniter Foundation <admin@codeigniter.com>
*
* 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public function testRule(): void
],
]);
}

public function testOnAppNamespaceWithNonAppCall(): void
{
$this->analyse([__DIR__ . '/../../Fixtures/Rules/Functions/bug-9.php'], []);
}
}

0 comments on commit 4b44060

Please sign in to comment.