Skip to content

Commit 7497404

Browse files
committed
refactor: add types for service()
1 parent c78450d commit 7497404

File tree

11 files changed

+71
-16
lines changed

11 files changed

+71
-16
lines changed

src/Auth.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
use CodeIgniter\Shield\Interfaces\Authenticatable;
1010
use CodeIgniter\Shield\Interfaces\UserProvider;
1111

12+
/**
13+
* AuthenticatorInterface:
14+
*
15+
* @method Result attempt(array $credentials)
16+
* @method Result check(array $credentials)
17+
* @method Authenticatable|null getUser()
18+
* @method bool loggedIn()
19+
* @method bool login(Authenticatable $user)
20+
* @method void loginById($userId)
21+
* @method bool logout()
22+
* @method void recordActive()
23+
*/
1224
class Auth
1325
{
1426
protected Authentication $authenticate;

src/Authentication/Authenticators/AccessTokens.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodeIgniter\Shield\Authentication\Authenticators;
44

5+
use CodeIgniter\HTTP\IncomingRequest;
56
use CodeIgniter\I18n\Time;
67
use CodeIgniter\Shield\Authentication\AuthenticationException;
78
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
@@ -46,7 +47,9 @@ public function __construct($provider)
4647
*/
4748
public function attempt(array $credentials): Result
4849
{
49-
$request = service('request');
50+
/** @var IncomingRequest $request */
51+
$request = service('request');
52+
5053
$ipAddress = $request->getIPAddress();
5154
$userAgent = $request->getUserAgent();
5255

@@ -138,8 +141,11 @@ public function loggedIn(): bool
138141
return true;
139142
}
140143

144+
/** @var IncomingRequest $request */
145+
$request = service('request');
146+
141147
return $this->attempt([
142-
'token' => service('request')->getHeaderLine(config('Auth')->authenticatorHeader['tokens']),
148+
'token' => $request->getHeaderLine(config('Auth')->authenticatorHeader['tokens']),
143149
])->isOK();
144150
}
145151

@@ -216,6 +222,7 @@ public function getUser()
216222
*/
217223
public function getBearerToken()
218224
{
225+
/** @var IncomingRequest $request */
219226
$request = service('request');
220227

221228
$header = $request->getHeaderLine(config('Auth')->authenticatorHeader['tokens']);

src/Authentication/Authenticators/Session.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace CodeIgniter\Shield\Authentication\Authenticators;
44

55
use CodeIgniter\Events\Events;
6+
use CodeIgniter\HTTP\IncomingRequest;
7+
use CodeIgniter\HTTP\Response;
68
use CodeIgniter\I18n\Time;
79
use CodeIgniter\Shield\Authentication\AuthenticationException;
810
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
@@ -60,7 +62,9 @@ public function remember(bool $shouldRemember = true)
6062
*/
6163
public function attempt(array $credentials)
6264
{
63-
$request = service('request');
65+
/** @var IncomingRequest $request */
66+
$request = service('request');
67+
6468
$ipAddress = $request->getIPAddress();
6569
$userAgent = $request->getUserAgent();
6670
$result = $this->check($credentials);
@@ -116,10 +120,10 @@ public function check(array $credentials)
116120
]);
117121
}
118122

119-
// Now, try matching the passwords.
120123
/** @var Passwords $passwords */
121124
$passwords = service('passwords');
122125

126+
// Now, try matching the passwords.
123127
if (! $passwords->verify($givenPassword, $user->password_hash)) {
124128
return new Result([
125129
'success' => false,
@@ -190,8 +194,11 @@ public function login(Authenticatable $user)
190194
// Let the session know we're logged in
191195
session()->set(setting('Auth.sessionConfig')['field'], $this->user->getAuthId());
192196

197+
/** @var Response $response */
198+
$response = service('response');
199+
193200
// When logged in, ensure cache control headers are in place
194-
service('response')->noCache();
201+
$response->noCache();
195202

196203
if ($this->shouldRemember && setting('Auth.sessionConfig')['allowRemembering']) {
197204
$this->rememberUser($this->user->getAuthId());
@@ -330,9 +337,10 @@ protected function rememberUser($userId): void
330337
// Store it in the database
331338
$this->rememberModel->rememberUser($userId, $selector, hash('sha256', $validator), $expires);
332339

333-
// Save it to the user's browser in a cookie.
340+
/** @var Response $response */
334341
$response = service('response');
335342

343+
// Save it to the user's browser in a cookie.
336344
// Create the cookie
337345
$response->setCookie(
338346
setting('Auth.sessionConfig')['rememberCookieName'],

src/Authentication/Passwords/ValidationRules.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodeIgniter\Shield\Authentication\Passwords;
44

5+
use CodeIgniter\HTTP\IncomingRequest;
56
use CodeIgniter\Shield\Entities\User;
67

78
/**
@@ -61,7 +62,10 @@ protected function buildUserFromRequest()
6162
{
6263
$fields = $this->prepareValidFields();
6364

64-
$data = service('request')->getPost($fields);
65+
/** @var IncomingRequest $request */
66+
$request = service('request');
67+
68+
$data = $request->getPost($fields);
6569

6670
return new User($data);
6771
}

src/Controllers/LoginController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Controllers\BaseController;
66
use CodeIgniter\Events\Events;
7+
use CodeIgniter\HTTP\IncomingRequest;
78
use CodeIgniter\HTTP\RedirectResponse;
89

910
class LoginController extends BaseController
@@ -23,7 +24,9 @@ public function loginView(): string
2324
*/
2425
public function loginAction(): RedirectResponse
2526
{
26-
$request = service('request');
27+
/** @var IncomingRequest $request */
28+
$request = service('request');
29+
2730
$credentials = $request->getPost(setting('Auth.validFields'));
2831
$credentials = array_filter($credentials);
2932
$credentials['password'] = $request->getPost('password');

src/Controllers/MagicLinkController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Controllers\BaseController;
66
use CodeIgniter\HTTP\RedirectResponse;
77
use CodeIgniter\I18n\Time;
8+
use CodeIgniter\Shield\Auth;
89
use CodeIgniter\Shield\Interfaces\UserProvider;
910
use CodeIgniter\Shield\Models\UserIdentityModel;
1011

@@ -117,8 +118,10 @@ public function verify(): RedirectResponse
117118
return redirect()->route('magic-link')->with('error', lang('Auth.magicLinkExpired'));
118119
}
119120

120-
// Log the user in
121+
/** @var Auth $auth */
121122
$auth = service('auth');
123+
124+
// Log the user in
122125
$auth->loginById($identity->user_id);
123126

124127
// Get our login redirect url

src/Controllers/RegisterController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CodeIgniter\Events\Events;
77
use CodeIgniter\HTTP\RedirectResponse;
88
use CodeIgniter\Shield\Entities\User;
9+
use CodeIgniter\Validation\Validation;
910

1011
/**
1112
* Class RegisterController
@@ -50,9 +51,11 @@ public function registerAction(): RedirectResponse
5051
// like the password, can only be validated properly here.
5152
$rules = $this->getValidationRules();
5253

54+
/** @var Validation $validation */
55+
$validation = service('validation');
56+
5357
if (! $this->validate($rules)) {
54-
return redirect()->back()->withInput()
55-
->with('errors', service('validation')->getErrors());
58+
return redirect()->back()->withInput()->with('errors', $validation->getErrors());
5659
}
5760

5861
// Save the user

src/Entities/User.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodeIgniter\Shield\Entities;
44

55
use CodeIgniter\Entity\Entity;
6+
use CodeIgniter\Shield\Authentication\Passwords;
67
use CodeIgniter\Shield\Authentication\Traits\Authenticatable;
78
use CodeIgniter\Shield\Authentication\Traits\HasAccessTokens;
89
use CodeIgniter\Shield\Authorization\Traits\Authorizable;
@@ -96,11 +97,14 @@ public function createEmailIdentity(array $credentials)
9697
/** @var UserIdentityModel $identityModel */
9798
$identityModel = model(UserIdentityModel::class);
9899

100+
/** @var Passwords $passwords */
101+
$passwords = service('passwords');
102+
99103
$identityModel->insert([
100104
'user_id' => $this->id,
101105
'type' => 'email_password',
102106
'secret' => $credentials['email'],
103-
'secret2' => service('passwords')->hash($credentials['password']),
107+
'secret2' => $passwords->hash($credentials['password']),
104108
]);
105109
}
106110

src/Entities/UserIdentity.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodeIgniter\Shield\Entities;
44

55
use CodeIgniter\Entity\Entity;
6+
use CodeIgniter\Shield\Authentication\Passwords;
67

78
/**
89
* Class UserIdentity
@@ -36,7 +37,10 @@ class UserIdentity extends Entity
3637
*/
3738
public function hashSecret(string $value)
3839
{
39-
$this->attributes['secret'] = service('passwords')->hash($value);
40+
/** @var Passwords $passwords */
41+
$passwords = service('passwords');
42+
43+
$this->attributes['secret'] = $passwords->hash($value);
4044

4145
return $this;
4246
}

src/Helpers/auth_helper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ function auth(?string $alias = null)
2727
*/
2828
function user_id()
2929
{
30-
return service('auth')->id();
30+
/** @var Auth $auth */
31+
$auth = service('auth');
32+
33+
return $auth->id();
3134
}
3235
}

0 commit comments

Comments
 (0)