Skip to content

Commit

Permalink
Apply Rector
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Apr 27, 2022
1 parent 7b1f045 commit 739eeb9
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 91 deletions.
10 changes: 6 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@

// Are there files or rules you need to skip?
$parameters->set(Option::SKIP, [
__DIR__ . '/src/Config/Auth.php',
__DIR__ . '/src/Language',
__DIR__ . '/src/Views',

JsonThrowOnErrorRector::class,
Expand Down Expand Up @@ -123,8 +125,8 @@
$services->set(MakeInheritedMethodVisibilitySameAsParentRector::class);
$services->set(SimplifyEmptyArrayCheckRector::class);
$services->set(NormalizeNamespaceByPSR4ComposerAutoloadRector::class);
$services->set(TypedPropertyRector::class)
->configure([
TypedPropertyRector::INLINE_PUBLIC => true,
]);
// $services->set(TypedPropertyRector::class)
// ->configure([
// TypedPropertyRector::INLINE_PUBLIC => true,
// ]);
};
12 changes: 4 additions & 8 deletions src/AuthTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ public function restrictToGroups($groups, $uri = null)
{
$this->setupAuthClasses();

if ($this->authenticate->check()) {
if ($this->authorize->inGroup($groups, $this->authenticate->id())) {
return true;
}
if ($this->authenticate->check() && $this->authorize->inGroup($groups, $this->authenticate->id())) {
return true;
}

if (method_exists($this, 'setMessage')) {
Expand Down Expand Up @@ -129,10 +127,8 @@ public function restrictWithPermissions($permissions, $uri = null)
{
$this->setupAuthClasses();

if ($this->authenticate->check()) {
if ($this->authorize->hasPermission($permissions, $this->authenticate->id())) {
return true;
}
if ($this->authenticate->check() && $this->authorize->hasPermission($permissions, $this->authenticate->id())) {
return true;
}

if (method_exists($this, 'setMessage')) {
Expand Down
4 changes: 2 additions & 2 deletions src/Authentication/AuthenticationBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function login(?User $user = null, bool $remember = false): bool
// We'll give a 20% chance to need to do a purge since we
// don't need to purge THAT often, it's just a maintenance issue.
// to keep the table from getting out of control.
if (mt_rand(1, 100) < 20) {
if (random_int(1, 100) < 20) {
$this->loginModel->purgeOldRememberTokens();
}

Expand Down Expand Up @@ -159,7 +159,7 @@ public function logout()
// Destroy the session data - but ensure a session is still
// available for flash messages, etc.
if (isset($_SESSION)) {
foreach ($_SESSION as $key => $value) {
foreach (array_keys($_SESSION) as $key) {
$_SESSION[$key] = null;
unset($_SESSION[$key]);
}
Expand Down
9 changes: 4 additions & 5 deletions src/Authentication/Passwords/NothingPersonalValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ protected function isNotPersonal($password, $user)

// Get any other "personal" fields defined in config
$personalFields = $this->config->personalFields;
if (! empty($personalFields)) {
foreach ($personalFields as $value) {
if (! empty($user->{$value})) {
$needles[] = \strtolower($user->{$value});
}

foreach ($personalFields as $value) {
if (! empty($user->{$value})) {
$needles[] = \strtolower($user->{$value});
}
}

Expand Down
7 changes: 1 addition & 6 deletions src/Authentication/Passwords/PwnedValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ public function check(string $password, ?Entity $user = null): bool

$startPos += 36; // right after the delimiter (:)
$endPos = strpos($range, "\r\n", $startPos);
if ($endPos !== false) {
$hits = (int) substr($range, $startPos, $endPos - $startPos);
} else {
// match is the last item in the range which does not end with "\r\n"
$hits = (int) substr($range, $startPos);
}
$hits = $endPos !== false ? (int) substr($range, $startPos, $endPos - $startPos) : (int) substr($range, $startPos);

$wording = $hits > 1 ? lang('Auth.errorPasswordPwnedDatabases') : lang('Auth.errorPasswordPwnedDatabase');
$this->error = lang('Auth.errorPasswordPwned', [$password, $hits, $wording]);
Expand Down
8 changes: 4 additions & 4 deletions src/Authorization/FlatAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ public function hasPermission($permission, int $userId)
}

// First check the permission model. If that exists, then we're golden.
if ($this->permissionModel->doesUserHavePermission($userId, (int) $permissionId)) {
if ($this->permissionModel->doesUserHavePermission($userId, $permissionId)) {
return true;
}

// Still here? Then we have one last check to make - any user private permissions.
return $this->doesUserHavePermission($userId, (int) $permissionId);
return $this->doesUserHavePermission($userId, $permissionId);
}

/**
Expand Down Expand Up @@ -191,7 +191,7 @@ public function addUserToGroup(int $userid, $group)
return null;
}

if (! $this->groupModel->addUserToGroup($userid, (int) $groupId)) {
if (! $this->groupModel->addUserToGroup($userid, $groupId)) {
$this->error = $this->groupModel->errors();

return false;
Expand Down Expand Up @@ -629,7 +629,7 @@ public function updatePermission(int $id, string $name, string $description = ''
$data['description'] = $description;
}

if (! $this->permissionModel->update((int) $id, $data)) {
if (! $this->permissionModel->update($id, $data)) {
$this->error = $this->permissionModel->errors();

return false;
Expand Down
10 changes: 5 additions & 5 deletions src/Authorization/GroupModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function addUserToGroup(int $userId, int $groupId)
cache()->delete("{$userId}_permissions");

$data = [
'user_id' => (int) $userId,
'group_id' => (int) $groupId,
'user_id' => $userId,
'group_id' => $groupId,
];

return (bool) $this->db->table('auth_groups_users')->insert($data);
Expand Down Expand Up @@ -74,7 +74,7 @@ public function removeUserFromAllGroups(int $userId)
cache()->delete("{$userId}_permissions");

return $this->db->table('auth_groups_users')
->where('user_id', (int) $userId)
->where('user_id', $userId)
->delete();
}

Expand Down Expand Up @@ -158,8 +158,8 @@ public function getPermissionsForGroup(int $groupId): array
public function addPermissionToGroup(int $permissionId, int $groupId)
{
$data = [
'permission_id' => (int) $permissionId,
'group_id' => (int) $groupId,
'permission_id' => $permissionId,
'group_id' => $groupId,
];

return $this->db->table('auth_groups_permissions')->insert($data);
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Routes.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Myth\Auth\Config;

// Myth:Auth routes file.
$routes->group('', ['namespace' => 'Myth\Auth\Controllers'], static function ($routes) {
// Login/out
Expand Down
7 changes: 2 additions & 5 deletions tests/authorization/FlatAuthorizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ final class FlatAuthorizationTest extends AuthTestCase
*/
protected $permissions;

/**
* @var FlatAuthorization
*/
protected $auth;
protected FlatAuthorization $auth;

protected function setUp(): void
{
Expand Down Expand Up @@ -548,7 +545,7 @@ public function testGroupPermissions()

$found = $this->auth->groupPermissions($group->id);

$this->assertTrue(isset($found[$perm->id]));
$this->assertArrayHasKey($perm->id, $found);
$this->assertSame((array) $perm, $found[$perm->id]);
}
}
25 changes: 15 additions & 10 deletions tests/controllers/RegisterTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php

use CodeIgniter\Config\Factories;
use CodeIgniter\Test\ControllerTestTrait;
use Config\Services;
use Config\Validation;
use Myth\Auth\Authentication\Passwords\ValidationRules;
use Myth\Auth\Config\Auth;
use Myth\Auth\Controllers\AuthController;
use Myth\Auth\Models\UserModel;
use Tests\Support\AuthTestCase;

/**
Expand All @@ -21,10 +26,10 @@ protected function setUp(): void
parent::setUp();

// Make sure our valiation rules include strong_password
$vConfig = new \Config\Validation();
$vConfig->ruleSets[] = \Myth\Auth\Authentication\Passwords\ValidationRules::class;
$vConfig = new Validation();
$vConfig->ruleSets[] = ValidationRules::class;
$vConfig->ruleSets = array_reverse($vConfig->ruleSets);
\CodeIgniter\Config\Factories::injectMock('Config', 'Validation', $vConfig);
Factories::injectMock('Config', 'Validation', $vConfig);

// Make sure our routes are mapped
$routes = service('routes');
Expand All @@ -47,9 +52,9 @@ public function testRegisterDisplaysForm()

public function testAttemptRegisterDisabled()
{
$config = new \Myth\Auth\Config\Auth();
$config = new Auth();
$config->allowRegistration = false;
\CodeIgniter\Config\Factories::injectMock('Config', 'Auth', $config);
Factories::injectMock('Config', 'Auth', $config);

$result = $this->withUri(site_url('register'))
->controller(AuthController::class)
Expand All @@ -61,9 +66,9 @@ public function testAttemptRegisterDisabled()

public function testAttemptRegisterValidationErrors()
{
$config = new \Myth\Auth\Config\Auth();
$config = new Auth();
$config->allowRegistration = true;
\CodeIgniter\Config\Factories::injectMock('Config', 'Auth', $config);
Factories::injectMock('Config', 'Auth', $config);

$result = $this->withUri(site_url('register'))
->controller(AuthController::class)
Expand Down Expand Up @@ -93,7 +98,7 @@ public function testAttemptRegisterCreatesUser()
// don't require activation for this...
$config = config('Auth');
$config->requireActivation = null;
\CodeIgniter\Config\Factories::injectMock('Config', 'Auth', $config);
Factories::injectMock('Config', 'Auth', $config);

$result = $this->withUri(site_url('register'))
->withRequest($request)
Expand Down Expand Up @@ -132,7 +137,7 @@ public function testAttemptRegisterCreatesUserWithDefaultGroup()
$config = config('Auth');
$config->requireActivation = null;
$config->defaultUserGroup = $group->name;
\CodeIgniter\Config\Factories::injectMock('Config', 'Auth', $config);
Factories::injectMock('Config', 'Auth', $config);

$result = $this->withUri(site_url('register'))
->withRequest($request)
Expand All @@ -147,7 +152,7 @@ public function testAttemptRegisterCreatesUserWithDefaultGroup()
'email' => $data['email'],
]);

$users = new \Myth\Auth\Models\UserModel();
$users = new UserModel();
$user = $users->where('username', $data['username'])->first();
$this->seeInDatabase('auth_groups_users', [
'user_id' => $user->id,
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/AuthTestTraitTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Myth\Auth\Test\AuthTestTrait;
use Myth\Auth\Test\Fakers\UserFaker;
use Tests\Support\AuthTestCase;

Expand All @@ -8,7 +9,7 @@
*/
final class AuthTestTraitTest extends AuthTestCase
{
use \Myth\Auth\Test\AuthTestTrait;
use AuthTestTrait;

public function testResetServicesResetsAuthentication()
{
Expand Down
15 changes: 5 additions & 10 deletions tests/unit/AuthenticationBaseLoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,26 @@

use CodeIgniter\Test\CIUnitTestCase;
use Mockery as m;
use Mockery\MockInterface;
use Myth\Auth\Authentication\AuthenticationBase;
use Myth\Auth\Config\Auth;
use Myth\Auth\Models\LoginModel;

/**
* @internal
*/
final class AuthenticationBaseLoginTest extends CIUnitTestCase
{
/**
* @var AuthenticationBase
*/
protected $auth;

/**
* @var Mockery\MockInterface
*/
protected $loginModel;
protected AuthenticationBase $auth;
protected MockInterface $loginModel;

protected function setUp(): void
{
parent::setUp();

$this->loginModel = m::mock(LoginModel::class);

$this->auth = new AuthenticationBase(new \Myth\Auth\Config\Auth());
$this->auth = new AuthenticationBase(new Auth());
$this->auth->setLoginModel($this->loginModel);
}

Expand Down
8 changes: 3 additions & 5 deletions tests/unit/CompositionValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

use CodeIgniter\Test\CIUnitTestCase;
use Myth\Auth\Authentication\Passwords\CompositionValidator;
use Myth\Auth\Config\Auth;

/**
* @internal
*/
final class CompositionValidatorTest extends CIUnitTestCase
{
/**
* @var CompositionValidator
*/
protected $validator;
protected CompositionValidator $validator;

protected function setUp(): void
{
parent::setUp();

$config = new \Myth\Auth\Config\Auth();
$config = new Auth();
$config->minimumPasswordLength = 8;

$this->validator = new CompositionValidator();
Expand Down
8 changes: 3 additions & 5 deletions tests/unit/DictionaryValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

use CodeIgniter\Test\CIUnitTestCase;
use Myth\Auth\Authentication\Passwords\DictionaryValidator;
use Myth\Auth\Config\Auth;

/**
* @internal
*/
final class DictionaryValidatorTest extends CIUnitTestCase
{
/**
* @var DictionaryValidator
*/
protected $validator;
protected DictionaryValidator $validator;

protected function setUp(): void
{
parent::setUp();

$config = new \Myth\Auth\Config\Auth();
$config = new Auth();

$this->validator = new DictionaryValidator();
$this->validator->setConfig($config);
Expand Down
Loading

0 comments on commit 739eeb9

Please sign in to comment.