Skip to content

Commit 00be259

Browse files
committed
Add phpstan
1 parent e8cdfc9 commit 00be259

6 files changed

Lines changed: 40 additions & 20 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,6 @@ jobs:
150150
# if: success() || failure()
151151
# run: vendor/bin/psalm.phar --output-format=github
152152
#
153-
# - name: Run phpstan
154-
# if: success() || failure()
155-
# run: vendor/bin/phpstan.phar analyse --error-format=github
153+
- name: Run phpstan
154+
if: success() || failure()
155+
run: vendor/bin/phpstan.phar analyse --error-format=github

composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"cakephp/cakephp": "^5.0",
2222
"cakephp/cakephp-codesniffer": "^5.1",
2323
"cakephp/migrations": "^4.0",
24+
"phpstan/phpstan": "^1.12",
2425
"phpunit/phpunit": "^10.5.5 || ^11.1.3 || ^12.0.9"
2526
},
2627
"autoload": {
@@ -36,8 +37,15 @@
3637
}
3738
},
3839
"scripts": {
40+
"check": [
41+
"@cs-check",
42+
"@stan",
43+
"@test"
44+
],
3945
"cs-check": "phpcs -p --extensions=php ./src ./tests",
4046
"cs-fix": "phpcbf -p --extensions=php ./src ./tests",
47+
"stan": "phpstan analyse",
48+
"test": "phpunit --colors=always",
4149
"update-lowest": "composer update --prefer-lowest --prefer-stable"
4250
},
4351
"config": {

phpstan.neon.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
parameters:
2+
level: 8
3+
checkMissingIterableValueType: false
4+
treatPhpDocTypesAsCertain: false
5+
paths:
6+
- src/
7+
excludePaths:
8+
- test_app/
9+
ignoreErrors:
10+
-
11+
identifier: missingType.generics

src/Authenticator/CookieAuthenticator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,13 @@ protected function _saveToken(ArrayAccess|array $identity, string $token): Entit
173173
throw new InvalidArgumentException('Can\'t detect user model');
174174
}
175175

176-
$userTable = $this->fetchTable($userModel);
177-
/** @var \RememberMe\Model\Table\RememberMeTokensTableInterface $tokenTable */
176+
$usersTable = $this->fetchTable($userModel);
177+
$primaryKey = $usersTable->getPrimaryKey();
178+
if (!is_string($primaryKey)) {
179+
throw new InvalidArgumentException('User model must have a single primary key.');
180+
}
181+
182+
/** @var \RememberMe\Model\Table\RememberMeTokensTableInterface&\Cake\ORM\Table $tokenTable */
178183
$tokenTable = $this->fetchTable($this->getConfig('tokenStorageModel'));
179184

180185
if ($this->getConfig('dropExpiredToken')) {
@@ -185,7 +190,7 @@ protected function _saveToken(ArrayAccess|array $identity, string $token): Entit
185190
// create token entity
186191
$entity = $tokenTable->newEntity([
187192
'model' => $userModel,
188-
'foreign_id' => $identity[$userTable->getPrimaryKey()],
193+
'foreign_id' => $identity[$primaryKey],
189194
'series' => static::_generateToken($identity),
190195
'token' => $token,
191196
'expires' => new DateTime($this->getConfig('cookie.expire')),

src/Identifier/RememberMeTokenIdentifier.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class RememberMeTokenIdentifier extends AbstractIdentifier
5050
/**
5151
* @inheritDoc
5252
*/
53-
protected function buildResolver($config): OrmResolver
53+
protected function buildResolver(array|string $config): OrmResolver
5454
{
5555
$instance = $this->traitBuildResolver($config);
5656

@@ -118,10 +118,10 @@ protected function _findIdentity(string $identifier): ArrayAccess|array|EntityIn
118118
}
119119

120120
/**
121-
* find user's remember me token.
121+
* find some user's remember me token.
122122
*
123123
* @param \Cake\Datasource\EntityInterface $identity the identity
124-
* @param string $series the credentials series
124+
* @param string $series the credential series
125125
* @return \Cake\Datasource\EntityInterface|null
126126
*/
127127
protected function _findToken(EntityInterface $identity, string $series): ?EntityInterface
@@ -133,11 +133,15 @@ protected function _findToken(EntityInterface $identity, string $series): ?Entit
133133

134134
$usersTable = $this->getResolver()->fetchTable($userModel);
135135
$tokenStorageTable = $this->getResolver()->fetchTable($this->getConfig('tokenStorageModel'));
136+
$primaryKey = $usersTable->getPrimaryKey();
137+
if (!is_string($primaryKey)) {
138+
throw new InvalidArgumentException('User model must have a single primary key.');
139+
}
136140

137141
return $tokenStorageTable->find()
138142
->where([
139143
'model' => $userModel,
140-
'foreign_id' => $identity->get($usersTable->getPrimaryKey()),
144+
'foreign_id' => $identity->get($primaryKey),
141145
'series' => $series,
142146
])
143147
->first();

src/Model/Entity/RememberMeToken.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,15 @@
2020
class RememberMeToken extends Entity
2121
{
2222
/**
23-
* Fields that can be mass assigned using newEntity() or patchEntity().
24-
*
25-
* Note that when '*' is set to true, this allows all unspecified fields to
26-
* be mass assigned. For security purposes, it is advised to set '*' to false
27-
* (or remove it), and explicitly make individual fields accessible as needed.
28-
*
29-
* @var array
23+
* @inheritDoc
3024
*/
3125
protected array $_accessible = [
3226
'*' => true,
3327
'id' => false,
3428
];
3529

3630
/**
37-
* Fields that are excluded from JSON versions of the entity.
38-
*
39-
* @var array
31+
* @inheritDoc
4032
*/
4133
protected array $_hidden = [
4234
'token',

0 commit comments

Comments
 (0)