Skip to content

Commit

Permalink
Merge pull request #45 from kenjis/chore-add-phpstan
Browse files Browse the repository at this point in the history
chore: add PHPStan
  • Loading branch information
datamweb authored Aug 23, 2023
2 parents 7654f13 + 0f27698 commit c8a5db3
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 53 deletions.
188 changes: 188 additions & 0 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php declare(strict_types = 1);

$ignoreErrors = [];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Commands\\\\OAuthSetup\\:\\:copyAndReplace\\(\\) has parameter \\$replaces with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Commands/OAuthSetup.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Commands\\\\OAuthSetup\\:\\:\\$arguments type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Commands/OAuthSetup.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Commands\\\\OAuthSetup\\:\\:\\$distPath has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Commands/OAuthSetup.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Config\\\\Registrar\\:\\:Generators\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Config/Registrar.php',
];
$ignoreErrors[] = [
'message' => '#^Call to deprecated function random_string\\(\\)\\:
The type \'basic\', \'md5\', and \'sha1\' are deprecated\\. They are not cryptographically secure\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Controllers/OAuthController.php',
];
$ignoreErrors[] = [
'message' => '#^Class Datamweb\\\\ShieldOAuth\\\\Controllers\\\\LoginModel not found\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Controllers/OAuthController.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Controllers\\\\OAuthController\\:\\:checkExistenceUser\\(\\) has parameter \\$find with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Controllers/OAuthController.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Controllers\\\\OAuthController\\:\\:syncingUserInfo\\(\\) has parameter \\$find with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Controllers/OAuthController.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Controllers\\\\OAuthController\\:\\:syncingUserInfo\\(\\) has parameter \\$updateFildes with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Controllers/OAuthController.php',
];
$ignoreErrors[] = [
'message' => '#^Access to an undefined property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\Basic\\\\AbstractOAuth\\:\\:\\$token\\.$#',
'count' => 2,
'path' => __DIR__ . '/src/Libraries/Basic/AbstractOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Libraries\\\\Basic\\\\AbstractOAuth\\:\\:fetchAccessTokenWithAuthCode\\(\\) has parameter \\$allGet with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/Basic/AbstractOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Libraries\\\\Basic\\\\AbstractOAuth\\:\\:getColumnsName\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/Basic/AbstractOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Libraries\\\\Basic\\\\AbstractOAuth\\:\\:getUserInfo\\(\\) has parameter \\$allGet with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/Basic/AbstractOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Libraries\\\\Basic\\\\AbstractOAuth\\:\\:setColumnsName\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/Basic/AbstractOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Call to deprecated function random_string\\(\\)\\:
The type \'basic\', \'md5\', and \'sha1\' are deprecated\\. They are not cryptographically secure\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GithubOAuth\\:\\:fetchAccessTokenWithAuthCode\\(\\) has parameter \\$allGet with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GithubOAuth\\:\\:setColumnsName\\(\\) has parameter \\$userInfo with no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GithubOAuth\\:\\:setColumnsName\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GithubOAuth\\:\\:\\$API_CODE_URL has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GithubOAuth\\:\\:\\$API_TOKEN_URL has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GithubOAuth\\:\\:\\$API_USER_INFO_URL has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GithubOAuth\\:\\:\\$APPLICATION_NAME has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GithubOAuth\\:\\:\\$client has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GithubOAuth\\:\\:\\$config has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Variable \\$usersColumnsName might not be defined\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GithubOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Call to deprecated function random_string\\(\\)\\:
The type \'basic\', \'md5\', and \'sha1\' are deprecated\\. They are not cryptographically secure\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GoogleOAuth\\:\\:fetchAccessTokenWithAuthCode\\(\\) has parameter \\$allGet with no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GoogleOAuth\\:\\:setColumnsName\\(\\) has parameter \\$userInfo with no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Method Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GoogleOAuth\\:\\:setColumnsName\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GoogleOAuth\\:\\:\\$API_CODE_URL has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GoogleOAuth\\:\\:\\$API_TOKEN_URL has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GoogleOAuth\\:\\:\\$API_USER_INFO_URL has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GoogleOAuth\\:\\:\\$APPLICATION_NAME has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GoogleOAuth\\:\\:\\$client has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Property Datamweb\\\\ShieldOAuth\\\\Libraries\\\\GoogleOAuth\\:\\:\\$config has no type specified\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];
$ignoreErrors[] = [
'message' => '#^Variable \\$usersColumnsName might not be defined\\.$#',
'count' => 1,
'path' => __DIR__ . '/src/Libraries/GoogleOAuth.php',
];

return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
23 changes: 23 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
includes:
- phpstan-baseline.php
parameters:
tmpDir: build/phpstan
level: 6
paths:
- src/
bootstrapFiles:
- vendor/codeigniter4/framework/system/Test/bootstrap.php
excludePaths:
- app/Config/Routes.php
- app/Views/*
ignoreErrors:
universalObjectCratesClasses:
- CodeIgniter\Entity
- CodeIgniter\Entity\Entity
- Faker\Generator
scanDirectories:
- vendor/codeigniter4/framework/system/Helpers
dynamicConstantNames:
- APP_NAMESPACE
- CI_DEBUG
- ENVIRONMENT
7 changes: 7 additions & 0 deletions src/Config/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@
class Registrar
{
/**
* --------------------------------------------------------------------------
* Shield OAuth Decorator
* --------------------------------------------------------------------------
* Register the ShieldOAuth decorator.
*
* @see https://codeigniter.com/user_guide/outgoing/view_decorators.html
*
* @return array<string, list<string>>
*/
public static function View(): array
{
Expand Down
13 changes: 11 additions & 2 deletions src/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@
* the LICENSE file that was distributed with this source code.
*/

/**
* @var CodeIgniter\Router\RouteCollection $routes
*/
$routes->group('oauth', ['namespace' => '\Datamweb\ShieldOAuth\Controllers'], static function ($routes): void {
$routes->addPlaceholder('allOAuthList', service('ShieldOAuth')->allOAuth());
/** @var \Datamweb\ShieldOAuth\Libraries\Basic\ShieldOAuth $shieldOAuthLib */
$shieldOAuthLib = service('ShieldOAuth');

$routes->addPlaceholder('allOAuthList', $shieldOAuthLib->allOAuth());
$routes->get('(:allOAuthList)', 'OAuthController::redirectOAuth/$1');

$routes->get(config('ShieldOAuthConfig')->call_back_route, 'OAuthController::callBack');
/** @var \Datamweb\ShieldOAuth\Config\ShieldOAuthConfig $config */
$config = config('ShieldOAuthConfig');

$routes->get($config->call_back_route, 'OAuthController::callBack');
});
12 changes: 10 additions & 2 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@

class Services extends BaseService
{
public static function ShieldOAuth($getShared = true)
/**
* -------------------------------------------------------
* Service Configuration file
* -------------------------------------------------------
* Create and share new class instances
*
* More Info @see https://codeigniter.com/user_guide/concepts/services.html
*/
public static function ShieldOAuth(bool $getShared = true): ShieldOAuth
{
if ($getShared) {
return static::getSharedInstance('ShieldOAuth');
}

return new ShieldOAuth(new ShieldOAuthConfig());
return new ShieldOAuth();
}
}
78 changes: 53 additions & 25 deletions src/Config/ShieldOAuthConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@

class ShieldOAuthConfig extends BaseConfig
{
/**
* --------------------------------------------------------------------------
* OAuth Configs
* --------------------------------------------------------------------------
*
* Set keys and active any OAuth
*
* Here you can set the keys received from any OAuth servers.
* for more information on getting keys:
*
* @see https://github.com/datamweb/shield-oauth/blob/develop/docs/get_keys.md
*
* @var array<string, array<string, bool|string>>
*/
public array $oauthConfigs = [
'github' => [
'client_id' => 'Get it from GitHub',
Expand All @@ -38,38 +52,52 @@ class ShieldOAuthConfig extends BaseConfig
// ],
];

/*
* If you use different names for the columns in the users table, use the following settings.
* Data of Table "users":
* +----+----------+--------+...+------------+-----------+--------+
* | id | username | status |...| first_name | last_name | avatar |
* +----+----------+--------+...+------------+-----------+--------+
* In fact, you set in which column the information received from the services should be recorded.
* For example, the first name received from OAuth should be recorded in column 'first_name' of the 'users' table shield.
*
* NOTE :
* This is suitable for those who have already installed the shield and created their own columns.
* In this case, there is no need to execute `php spark migrate -n Datamweb\ShieldOAuth`.
* Just set the following values with your table columns.
*/
/**
* --------------------------------------------------------------------------
* Users Columns Name
* --------------------------------------------------------------------------
* If you use different names for the columns in the users table, use the following settings.
*
* Data of Table "users":
* +----+----------+--------+...+------------+-----------+--------+
* | id | username | status |...| first_name | last_name | avatar |
* +----+----------+--------+...+------------+-----------+--------+
* In fact, you set in which column the information received from the OAuth services should be recorded.
* For example, the first name received from OAuth should be recorded in column 'first_name' of the 'users' table shield.
* NOTE :
* This is suitable for those who have already installed the shield and created their own columns.
* In this case, there is no need to execute `php spark migrate -n Datamweb\ShieldOAuth`.
* Just set the following values with your table columns.
*
* @var array<string, string>
*/
public array $usersColumnsName = [
'first_name' => 'first_name',
'last_name' => 'last_name',
'avatar' => 'avatar',
];

/*
* If the user is already registered, by default when trying to login, he/she information will be synchronized.
* if you want to cancel it, set false.
*/
/**
* --------------------------------------------------------------------------
* Syncing User Info
* --------------------------------------------------------------------------
* Turn ON/OFF user data update
*
* If the user is already registered, by default when trying to login, their
* information will be synchronized. If you want to cancel it, set to false.
*/
public bool $syncingUserInfo = true;

/*
* When the user login with his profile, the OAuth server directs him to the following path.
* So change this value only when you need to make an order.
* By default it returns to the following path:
* http:\\localhost:8080\oauth\call-back
*/
/**
* --------------------------------------------------------------------------
* Call Back Route
* --------------------------------------------------------------------------
* Set your custom call-back name
*
* When the user login with his profile, the OAuth server directs him to the following path.
* So change this value only when you need to customize it.
* By default, it returns to the following path:
* http://localhost:8080/oauth/call-back
*/
public string $call_back_route = 'call-back';
}
6 changes: 4 additions & 2 deletions src/Controllers/OAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
namespace Datamweb\ShieldOAuth\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Models\LoginModel;
use Datamweb\ShieldOAuth\Libraries\Basic\ControllersInterface;

class OAuthController extends BaseController implements ControllersInterface
{
public function redirectOAuth(string $oauthName)
public function redirectOAuth(string $oauthName): RedirectResponse
{
// if user login
if (auth()->loggedIn()) {
Expand All @@ -45,7 +47,7 @@ public function redirectOAuth(string $oauthName)
return redirect()->to($redirectLink);
}

public function callBack()
public function callBack(): RedirectResponse
{
// if user after callback request url
if (! session('oauth_name')) {
Expand Down
Loading

0 comments on commit c8a5db3

Please sign in to comment.