Skip to content

Commit 072c078

Browse files
committed
refactor: add/fix type declarations
1 parent 7497404 commit 072c078

Some content is hidden

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

46 files changed

+262
-340
lines changed

src/Auth.php

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function __construct(Authentication $authenticate)
4040

4141
/**
4242
* Sets the Authenticator alias that should be used for this request.
43+
*
44+
* @return $this
4345
*/
4446
public function setAuthenticator(?string $alias = null)
4547
{
@@ -52,21 +54,17 @@ public function setAuthenticator(?string $alias = null)
5254

5355
/**
5456
* Returns the current authentication class.
55-
*
56-
* @return AuthenticatorInterface
5757
*/
58-
public function getAuthenticator()
58+
public function getAuthenticator(): AuthenticatorInterface
5959
{
6060
return $this->authenticate
6161
->factory($this->alias);
6262
}
6363

6464
/**
6565
* Returns the current user, if logged in.
66-
*
67-
* @return Authenticatable|null
6866
*/
69-
public function user()
67+
public function user(): ?Authenticatable
7068
{
7169
return $this->getAuthenticator()->loggedIn()
7270
? $this->getAuthenticator()->getUser()
@@ -76,7 +74,7 @@ public function user()
7674
/**
7775
* Returns the current user's id, if logged in.
7876
*
79-
* @return mixed|null
77+
* @return int|string|null
8078
*/
8179
public function id()
8280
{
@@ -85,7 +83,7 @@ public function id()
8583
: null;
8684
}
8785

88-
public function authenticate(array $credentials)
86+
public function authenticate(array $credentials): Result
8987
{
9088
$response = $this->authenticate
9189
->factory($this->alias)
@@ -106,11 +104,11 @@ public function authenticate(array $credentials)
106104
* - auth()->routes($routes);
107105
* - auth()->routes($routes, ['except' => ['login', 'register']])
108106
*/
109-
public function routes(RouteCollection &$routes, array $config = [])
107+
public function routes(RouteCollection &$routes, array $config = []): void
110108
{
111109
$authRoutes = config('AuthRoutes')->routes;
112110

113-
$routes->group('/', ['namespace' => 'CodeIgniter\Shield\Controllers'], static function ($routes) use ($authRoutes, $config) {
111+
$routes->group('/', ['namespace' => 'CodeIgniter\Shield\Controllers'], static function (RouteCollection $routes) use ($authRoutes, $config) {
114112
foreach ($authRoutes as $name => $row) {
115113
if (! isset($config['except']) || (isset($config['except']) && ! in_array($name, $config['except'], true))) {
116114
foreach ($row as $params) {
@@ -128,10 +126,8 @@ public function routes(RouteCollection &$routes, array $config = [])
128126
* Returns the Model that is responsible for getting users.
129127
*
130128
* @throws AuthenticationException
131-
*
132-
* @return mixed|UserProvider
133129
*/
134-
public function getProvider()
130+
public function getProvider(): UserProvider
135131
{
136132
if ($this->userProvider !== null) {
137133
return $this->userProvider;
@@ -155,12 +151,11 @@ public function getProvider()
155151
* own, additional, features on top of the required ones,
156152
* like "remember-me" functionality.
157153
*
158-
* @param string $method
159-
* @param array $args
154+
* @param string[] $args
160155
*
161156
* @throws AuthenticationException
162157
*/
163-
public function __call($method, $args)
158+
public function __call(string $method, array $args)
164159
{
165160
$authenticate = $this->authenticate->factory($this->alias);
166161

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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Authentication
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

@@ -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

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: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodeIgniter\Shield\Authentication;
44

55
use CodeIgniter\Shield\Interfaces\Authenticatable;
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(Authenticatable $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(): ?Authenticatable;
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)