diff --git a/docs/addons/jwt.md b/docs/addons/jwt.md index f5f962d4d..0e3f4b8a6 100644 --- a/docs/addons/jwt.md +++ b/docs/addons/jwt.md @@ -191,7 +191,7 @@ class LoginController extends BaseController // Get the credentials for login $credentials = $this->request->getJsonVar(setting('Auth.validFields')); - $credentials = array_filter($credentials); + $credentials = array_filter($credentials, static fn ($value): bool => $value !== null && $value !== ''); $credentials['password'] = $this->request->getJsonVar('password'); /** @var Session $authenticator */ diff --git a/docs/guides/mobile_apps.md b/docs/guides/mobile_apps.md index 9f1af569f..3b47235a1 100644 --- a/docs/guides/mobile_apps.md +++ b/docs/guides/mobile_apps.md @@ -47,7 +47,7 @@ class LoginController extends BaseController // Get the credentials for login $credentials = $this->request->getPost(setting('Auth.validFields')); - $credentials = array_filter($credentials); + $credentials = array_filter($credentials, static fn ($value): bool => $value !== null && $value !== ''); $credentials['password'] = $this->request->getPost('password'); // Attempt to login diff --git a/phpstan.dist.neon b/phpstan.dist.neon index 0362f5adc..c95598b17 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -25,16 +25,10 @@ parameters: - CodeIgniter\Shield\Config additionalServices: - CodeIgniter\Shield\Config\Services - strictRules: - allRules: false - disallowedLooseComparison: true - booleansInConditions: true - disallowedBacktick: true - disallowedEmpty: true - disallowedImplicitArrayCreation: true - disallowedShortTernary: true - matchingInheritedMethodNames: true ignoreErrors: - identifier: function.internal - identifier: return.internalClass - identifier: staticMethod.internal + - + message: '#Dynamic call to static method PHPUnit\\#' + identifier: staticMethod.dynamicCall diff --git a/src/Auth.php b/src/Auth.php index 03392fdbf..8a3a8d18d 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -158,7 +158,7 @@ public function routes(RouteCollection &$routes, array $config = []): void $options = isset($params[3]) ? ['as' => $params[3]] : null; - $routes->{$params[0]}($params[1], $params[2], $options); + $routes->{$params[0]}($params[1], $params[2], $options); // @phpstan-ignore method.dynamicName } } } @@ -199,7 +199,7 @@ public function __call(string $method, array $args) $authenticator = $this->authenticate->factory($this->alias); if (method_exists($authenticator, $method)) { - return $authenticator->{$method}(...$args); + return $authenticator->{$method}(...$args); // @phpstan-ignore method.dynamicName } } } diff --git a/src/Authentication/Authenticators/Session.php b/src/Authentication/Authenticators/Session.php index a9c1ba424..c3dd14b60 100644 --- a/src/Authentication/Authenticators/Session.php +++ b/src/Authentication/Authenticators/Session.php @@ -124,7 +124,7 @@ public function remember(bool $shouldRemember = true): self * Attempts to authenticate a user with the given $credentials. * Logs the user in with a successful check. * - * @phpstan-param array{email?: string, username?: string, password?: string, ...} $credentials + * @param array{email?: string, username?: string, password?: string, ...} $credentials */ public function attempt(array $credentials): Result { @@ -345,7 +345,7 @@ private function recordLoginAttempt( * Checks a user's $credentials to see if they match an * existing user. * - * @phpstan-param array{email?: string, username?: string, password?: string, ...} $credentials + * @param array{email?: string, username?: string, password?: string, ...} $credentials */ public function check(array $credentials): Result { diff --git a/src/Authentication/JWT/JWSDecoder.php b/src/Authentication/JWT/JWSDecoder.php index a5dcc7462..7204e7e84 100644 --- a/src/Authentication/JWT/JWSDecoder.php +++ b/src/Authentication/JWT/JWSDecoder.php @@ -18,12 +18,14 @@ class JWSDecoder { + private readonly JWSAdapterInterface $jwsAdapter; + /** * @var string The key group. The array key of Config\AuthJWT::$keys. */ protected $keyset = 'default'; - public function __construct(private ?JWSAdapterInterface $jwsAdapter = null) + public function __construct(?JWSAdapterInterface $jwsAdapter = null) { $this->jwsAdapter = $jwsAdapter ?? new FirebaseAdapter(); } diff --git a/src/Authentication/JWT/JWSEncoder.php b/src/Authentication/JWT/JWSEncoder.php index 699bbf706..ccc627405 100644 --- a/src/Authentication/JWT/JWSEncoder.php +++ b/src/Authentication/JWT/JWSEncoder.php @@ -19,7 +19,10 @@ class JWSEncoder { - public function __construct(protected ?JWSAdapterInterface $jwsAdapter = null, protected ?Time $clock = null) + protected JWSAdapterInterface $jwsAdapter; + protected Time $clock; + + public function __construct(?JWSAdapterInterface $jwsAdapter = null, ?Time $clock = null) { $this->jwsAdapter = $jwsAdapter ?? new FirebaseAdapter(); $this->clock = $clock ?? new Time(); @@ -54,7 +57,7 @@ public function encode( ); if (! array_key_exists('iat', $claims)) { - $payload['iat'] = $this->clock->now()->getTimestamp(); + $payload['iat'] = $this->clock::now()->getTimestamp(); } if (! array_key_exists('exp', $claims)) { diff --git a/src/Authentication/JWTManager.php b/src/Authentication/JWTManager.php index 46ca125c9..ad0806465 100644 --- a/src/Authentication/JWTManager.php +++ b/src/Authentication/JWTManager.php @@ -24,10 +24,14 @@ */ class JWTManager { + protected Time $clock; + protected JWSEncoder $jwsEncoder; + protected JWSDecoder $jwsDecoder; + public function __construct( - protected ?Time $clock = null, - protected ?JWSEncoder $jwsEncoder = null, - protected ?JWSDecoder $jwsDecoder = null, + ?Time $clock = null, + ?JWSEncoder $jwsEncoder = null, + ?JWSDecoder $jwsDecoder = null, ) { $this->clock = $clock ?? new Time(); $this->jwsEncoder = $jwsEncoder ?? new JWSEncoder(null, $this->clock); diff --git a/src/Authentication/Passwords/NothingPersonalValidator.php b/src/Authentication/Passwords/NothingPersonalValidator.php index 62fb69df2..312157ba5 100644 --- a/src/Authentication/Passwords/NothingPersonalValidator.php +++ b/src/Authentication/Passwords/NothingPersonalValidator.php @@ -15,6 +15,7 @@ use CodeIgniter\Shield\Entities\User; use CodeIgniter\Shield\Result; +use Stringable; /** * Class NothingPersonalValidator @@ -94,8 +95,20 @@ protected function isNotPersonal(string $password, ?User $user): bool $personalFields = $this->config->personalFields; foreach ($personalFields as $value) { - if (($user->{$value} ?? '') !== '') { - $needles[] = strtolower($user->{$value}); + if (! isset($user->{$value})) { + continue; + } + + $personalData = $user->{$value}; + + if (! is_string($personalData) && ! $personalData instanceof Stringable) { + continue; + } + + $personalData = (string) $personalData; + + if ($personalData !== '') { + $needles[] = mb_strtolower($personalData); } } diff --git a/src/Commands/Setup.php b/src/Commands/Setup.php index 4296d7692..11ae90481 100644 --- a/src/Commands/Setup.php +++ b/src/Commands/Setup.php @@ -365,8 +365,8 @@ private function setupEmail(): void } $config = config(EmailConfig::class); - $fromEmail = (string) $config->fromEmail; // Old Config may return null. - $fromName = (string) $config->fromName; + $fromEmail = $config->fromEmail; + $fromName = $config->fromName; if ($fromEmail !== '' && $fromName !== '') { $this->write(CLI::color(' Email Setup: ', 'green') . 'Everything is fine.'); diff --git a/src/Config/AuthJWT.php b/src/Config/AuthJWT.php index 1aa0c9c1e..e44bf6d39 100644 --- a/src/Config/AuthJWT.php +++ b/src/Config/AuthJWT.php @@ -49,8 +49,7 @@ class AuthJWT extends BaseConfig * The key of the array is the key group name. * The first key of the group is used for signing. * - * @var array>> - * @phpstan-var array>> + * @var array>> */ public array $keys = [ 'default' => [ diff --git a/src/Controllers/ActionController.php b/src/Controllers/ActionController.php index b034176c1..934125aaf 100644 --- a/src/Controllers/ActionController.php +++ b/src/Controllers/ActionController.php @@ -47,7 +47,7 @@ public function _remap(string $method, ...$params) throw new PageNotFoundException(); } - return $this->{$method}(...$params); + return $this->{$method}(...$params); // @phpstan-ignore method.dynamicName } /** diff --git a/src/Controllers/LoginController.php b/src/Controllers/LoginController.php index 4d42b74c6..7fc391ff6 100644 --- a/src/Controllers/LoginController.php +++ b/src/Controllers/LoginController.php @@ -60,7 +60,7 @@ public function loginAction(): RedirectResponse /** @var array $credentials */ $credentials = $this->request->getPost(setting('Auth.validFields')) ?? []; - $credentials = array_filter($credentials); + $credentials = array_filter($credentials, static fn ($value): bool => $value !== null && $value !== ''); $credentials['password'] = $this->request->getPost('password'); $remember = (bool) $this->request->getPost('remember'); diff --git a/src/Entities/User.php b/src/Entities/User.php index d86e15d91..25986885d 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -135,7 +135,7 @@ public function setIdentities(?array $identities): void * Creates a new identity for this user with an email/password * combination. * - * @phpstan-param array{email: string, password: string} $credentials + * @param array{email: string, password: string} $credentials */ public function createEmailIdentity(array $credentials): void { diff --git a/src/Models/UserIdentityModel.php b/src/Models/UserIdentityModel.php index 7eccf2f8c..92f2e8b98 100644 --- a/src/Models/UserIdentityModel.php +++ b/src/Models/UserIdentityModel.php @@ -75,7 +75,7 @@ public function create($data): void * Creates a new identity for this user with an email/password * combination. * - * @phpstan-param array{email: string, password: string} $credentials + * @param array{email: string, password: string} $credentials */ public function createEmailIdentity(User $user, array $credentials): void { diff --git a/src/Models/UserModel.php b/src/Models/UserModel.php index 7acb01258..ea50c9aa7 100644 --- a/src/Models/UserModel.php +++ b/src/Models/UserModel.php @@ -151,8 +151,7 @@ protected function fetchIdentities(array $data): array * @param array $data Event $data * @param list $identities * - * @return list UserId => User object - * @phpstan-return array UserId => User object + * @return array UserId => User object */ private function assignIdentities(array $data, array $identities): array { @@ -280,7 +279,7 @@ private function assignProperties(array $data, array $properties, string $type): // Assign properties to all users (empty array if no properties found) foreach ($mappedUsers as $userId => $user) { $propertyArray = $properties[$userId] ?? []; - $user->{$method}($propertyArray); + $user->{$method}($propertyArray); // @phpstan-ignore method.dynamicName } unset($properties); diff --git a/src/Result.php b/src/Result.php index 45f75cc5f..5f7a9b939 100644 --- a/src/Result.php +++ b/src/Result.php @@ -34,8 +34,7 @@ class Result protected $extraInfo; /** - * @phpstan-param array{success: bool, reason?: string|null, extraInfo?: string|User} $details - * @psalm-param array{success: bool, reason?: string|null, extraInfo?: string|User} $details + * @param array{success: bool, reason?: string|null, extraInfo?: string|User} $details */ public function __construct(array $details) { diff --git a/tests/Authentication/AuthHelperTest.php b/tests/Authentication/AuthHelperTest.php index b739158fd..0af44a990 100644 --- a/tests/Authentication/AuthHelperTest.php +++ b/tests/Authentication/AuthHelperTest.php @@ -32,7 +32,7 @@ protected function setUp(): void { parent::setUp(); - $this->setPrivateProperty(auth(), 'alias', null); + self::setPrivateProperty(auth(), 'alias', null); } public function testAuthReturnsDefaultAuthenticator(): void @@ -66,7 +66,7 @@ public function testUserIdReturnsNull(): void public function testUserIdReturnsId(): void { $user = fake(UserModel::class, ['id' => 1]); - $this->setPrivateProperty(auth()->getAuthenticator(), 'user', $user); + self::setPrivateProperty(auth()->getAuthenticator(), 'user', $user); $_SESSION['user']['id'] = $user->id; $this->assertTrue(auth()->loggedIn()); diff --git a/tests/Authentication/Authenticators/JWTAuthenticatorTest.php b/tests/Authentication/Authenticators/JWTAuthenticatorTest.php index 2fd6ba685..24399b43f 100644 --- a/tests/Authentication/Authenticators/JWTAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/JWTAuthenticatorTest.php @@ -274,7 +274,7 @@ private function generateJWT(?Time $clock = null): string { $this->user = \fake(UserModel::class, ['id' => 1, 'username' => 'John Smith']); - $generator = new JWTManager($clock); + $generator = $clock instanceof Time ? new JWTManager($clock) : new JWTManager(); return $generator->generateToken($this->user); } diff --git a/tests/Authentication/MagicLinkTest.php b/tests/Authentication/MagicLinkTest.php index 272e276e6..a9d7468ad 100644 --- a/tests/Authentication/MagicLinkTest.php +++ b/tests/Authentication/MagicLinkTest.php @@ -75,7 +75,7 @@ public function testMagicLinkSubmitBadEmail(): void public function testMagicLinkSubmitSuccess(): void { - /** @phpstan-var User $user */ + /** @var User $user */ $user = fake(UserModel::class); $user->createEmailIdentity(['email' => 'foo@example.com', 'password' => 'secret123']); @@ -104,7 +104,7 @@ public function testMagicLinkVerifyExpired(): void { $identities = new UserIdentityModel(); - /** @phpstan-var User $user */ + /** @var User $user */ $user = fake(UserModel::class); $user->createEmailIdentity(['email' => 'foo@example.com', 'password' => 'secret123']); $identities->insert([ diff --git a/tests/Commands/SetupTest.php b/tests/Commands/SetupTest.php index 377bfde42..055a27e90 100644 --- a/tests/Commands/SetupTest.php +++ b/tests/Commands/SetupTest.php @@ -61,8 +61,7 @@ private function removeDirectory(string $directory): void /** * Set MockInputOutput and user inputs. * - * @param array $inputs User inputs - * @phpstan-param list $inputs + * @param list $inputs User inputs */ private function setMockIo(array $inputs): void { @@ -86,7 +85,7 @@ public function testRun(): void $command = new Setup(Services::logger(), Services::commands()); - $this->setPrivateProperty($command, 'distPath', $appFolder); + self::setPrivateProperty($command, 'distPath', $appFolder); $command->run([]); @@ -142,7 +141,7 @@ public function testRunEmailConfigIsFine(): void $command = new Setup(Services::logger(), Services::commands()); - $this->setPrivateProperty($command, 'distPath', $appFolder); + self::setPrivateProperty($command, 'distPath', $appFolder); $command->run([]); diff --git a/tests/Commands/UserTest.php b/tests/Commands/UserTest.php index bfae2210a..8ba552bd2 100644 --- a/tests/Commands/UserTest.php +++ b/tests/Commands/UserTest.php @@ -37,8 +37,7 @@ protected function tearDown(): void /** * Set MockInputOutput and user inputs. * - * @param array $inputs User inputs - * @phpstan-param list $inputs + * @param list $inputs User inputs */ private function setMockIo(array $inputs): void { diff --git a/tests/Unit/Authentication/JWT/Adapters/FirebaseAdapaterTest.php b/tests/Unit/Authentication/JWT/Adapters/FirebaseAdapaterTest.php index 18c987f2c..cc1032e5c 100644 --- a/tests/Unit/Authentication/JWT/Adapters/FirebaseAdapaterTest.php +++ b/tests/Unit/Authentication/JWT/Adapters/FirebaseAdapaterTest.php @@ -31,7 +31,7 @@ final class FirebaseAdapaterTest extends TestCase { public function testDecode(): void { - $token = $this->generateJWT(); + $token = self::generateJWT(); $adapter = new FirebaseAdapter(); @@ -52,7 +52,7 @@ public static function generateJWT(?Time $clock = null): string { $user = fake(UserModel::class, ['id' => 1, 'username' => 'John Smith'], false); - $generator = new JWTManager($clock); + $generator = $clock instanceof Time ? new JWTManager($clock) : new JWTManager(); return $generator->generateToken($user); } @@ -77,7 +77,7 @@ public function testDecodeExpiredToken(): void $adapter = new FirebaseAdapter(); Time::setTestNow('-1 hour'); - $token = $this->generateJWT(); + $token = self::generateJWT(); Time::setTestNow(); $key = 'default'; @@ -86,7 +86,7 @@ public function testDecodeExpiredToken(): void public function testDecodeInvalidTokenExceptionUnexpectedValueException(): void { - $token = $this->generateJWT(); + $token = self::generateJWT(); // Change algorithm and it makes the key invalid. /** @var AuthJWT $config */ @@ -115,7 +115,7 @@ public function testDecodeInvalidArgumentException(): void $this->expectException(ShieldInvalidArgumentException::class); $this->expectExceptionMessage('Invalid Keyset: "default". Key material must not be empty'); - $token = $this->generateJWT(); + $token = self::generateJWT(); // Set invalid key. /** @var AuthJWT $config */ diff --git a/tests/Unit/Authentication/JWT/JWTManagerTest.php b/tests/Unit/Authentication/JWT/JWTManagerTest.php index f0f7f32af..896c6e602 100644 --- a/tests/Unit/Authentication/JWT/JWTManagerTest.php +++ b/tests/Unit/Authentication/JWT/JWTManagerTest.php @@ -27,7 +27,7 @@ final class JWTManagerTest extends TestCase { private function createJWTManager(?Time $clock = null): JWTManager { - return new JWTManager($clock); + return $clock instanceof Time ? new JWTManager($clock) : new JWTManager(); } public function testGenerateToken(): array @@ -40,7 +40,7 @@ public function testGenerateToken(): array $clock = new Time(); $manager = $this->createJWTManager($clock); - $currentTime = $clock->now(); + $currentTime = $clock::now(); $token = $manager->generateToken($user); @@ -96,7 +96,7 @@ public function testIssue(): array $clock = new Time(); $manager = $this->createJWTManager($clock); - $currentTime = $clock->now(); + $currentTime = $clock::now(); $payload = [ 'user_id' => '1',