Skip to content

Commit 59bca13

Browse files
authored
Merge pull request #44 from kenjis/add-types
refactor: add type declarations
2 parents ba15c38 + 2b33954 commit 59bca13

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+371
-503
lines changed

src/Auth.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,24 @@
66
use CodeIgniter\Shield\Authentication\Authentication;
77
use CodeIgniter\Shield\Authentication\AuthenticationException;
88
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
9-
use CodeIgniter\Shield\Interfaces\Authenticatable;
10-
use CodeIgniter\Shield\Interfaces\UserProvider;
11-
9+
use CodeIgniter\Shield\Entities\User;
10+
use CodeIgniter\Shield\Models\UserModel;
11+
12+
/**
13+
* AuthenticatorInterface:
14+
*
15+
* @method Result attempt(array $credentials)
16+
* @method Result check(array $credentials)
17+
* @method User|null getUser()
18+
* @method bool loggedIn()
19+
* @method bool login(User $user)
20+
* @method void loginById($userId)
21+
* @method bool logout()
22+
* @method void recordActive()
23+
*
24+
* Authenticators\Session:
25+
* @method $this remember(bool $shouldRemember = true)
26+
*/
1227
class Auth
1328
{
1429
protected Authentication $authenticate;
@@ -18,8 +33,8 @@ class Auth
1833
*/
1934
protected ?string $alias = null;
2035

21-
protected ?Authenticatable $user = null;
22-
protected ?UserProvider $userProvider = null;
36+
protected ?User $user = null;
37+
protected ?UserModel $userProvider = null;
2338

2439
public function __construct(Authentication $authenticate)
2540
{
@@ -28,8 +43,10 @@ public function __construct(Authentication $authenticate)
2843

2944
/**
3045
* Sets the Authenticator alias that should be used for this request.
46+
*
47+
* @return $this
3148
*/
32-
public function setAuthenticator(?string $alias = null)
49+
public function setAuthenticator(?string $alias = null): self
3350
{
3451
if (! empty($alias)) {
3552
$this->alias = $alias;
@@ -40,21 +57,17 @@ public function setAuthenticator(?string $alias = null)
4057

4158
/**
4259
* Returns the current authentication class.
43-
*
44-
* @return AuthenticatorInterface
4560
*/
46-
public function getAuthenticator()
61+
public function getAuthenticator(): AuthenticatorInterface
4762
{
4863
return $this->authenticate
4964
->factory($this->alias);
5065
}
5166

5267
/**
5368
* Returns the current user, if logged in.
54-
*
55-
* @return Authenticatable|null
5669
*/
57-
public function user()
70+
public function user(): ?User
5871
{
5972
return $this->getAuthenticator()->loggedIn()
6073
? $this->getAuthenticator()->getUser()
@@ -64,7 +77,7 @@ public function user()
6477
/**
6578
* Returns the current user's id, if logged in.
6679
*
67-
* @return mixed|null
80+
* @return int|string|null
6881
*/
6982
public function id()
7083
{
@@ -73,7 +86,7 @@ public function id()
7386
: null;
7487
}
7588

76-
public function authenticate(array $credentials)
89+
public function authenticate(array $credentials): Result
7790
{
7891
$response = $this->authenticate
7992
->factory($this->alias)
@@ -94,11 +107,11 @@ public function authenticate(array $credentials)
94107
* - auth()->routes($routes);
95108
* - auth()->routes($routes, ['except' => ['login', 'register']])
96109
*/
97-
public function routes(RouteCollection &$routes, array $config = [])
110+
public function routes(RouteCollection &$routes, array $config = []): void
98111
{
99112
$authRoutes = config('AuthRoutes')->routes;
100113

101-
$routes->group('/', ['namespace' => 'CodeIgniter\Shield\Controllers'], static function ($routes) use ($authRoutes, $config) {
114+
$routes->group('/', ['namespace' => 'CodeIgniter\Shield\Controllers'], static function (RouteCollection $routes) use ($authRoutes, $config) {
102115
foreach ($authRoutes as $name => $row) {
103116
if (! isset($config['except']) || (isset($config['except']) && ! in_array($name, $config['except'], true))) {
104117
foreach ($row as $params) {
@@ -116,10 +129,8 @@ public function routes(RouteCollection &$routes, array $config = [])
116129
* Returns the Model that is responsible for getting users.
117130
*
118131
* @throws AuthenticationException
119-
*
120-
* @return mixed|UserProvider
121132
*/
122-
public function getProvider()
133+
public function getProvider(): UserModel
123134
{
124135
if ($this->userProvider !== null) {
125136
return $this->userProvider;
@@ -143,12 +154,11 @@ public function getProvider()
143154
* own, additional, features on top of the required ones,
144155
* like "remember-me" functionality.
145156
*
146-
* @param string $method
147-
* @param array $args
157+
* @param string[] $args
148158
*
149159
* @throws AuthenticationException
150160
*/
151-
public function __call($method, $args)
161+
public function __call(string $method, array $args)
152162
{
153163
$authenticate = $this->authenticate->factory($this->alias);
154164

src/Authentication/Actions/ActionInterface.php

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

55
use CodeIgniter\HTTP\IncomingRequest;
6+
use CodeIgniter\HTTP\Response;
67

78
/**
89
* Interface ActionInterface
@@ -18,14 +19,14 @@ interface ActionInterface
1819
* This might be asking for the user's email to reset a password,
1920
* or asking for a cell-number for a 2FA.
2021
*
21-
* @return mixed
22+
* @return Response|string
2223
*/
2324
public function show();
2425

2526
/**
2627
* Processes the form that was displayed in the previous form.
2728
*
28-
* @return mixed
29+
* @return Response|string
2930
*/
3031
public function handle(IncomingRequest $request);
3132

@@ -35,7 +36,7 @@ public function handle(IncomingRequest $request);
3536
* from clicking the 'confirm my email' action or
3637
* following entering a code sent in an SMS.
3738
*
38-
* @return mixed
39+
* @return Response|string
3940
*/
4041
public function verify(IncomingRequest $request);
4142
}

src/Authentication/Actions/Email2FA.php

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

55
use CodeIgniter\HTTP\IncomingRequest;
6+
use CodeIgniter\HTTP\RedirectResponse;
67
use CodeIgniter\Shield\Models\UserIdentityModel;
78

89
/**
@@ -15,10 +16,8 @@ class Email2FA implements ActionInterface
1516
/**
1617
* Displays the "Hey we're going to send you an number to your email"
1718
* message to the user with a prompt to continue.
18-
*
19-
* @return mixed
2019
*/
21-
public function show()
20+
public function show(): string
2221
{
2322
$user = auth()->user();
2423

@@ -48,7 +47,7 @@ public function show()
4847
* with the user, and fires off an email to the user with the code,
4948
* then displays the form to accept the 6 digits
5049
*
51-
* @return mixed
50+
* @return RedirectResponse|string
5251
*/
5352
public function handle(IncomingRequest $request)
5453
{
@@ -83,7 +82,7 @@ public function handle(IncomingRequest $request)
8382
/**
8483
* Attempts to verify the code the user entered.
8584
*
86-
* @return mixed
85+
* @return RedirectResponse|string
8786
*/
8887
public function verify(IncomingRequest $request)
8988
{

src/Authentication/Actions/EmailActivator.php

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

55
use CodeIgniter\Exceptions\PageNotFoundException;
66
use CodeIgniter\HTTP\IncomingRequest;
7+
use CodeIgniter\HTTP\RedirectResponse;
78
use CodeIgniter\Shield\Models\UserIdentityModel;
89

910
class EmailActivator implements ActionInterface
@@ -12,10 +13,8 @@ class EmailActivator implements ActionInterface
1213
* Shows the initial screen to the user telling them
1314
* that an email was just sent to them with a link
1415
* to confirm their email address.
15-
*
16-
* @return mixed
1716
*/
18-
public function show()
17+
public function show(): string
1918
{
2019
$user = auth()->user();
2120

@@ -47,13 +46,11 @@ public function show()
4746
->send();
4847

4948
// Display the info page
50-
echo view(setting('Auth.views')['action_email_activate_show'], ['user' => $user]);
49+
return view(setting('Auth.views')['action_email_activate_show'], ['user' => $user]);
5150
}
5251

5352
/**
5453
* This method is unused.
55-
*
56-
* @return mixed
5754
*/
5855
public function handle(IncomingRequest $request)
5956
{
@@ -64,7 +61,7 @@ public function handle(IncomingRequest $request)
6461
* Verifies the email address and code matches an
6562
* identity we have for that user.
6663
*
67-
* @return mixed
64+
* @return RedirectResponse|string
6865
*/
6966
public function verify(IncomingRequest $request)
7067
{

src/Authentication/Authentication.php

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

55
use CodeIgniter\Shield\Config\Auth as AuthConfig;
6-
use CodeIgniter\Shield\Interfaces\UserProvider;
6+
use CodeIgniter\Shield\Models\UserModel;
77

88
class Authentication
99
{
1010
/**
1111
* Instantiated Authenticator objects,
1212
* stored by Authenticator alias.
1313
*
14-
* @var array<string, AuthenticatorInterface>
14+
* @var array<string, AuthenticatorInterface> [Authenticator_alias => Authenticator_instance]
1515
*/
1616
protected array $instances = [];
1717

18-
protected ?UserProvider $userProvider = null;
18+
protected ?UserModel $userProvider = null;
1919
protected AuthConfig $config;
2020

2121
public function __construct(AuthConfig $config)
@@ -33,10 +33,8 @@ public function __construct(AuthConfig $config)
3333
* @param string|null $alias Authenticator alias
3434
*
3535
* @throws AuthenticationException
36-
*
37-
* @return AuthenticatorInterface
3836
*/
39-
public function factory(?string $alias = null)
37+
public function factory(?string $alias = null): AuthenticatorInterface
4038
{
4139
// Determine actual Authenticator alias
4240
$alias ??= $this->config->defaultAuthenticator;
@@ -53,7 +51,7 @@ public function factory(?string $alias = null)
5351

5452
$className = $this->config->authenticators[$alias];
5553

56-
assert($this->userProvider !== null, '$userProvider must be set.');
54+
assert($this->userProvider !== null, 'You must set $this->userProvider.');
5755

5856
$this->instances[$alias] = new $className($this->userProvider);
5957

@@ -65,7 +63,7 @@ public function factory(?string $alias = null)
6563
*
6664
* @return $this
6765
*/
68-
public function setProvider(UserProvider $provider)
66+
public function setProvider(UserModel $provider): self
6967
{
7068
$this->userProvider = $provider;
7169

src/Authentication/AuthenticationException.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ class AuthenticationException extends Exception
1212
/**
1313
* @param string $alias Authenticator alias
1414
*/
15-
public static function forUnknownAuthenticator(string $alias)
15+
public static function forUnknownAuthenticator(string $alias): self
1616
{
1717
return new self(lang('Auth.unknownAuthenticator', [$alias]));
1818
}
1919

20-
public static function forUnknownUserProvider()
20+
public static function forUnknownUserProvider(): self
2121
{
2222
return new self(lang('Auth.unknownUserProvider'));
2323
}
2424

25-
public static function forInvalidUser()
25+
public static function forInvalidUser(): self
2626
{
2727
return new self(lang('Auth.invalidUser'));
2828
}
2929

30-
public static function forNoEntityProvided()
30+
public static function forNoEntityProvided(): self
3131
{
3232
return new self(lang('Auth.noUserEntity'), 500);
3333
}

src/Authentication/AuthenticatorInterface.php

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

33
namespace CodeIgniter\Shield\Authentication;
44

5-
use CodeIgniter\Shield\Interfaces\Authenticatable;
5+
use CodeIgniter\Shield\Entities\User;
6+
use CodeIgniter\Shield\Result;
67

78
interface AuthenticatorInterface
89
{
@@ -11,18 +12,14 @@ interface AuthenticatorInterface
1112
* Logs the user in with a successful check.
1213
*
1314
* @throws AuthenticationException
14-
*
15-
* @return mixed
1615
*/
17-
public function attempt(array $credentials);
16+
public function attempt(array $credentials): Result;
1817

1918
/**
2019
* Checks a user's $credentials to see if they match an
2120
* existing user.
22-
*
23-
* @return mixed
2421
*/
25-
public function check(array $credentials);
22+
public function check(array $credentials): Result;
2623

2724
/**
2825
* Checks if the user is currently logged in.
@@ -34,10 +31,8 @@ public function loggedIn(): bool;
3431
* On success this must trigger the "login" Event.
3532
*
3633
* @see https://codeigniter4.github.io/CodeIgniter4/extending/authentication.html
37-
*
38-
* @return mixed
3934
*/
40-
public function login(Authenticatable $user);
35+
public function login(User $user): bool;
4136

4237
/**
4338
* Logs a user in based on their ID.
@@ -54,22 +49,16 @@ public function loginById($userId): void;
5449
* On success this must trigger the "logout" Event.
5550
*
5651
* @see https://codeigniter4.github.io/CodeIgniter4/extending/authentication.html
57-
*
58-
* @return mixed
5952
*/
60-
public function logout();
53+
public function logout(): bool;
6154

6255
/**
6356
* Returns the currently logged in user.
64-
*
65-
* @return Authenticatable|null
6657
*/
67-
public function getUser();
58+
public function getUser(): ?User;
6859

6960
/**
7061
* Updates the user's last active date.
71-
*
72-
* @return mixed
7362
*/
74-
public function recordActive();
63+
public function recordActive(): void;
7564
}

0 commit comments

Comments
 (0)