Skip to content

Commit c080d0f

Browse files
committed
improve docblocks typehinting with phpstan l6
1 parent 63a6568 commit c080d0f

7 files changed

Lines changed: 148 additions & 60 deletions

File tree

phpstan.dist.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ includes:
33
- phpstan-baseline.neon
44

55
parameters:
6-
level: 4
6+
level: 6
77
paths:
88
- src
99
- tests

src/Auth.php

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ class Auth
3434

3535
/**
3636
* All errors caught
37-
* @var array
37+
* @var array<string, string>
3838
*/
3939
protected $errorsArray = [];
4040

41-
/**
42-
* Configured oauth clients
43-
*/
41+
/** @var array<string, \League\OAuth2\Client\Provider\AbstractProvider> Configured oauth clients */
4442
protected $oauthClients = [];
4543

4644
public function __construct()
@@ -113,7 +111,14 @@ class_exists('League\OAuth2\Client\Provider\Google') &&
113111

114112
/**
115113
* Connect leaf auth to the database
116-
* @param array $dbConfig Configuration for leaf db connection
114+
* @param array{
115+
* host?: string,
116+
* dbname?: string,
117+
* username?: string,
118+
* password?: string,
119+
* dbtype?: string,
120+
* pdoOptions?: mixed[],
121+
* } $dbConfig Configuration for leaf db connection
117122
* @return $this
118123
*/
119124
public function connect($dbConfig = [])
@@ -127,7 +132,7 @@ public function connect($dbConfig = [])
127132
/**
128133
* Connect to database using environment variables
129134
*
130-
* @param array $pdoOptions Options for PDO connection
135+
* @param mixed[] $pdoOptions Options for PDO connection
131136
* @return $this
132137
*/
133138
public function autoConnect(array $pdoOptions = [])
@@ -158,7 +163,10 @@ public function dbConnection(\PDO $connection)
158163
* Register a Google OAuth client to use with Leaf Auth, should be a league/oauth2-client compatible client.
159164
* @param string $clientId
160165
* @param string $clientSecret
161-
* @param array $options
166+
* @param array{
167+
* name?: string,
168+
* redirectUri?: string,
169+
* } $options
162170
* @return static
163171
*/
164172
public function withGoogle(
@@ -210,8 +218,9 @@ public function client(string $clientName)
210218
/**
211219
* Get/Set Leaf Auth config
212220
*
213-
* @param string|array $config The auth config key or array of config
221+
* @param string|array<string, mixed> $config The auth config key or array of config
214222
* @param mixed $value The value if $config is a string
223+
* @return mixed|void
215224
*/
216225
public function config($config, $value = null)
217226
{
@@ -229,7 +238,7 @@ public function config($config, $value = null)
229238
/**
230239
* Create roles and permissions
231240
*
232-
* @param array $roles Array of roles and their permissions
241+
* @param array<string, string[]> $roles Array of roles and their permissions
233242
* @return Auth
234243
*/
235244
public function createRoles(array $roles)
@@ -244,7 +253,7 @@ public function createRoles(array $roles)
244253
/**
245254
* Return all roles and their permissions
246255
*
247-
* @return array
256+
* @return array<string, string[]>
248257
*/
249258
public function roles()
250259
{
@@ -256,7 +265,7 @@ public function roles()
256265
* ---
257266
* Verify user credentials and sign them in with token or session
258267
*
259-
* @param array $credentials User credentials
268+
* @param array<string, mixed> $credentials User credentials
260269
* @return bool
261270
*/
262271
public function login(array $credentials): bool
@@ -310,7 +319,7 @@ public function login(array $credentials): bool
310319
* ---
311320
* Save a new user to the database
312321
*
313-
* @param array $userData User data
322+
* @param array<string, mixed> $userData User data
314323
* @return bool
315324
*/
316325
public function register(array $userData): bool
@@ -371,7 +380,7 @@ public function register(array $userData): bool
371380
* ---
372381
* Update user data in the database
373382
*
374-
* @param array $userData User data
383+
* @param array<string, mixed> $userData User data
375384
* @return bool
376385
*/
377386
public function update(array $userData): bool
@@ -495,7 +504,7 @@ public function updatePassword(string $oldPassword, string $newPassword): bool
495504
/**
496505
* Create a new user from OAuth
497506
*
498-
* @param array $userData User data
507+
* @param array<string, mixed> $userData User data
499508
*
500509
* @return bool
501510
*/
@@ -553,7 +562,9 @@ public function find($id)
553562
* ---
554563
* Create an account for another user
555564
*
556-
* @param array $userData The user details to save
565+
* @param array<string, mixed> $userData The user details to save
566+
* @return User|false|never
567+
* @throws \Exception If database connection is not established
557568
*/
558569
public function createUserFor($userData)
559570
{
@@ -608,6 +619,7 @@ public function createUserFor($userData)
608619

609620
/**
610621
* Get saved OAuth token
622+
* @return mixed
611623
*/
612624
public function oauthToken()
613625
{
@@ -619,7 +631,7 @@ public function oauthToken()
619631
* ---
620632
* Sign out the currently authenticated user
621633
*
622-
* @param string|array|callable|null $action Redirect to this url after logout
634+
* @param string|mixed[]|callable|null $action Redirect to this url after logout
623635
* @return bool
624636
*/
625637
public function logout($action = null): bool
@@ -716,7 +728,10 @@ public function data()
716728

717729
/**
718730
* Get generated access tokens
719-
* @return array|null
731+
* @return array{
732+
* access?: string,
733+
* refresh?: string,
734+
* }|null
720735
*/
721736
public function tokens()
722737
{
@@ -733,6 +748,8 @@ public function tokens()
733748
* Register auth middleware for your Leaf apps
734749
* @param string $middleware The middleware to register
735750
* @param callable $callback The callback to run if middleware fails
751+
* @return void|never
752+
* @throws \Exception If not used with leafs/leaf installed
736753
*/
737754
public function middleware(string $middleware, callable $callback)
738755
{
@@ -741,84 +758,101 @@ public function middleware(string $middleware, callable $callback)
741758
}
742759

743760
if ($middleware === 'auth.required') {
744-
return app()->registerMiddleware('auth.required', function () use ($callback) {
761+
app()->registerMiddleware('auth.required', function () use ($callback) {
745762
if (!$this->user()) {
746763
$callback();
747764
exit;
748765
}
749766
});
767+
768+
return;
750769
}
751770

752771
if ($middleware === 'auth.guest') {
753-
return app()->registerMiddleware('auth.guest', function () use ($callback) {
772+
app()->registerMiddleware('auth.guest', function () use ($callback) {
754773
if ($this->user()) {
755774
$callback();
756775
exit;
757776
}
758777

759778
auth()->clearErrors();
760779
});
780+
781+
return;
761782
}
762783

763784
if ($middleware === 'is') {
764-
return app()->registerMiddleware('is', function ($role) use ($callback) {
785+
app()->registerMiddleware('is', function ($role) use ($callback) {
765786
if (!$this->user() || $this->user()->isNot($role)) {
766787
$callback($role);
767788
exit;
768789
}
769790
});
791+
792+
return;
770793
}
771794

772795
if ($middleware === 'isNot') {
773-
return app()->registerMiddleware('isNot', function ($role) use ($callback) {
796+
app()->registerMiddleware('isNot', function ($role) use ($callback) {
774797
if (!$this->user() || $this->user()->is($role)) {
775798
$callback($role);
776799
exit;
777800
}
778801
});
802+
803+
return;
779804
}
780805

781806
if ($middleware === 'can') {
782-
return app()->registerMiddleware('can', function ($role) use ($callback) {
807+
app()->registerMiddleware('can', function ($role) use ($callback) {
783808
if (!$this->user() || $this->user()->cannot($role)) {
784809
$callback($role);
785810
exit;
786811
}
787812
});
813+
814+
return;
788815
}
789816

790817
if ($middleware === 'cannot') {
791-
return app()->registerMiddleware('cannot', function ($role) use ($callback) {
818+
app()->registerMiddleware('cannot', function ($role) use ($callback) {
792819
if (!$this->user() || $this->user()->can($role)) {
793820
$callback($role);
794821
exit;
795822
}
796823
});
824+
825+
return;
797826
}
798827

799828
if ($middleware === 'auth.verified') {
800-
return app()->registerMiddleware('auth.verified', function () use ($callback) {
829+
app()->registerMiddleware('auth.verified', function () use ($callback) {
801830
if (!$this->user() || !$this->user()->isVerified()) {
802831
$callback();
803832
exit;
804833
}
805834
});
835+
836+
return;
806837
}
807838

808839
if ($middleware === 'auth.unverified') {
809-
return app()->registerMiddleware('auth.unverified', function () use ($callback) {
840+
app()->registerMiddleware('auth.unverified', function () use ($callback) {
810841
if (!$this->user() || $this->user()->isVerified()) {
811842
$callback();
812843
exit;
813844
}
814845
});
846+
847+
return;
815848
}
816849

817850
app()->registerMiddleware($middleware, $callback);
818851
}
819852

820853
/**
821854
* Parse the current user's token
855+
* @return array<string, mixed>|null
822856
*/
823857
public function parseToken()
824858
{
@@ -904,6 +938,10 @@ protected function checkDbConnection(): void
904938
}
905939
}
906940

941+
/**
942+
* @param string|array<string, mixed> $value
943+
* @return ?mixed
944+
*/
907945
protected function getFromSession($value)
908946
{
909947
if ($this->checkAndExpireSession()) {
@@ -913,6 +951,10 @@ protected function getFromSession($value)
913951
return Session::get($value);
914952
}
915953

954+
/**
955+
* @return void|never
956+
* @throws \Exception If sessions are not enabled
957+
*/
916958
protected function sessionCheck()
917959
{
918960
if (!Config::get('session')) {
@@ -937,6 +979,7 @@ protected function checkAndExpireSession(): bool
937979
return $isSessionExpired;
938980
}
939981

982+
/** @return ?string */
940983
protected function getTokenFromRequest()
941984
{
942985
$headers = null;
@@ -966,13 +1009,15 @@ protected function getTokenFromRequest()
9661009
return null;
9671010
}
9681011

1012+
/** @return ?mixed */
9691013
protected function getTokenFromSession()
9701014
{
9711015
return Session::get('auth.token');
9721016
}
9731017

9741018
/**
9751019
* Clear all errors caught
1020+
* @return void
9761021
*/
9771022
public function clearErrors()
9781023
{
@@ -981,6 +1026,7 @@ public function clearErrors()
9811026

9821027
/**
9831028
* Return all errors caught
1029+
* @return array<string, mixed>
9841030
*/
9851031
public function errors(): array
9861032
{

0 commit comments

Comments
 (0)