Skip to content

Commit 1f4b5fb

Browse files
authored
Apply conservative Rector cleanup (#170)
* Apply conservative Rector cleanup * Run PHPCS cleanup * Remove unused startup event * Validate role column config strictly * Restore !empty ternaries in tinyauth
1 parent 609b2af commit 1f4b5fb

11 files changed

Lines changed: 20 additions & 62 deletions

File tree

src/Auth/AclAdapter/AbstractAclAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getAcl(array $availableRoles, array $config): array {
7979
}
8080

8181
foreach ($roles as $role) {
82-
$role = trim($role);
82+
$role = trim((string)$role);
8383
if (!$role) {
8484
continue;
8585
}

src/Auth/AclTrait.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,7 @@ protected function _prefixMap(array $roles): array {
255255
return [];
256256
}
257257

258-
if ($prefixMap === true) {
259-
$prefixMap = $this->_prefixesFromRoles($roles);
260-
} else {
261-
$prefixMap = $this->_normalizePrefixes($prefixMap);
262-
}
258+
$prefixMap = $prefixMap === true ? $this->_prefixesFromRoles($roles) : $this->_normalizePrefixes($prefixMap);
263259

264260
$this->_prefixMap = $prefixMap;
265261

@@ -556,7 +552,7 @@ protected function _getUserRoles(ArrayAccess|array $user) {
556552
// Single-role from session
557553
if (!$this->getConfig('multiRole')) {
558554
$roleColumn = $this->getConfig('roleColumn');
559-
if (!$roleColumn) {
555+
if (!is_string($roleColumn) || $roleColumn === '') {
560556
throw new CakeException('Invalid TinyAuth config, `roleColumn` config missing.');
561557
}
562558

src/Auth/AllowTrait.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ protected function _isActionAllowed(array $rule, $action) {
9797
return false;
9898
}
9999

100-
if (!in_array($action, $rule['allow'], true) && !in_array('*', $rule['allow'], true)) {
101-
return false;
102-
}
103-
104-
return true;
100+
return in_array($action, $rule['allow'], true) || in_array('*', $rule['allow'], true);
105101
}
106102

107103
/**

src/Auth/AuthUserTrait.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ public function roles(): array {
9696
return [];
9797
}
9898

99-
$roles = $this->_getUserRoles($user);
100-
101-
return $roles;
99+
return $this->_getUserRoles($user);
102100
}
103101

104102
/**
@@ -117,11 +115,7 @@ public function roles(): array {
117115
* @return bool Success
118116
*/
119117
public function hasRole($expectedRole, $providedRoles = null) {
120-
if ($providedRoles !== null) {
121-
$roles = (array)$providedRoles;
122-
} else {
123-
$roles = $this->roles();
124-
}
118+
$roles = $providedRoles !== null ? (array)$providedRoles : $this->roles();
125119

126120
if (!$roles) {
127121
return false;
@@ -161,11 +155,7 @@ public function hasRole($expectedRole, $providedRoles = null) {
161155
* @return bool Success
162156
*/
163157
public function hasRoles($expectedRoles, $oneRoleIsEnough = true, $providedRoles = null): bool {
164-
if ($providedRoles !== null) {
165-
$roles = $providedRoles;
166-
} else {
167-
$roles = $this->roles();
168-
}
158+
$roles = $providedRoles ?? $this->roles();
169159

170160
$expectedRoles = (array)$expectedRoles;
171161
if (!$expectedRoles) {
@@ -179,18 +169,12 @@ public function hasRoles($expectedRoles, $oneRoleIsEnough = true, $providedRoles
179169
return true;
180170
}
181171
$count++;
182-
} else {
183-
if (!$oneRoleIsEnough) {
184-
return false;
185-
}
172+
} elseif (!$oneRoleIsEnough) {
173+
return false;
186174
}
187175
}
188176

189-
if ($count === count($expectedRoles)) {
190-
return true;
191-
}
192-
193-
return false;
177+
return $count === count($expectedRoles);
194178
}
195179

196180
/**

src/Panel/AuthPanel.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ public function shutdown(EventInterface $event): void {
9292

9393
$access = [];
9494
foreach ($availableRoles as $role => $id) {
95-
if ($user) {
96-
$tmpUser = $this->_injectRole($user, $role, $id);
97-
} else {
98-
$tmpUser = $this->_generateUser($role, $id);
99-
}
95+
$tmpUser = $user ? $this->_injectRole($user, $role, $id) : $this->_generateUser($role, $id);
10096
$access[$role] = $this->_checkUser($tmpUser, $params);
10197
}
10298

src/Sync/Adder.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function addAcl(string $controller, string $action, array $roles, Argumen
6666

6767
if (isset($content[$controller][$action]) || isset($content[$controller]['*'])) {
6868
$mappedRoles = $content[$controller][$action] ?? $content[$controller]['*'];
69-
if (strpos($mappedRoles, ',') !== false) {
69+
if (str_contains($mappedRoles, ',')) {
7070
$mappedRoles = array_map('trim', explode(',', $mappedRoles));
7171
}
7272
$this->checkRoles($roles, (array)$mappedRoles, $io);
@@ -203,7 +203,7 @@ protected function _listDirectory(string $folder): array {
203203
* @return bool
204204
*/
205205
protected function _noAuthenticationNeeded($name, $plugin, $prefix) {
206-
if (!isset($this->authAllow)) {
206+
if ($this->authAllow === null) {
207207
$this->authAllow = $this->_parseAuthAllow();
208208
}
209209

@@ -212,12 +212,8 @@ protected function _noAuthenticationNeeded($name, $plugin, $prefix) {
212212
return false;
213213
}
214214

215-
if ($this->authAllow[$key] === '*') {
216-
return true;
217-
}
218-
219215
//TODO: specific actions?
220-
return false;
216+
return $this->authAllow[$key] === '*';
221217
}
222218

223219
/**

src/Sync/Syncer.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected function _listDirectory(string $folder): array {
195195
* @return bool
196196
*/
197197
protected function _noAuthenticationNeeded($name, $plugin, $prefix) {
198-
if (!isset($this->authAllow)) {
198+
if ($this->authAllow === null) {
199199
$this->authAllow = $this->_parseAuthAllow();
200200
}
201201

@@ -204,12 +204,8 @@ protected function _noAuthenticationNeeded($name, $plugin, $prefix) {
204204
return false;
205205
}
206206

207-
if ($this->authAllow[$key] === '*') {
208-
return true;
209-
}
210-
211207
//TODO: specific actions?
212-
return false;
208+
return $this->authAllow[$key] === '*';
213209
}
214210

215211
/**

src/Utility/TinyAuth.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ public function __construct(array $config = []) {
3030
* @return array<string, int|string>
3131
*/
3232
public function getAvailableRoles() {
33-
$roles = $this->_getAvailableRoles();
34-
35-
return $roles;
33+
return $this->_getAvailableRoles();
3634
}
3735

3836
}

tests/TestCase/Controller/Component/AuthUserComponentTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public function can(?IdentityInterface $user, string $action, mixed $resource, .
365365
* @return \Authorization\Policy\ResultInterface
366366
*/
367367
public function canResult(?IdentityInterface $user, string $action, mixed $resource, ...$optionalArgs): ResultInterface {
368-
$x = new class implements ResultInterface {
368+
return new class implements ResultInterface {
369369
/**
370370
* @return bool
371371
*/
@@ -380,8 +380,6 @@ public function getReason(): ?string {
380380
return null;
381381
}
382382
};
383-
384-
return $x;
385383
}
386384

387385
/**

tests/TestCase/Controller/Component/AuthenticationComponentTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Cake\Controller\ComponentRegistry;
66
use Cake\Controller\Controller;
77
use Cake\Core\Plugin;
8-
use Cake\Event\Event;
98
use Cake\Http\ServerRequest;
109
use Cake\TestSuite\TestCase;
1110
use TinyAuth\Controller\Component\AuthenticationComponent;
@@ -52,8 +51,7 @@ public function testValid() {
5251
$config = [];
5352
$this->component->initialize($config);
5453

55-
$event = new Event('Controller.startup', $controller);
56-
$response = $this->component->startup($event);
54+
$response = $this->component->startup();
5755
$this->assertNull($response);
5856
}
5957

0 commit comments

Comments
 (0)