Skip to content

Commit

Permalink
Merge pull request #47 from kenjis/chore-add-rector
Browse files Browse the repository at this point in the history
refactor: add Rector and refactor
  • Loading branch information
datamweb authored Aug 24, 2023
2 parents 8358af6 + 46b6e5a commit 4c687d7
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 77 deletions.
5 changes: 4 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
// __DIR__ . '/tests/',
])
->exclude('build')
->append([__FILE__]);
->append([
__FILE__,
__DIR__ . '/rector.php',
]);

$overrides = [
'declare_strict_types' => true,
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"require-dev": {
"codeigniter4/devkit": "^1.0",
"codeigniter4/framework": "^4.2.7",
"codeigniter4/shield": "dev-develop"
"codeigniter4/shield": "dev-develop",
"rector/rector": "^0.18.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -69,4 +70,4 @@
"issues": "https://github.com/datamweb/shield-oauth/issues",
"docs": "https://github.com/datamweb/shield-oauth/blob/develop/docs/index.md"
}
}
}
50 changes: 0 additions & 50 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
'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,
Expand All @@ -47,11 +42,6 @@
'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,
Expand Down Expand Up @@ -93,26 +83,6 @@
'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,
Expand Down Expand Up @@ -149,26 +119,6 @@
'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,
Expand Down
134 changes: 134 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

declare(strict_types=1);

/**
* This file is part of Shield OAuth.
*
* (c) Datamweb <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector;
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector;
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
use Rector\CodeQuality\Rector\If_\CombineIfRector;
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector;
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
SetList::DEAD_CODE,
LevelSetList::UP_TO_PHP_74,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
PHPUnitSetList::PHPUNIT_100,
]);

$rectorConfig->parallel();

// The paths to refactor (can also be supplied with CLI arguments)
$rectorConfig->paths([
__DIR__ . '/src/',
// __DIR__ . '/tests/',
]);

// Include Composer's autoload - required for global execution, remove if running locally
$rectorConfig->autoloadPaths([
__DIR__ . '/vendor/autoload.php',
]);

// Do you need to include constants, class aliases, or a custom autoloader?
$rectorConfig->bootstrapFiles([
realpath(getcwd()) . '/vendor/codeigniter4/framework/system/Test/bootstrap.php',
]);

if (is_file(__DIR__ . '/phpstan.neon.dist')) {
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon.dist');
}

// Set the target version for refactoring
$rectorConfig->phpVersion(PhpVersion::PHP_74);

// Auto-import fully qualified class names
$rectorConfig->importNames();

// Are there files or rules you need to skip?
$rectorConfig->skip([
__DIR__ . '/app/Views',

JsonThrowOnErrorRector::class,
StringifyStrNeedlesRector::class,

// Note: requires php 8
RemoveUnusedPromotedPropertyRector::class,

// May load view files directly when detecting classes
StringClassNameToClassConstantRector::class,

TypedPropertyFromAssignsRector::class => [
__DIR__ . '/src/Commands/OAuthSetup.php',
__DIR__ . '/src/Commands/Generators/NewShieldOauthGenerator.php',
],
]);

// auto import fully qualified class names
$rectorConfig->importNames();

$rectorConfig->rule(SimplifyUselessVariableRector::class);
$rectorConfig->rule(RemoveAlwaysElseRector::class);
$rectorConfig->rule(CountArrayToEmptyArrayComparisonRector::class);
$rectorConfig->rule(ChangeNestedForeachIfsToEarlyContinueRector::class);
$rectorConfig->rule(ChangeIfElseValueAssignToEarlyReturnRector::class);
$rectorConfig->rule(SimplifyStrposLowerRector::class);
$rectorConfig->rule(CombineIfRector::class);
$rectorConfig->rule(SimplifyIfReturnBoolRector::class);
$rectorConfig->rule(InlineIfToExplicitIfRector::class);
$rectorConfig->rule(PreparedValueToEarlyReturnRector::class);
$rectorConfig->rule(ShortenElseIfRector::class);
$rectorConfig->rule(SimplifyIfElseToTernaryRector::class);
$rectorConfig->rule(UnusedForeachValueToArrayKeysRector::class);
$rectorConfig->rule(ChangeArrayPushToArrayAssignRector::class);
$rectorConfig->rule(UnnecessaryTernaryExpressionRector::class);
$rectorConfig->rule(SimplifyRegexPatternRector::class);
$rectorConfig->rule(FuncGetArgsToVariadicParamRector::class);
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
$rectorConfig
->ruleWithConfiguration(TypedPropertyFromAssignsRector::class, [
/**
* The INLINE_PUBLIC value is default to false to avoid BC break, if you use for libraries and want to preserve BC break, you don't need to configure it, as it included in LevelSetList::UP_TO_PHP_74
* Set to true for projects that allow BC break
*/
TypedPropertyFromAssignsRector::INLINE_PUBLIC => true,
]);
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
};
4 changes: 1 addition & 3 deletions src/Commands/Generators/NewShieldOauthGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ class NewShieldOauthGenerator extends BaseCommand

/**
* Path to the app folder
*
* @var string
*/
protected $distPath = APPPATH;
protected string $distPath = APPPATH;

/**
* The ContentReplacer class
Expand Down
10 changes: 7 additions & 3 deletions src/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
* the LICENSE file that was distributed with this source code.
*/

use CodeIgniter\Router\RouteCollection;
use Datamweb\ShieldOAuth\Config\ShieldOAuthConfig;
use Datamweb\ShieldOAuth\Libraries\Basic\ShieldOAuth;

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

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

/** @var \Datamweb\ShieldOAuth\Config\ShieldOAuthConfig $config */
/** @var ShieldOAuthConfig $config */
$config = config('ShieldOAuthConfig');

$routes->get($config->call_back_route, 'OAuthController::callBack');
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/OAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private function makeAntiForgeryKey(): string

private function checkAntiForgery(string $state): bool
{
return (bool) ($state === session()->get('state'));
return $state === session()->get('state');
}

private function checkExistenceUser(array $find = []): bool
Expand All @@ -127,7 +127,7 @@ private function checkExistenceUser(array $find = []): bool
// $find = ['email' => $this->userInfo()->email];
$findUser = $users->findByCredentials($find);

return (bool) ($findUser !== null);
return $findUser !== null;
}

private function syncingUserInfo(array $find = [], array $updateFildes = []): int
Expand Down
2 changes: 2 additions & 0 deletions src/Libraries/Basic/AbstractOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

abstract class AbstractOAuth
{
protected string $token;

abstract protected function makeGoLink(string $state): string;

abstract protected function fetchAccessTokenWithAuthCode(array $allGet): void;
Expand Down
6 changes: 2 additions & 4 deletions src/Libraries/Basic/ShieldOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function makeOAuthButton(string $forPage = 'login'): string

$Button .= '<a href=' . base_url('oauth/google') . " class='btn btn-outline-secondary' aria-current='page'>" . lang('ShieldOAuthLang.Google.google') . '</a>';
$Button .= '<a href=' . base_url('oauth/github') . " class='btn btn-outline-secondary' aria-current='page'>" . lang('ShieldOAuthLang.Github.github') . '</a>';
if (count($this->otherOAuth()) > 0) {
if ($this->otherOAuth() !== []) {
$Button .= "<div class='btn-group' role='group'>
<button type='button' class='btn btn-outline-secondary dropdown-toggle' data-bs-toggle='dropdown' aria-expanded='false'>"
. lang('ShieldOAuthLang.other') . "
Expand All @@ -141,10 +141,8 @@ public function makeOAuthButton(string $forPage = 'login'): string
$Button .= '</ul></div>';
}

$Button .= '
return $Button . '
</div>
</div>';

return $Button;
}
}
13 changes: 7 additions & 6 deletions src/Libraries/GithubOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@

namespace Datamweb\ShieldOAuth\Libraries;

use Config\Services;
use Datamweb\ShieldOAuth\Libraries\Basic\AbstractOAuth;
use Exception;

class GithubOAuth extends AbstractOAuth
{
public static $API_CODE_URL = 'https://github.com/login/oauth/authorize';
public static $API_TOKEN_URL = 'https://github.com/login/oauth/access_token';
public static $API_USER_INFO_URL = 'https://api.github.com/user';
private static $APPLICATION_NAME = 'ShieldOAuth';
public static string $API_CODE_URL = 'https://github.com/login/oauth/authorize';
public static string $API_TOKEN_URL = 'https://github.com/login/oauth/access_token';
public static string $API_USER_INFO_URL = 'https://api.github.com/user';
private static string $APPLICATION_NAME = 'ShieldOAuth';
protected string $token;
protected $client;
protected $config;
Expand All @@ -32,15 +33,15 @@ class GithubOAuth extends AbstractOAuth
public function __construct(string $token = '')
{
$this->token = $token;
$this->client = \Config\Services::curlrequest();
$this->client = Services::curlrequest();

$this->config = config('ShieldOAuthConfig');
$this->callback_url = base_url('oauth/' . $this->config->call_back_route);
$this->client_id = $this->config->oauthConfigs['github']['client_id'];
$this->client_secret = $this->config->oauthConfigs['github']['client_secret'];
}

public function makeGoLink(string $state): string
protected function makeGoLink(string $state): string
{
return $redirectUrl = self::$API_CODE_URL . "?client_id={$this->client_id}&redirect_uri={$this->callback_url}&scope=user%3Aemail&response_type=code&state={$state}";
}
Expand Down
Loading

0 comments on commit 4c687d7

Please sign in to comment.