Skip to content

Commit 98967ee

Browse files
committed
add rector
1 parent 6540b9a commit 98967ee

37 files changed

+276
-279
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
"stan": "@phpstan",
7474
"stan-baseline": "tools/phpstan --generate-baseline",
7575
"stan-setup": "phive install",
76+
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.3.1\" && mv composer.backup composer.json",
77+
"rector-check": "vendor/bin/rector process --dry-run",
78+
"rector-fix": "vendor/bin/rector process",
7679
"test": "phpunit",
7780
"test-coverage": "phpunit --coverage-clover=clover.xml"
7881
},

rector.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
5+
use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector;
6+
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
7+
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
8+
use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
9+
use Rector\CodingStyle\Rector\Stmt\NewlineAfterStatementRector;
10+
use Rector\Config\RectorConfig;
11+
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
12+
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
13+
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
14+
use Rector\Set\ValueObject\SetList;
15+
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromCreateMockAssignRector;
16+
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictFluentReturnRector;
17+
18+
$cacheDir = getenv('RECTOR_CACHE_DIR') ?: sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'rector';
19+
20+
return RectorConfig::configure()
21+
->withPaths([
22+
__DIR__ . '/src',
23+
__DIR__ . '/tests',
24+
])
25+
26+
->withCache(
27+
cacheClass: FileCacheStorage::class,
28+
cacheDirectory: $cacheDir,
29+
)
30+
31+
->withPhpSets()
32+
->withAttributesSets()
33+
34+
->withSets([
35+
SetList::CODE_QUALITY,
36+
SetList::CODING_STYLE,
37+
SetList::DEAD_CODE,
38+
SetList::EARLY_RETURN,
39+
SetList::INSTANCEOF,
40+
SetList::TYPE_DECLARATION,
41+
])
42+
43+
->withSkip([
44+
__DIR__ . '/tests/comparisons',
45+
ClassPropertyAssignToConstructorPromotionRector::class,
46+
CatchExceptionNameMatchingTypeRector::class,
47+
ClosureToArrowFunctionRector::class,
48+
RemoveUselessReturnTagRector::class,
49+
CompactToVariablesRector::class,
50+
ReturnTypeFromStrictFluentReturnRector::class,
51+
SplitDoubleAssignRector::class,
52+
NewlineAfterStatementRector::class,
53+
ExplicitBoolCompareRector::class,
54+
TypedPropertyFromCreateMockAssignRector::class,
55+
]);

src/AuthorizationPlugin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace Authorization;
1818

1919
use Authorization\Command\PolicyCommand;
20+
use Bake\Command\SimpleBakeCommand;
2021
use Cake\Console\CommandCollection;
2122
use Cake\Core\BasePlugin;
2223

@@ -33,7 +34,7 @@ class AuthorizationPlugin extends BasePlugin
3334
*/
3435
public function console(CommandCollection $commands): CommandCollection
3536
{
36-
if (class_exists('Bake\Command\SimpleBakeCommand')) {
37+
if (class_exists(SimpleBakeCommand::class)) {
3738
$commands->add('bake policy', PolicyCommand::class);
3839
}
3940

src/AuthorizationService.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@ class AuthorizationService implements AuthorizationServiceInterface
2929
{
3030
/**
3131
* Authorization policy resolver.
32-
*
33-
* @var \Authorization\Policy\ResolverInterface
3432
*/
3533
protected ResolverInterface $resolver;
3634

3735
/**
3836
* Track whether authorization was checked.
39-
*
40-
* @var bool
4137
*/
4238
protected bool $authorizationChecked = false;
4339

@@ -144,7 +140,7 @@ protected function getCanHandler(mixed $policy, string $action): Closure
144140

145141
assert(
146142
method_exists($policy, $method) || method_exists($policy, '__call'),
147-
new MissingMethodException([$method, $action, get_class($policy)]),
143+
new MissingMethodException([$method, $action, $policy::class]),
148144
);
149145

150146
/** @phpstan-ignore callable.nonCallable */
@@ -165,7 +161,7 @@ protected function getScopeHandler(mixed $policy, string $action): Closure
165161

166162
assert(
167163
method_exists($policy, $method) || method_exists($policy, '__call'),
168-
new MissingMethodException([$method, $action, get_class($policy)]),
164+
new MissingMethodException([$method, $action, $policy::class]),
169165
);
170166

171167
/** @phpstan-ignore callable.nonCallable */

src/Command/PolicyCommand.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
namespace Authorization\Command;
1818

19+
use Authorization\IdentityInterface;
1920
use Bake\Command\SimpleBakeCommand;
2021
use Cake\Console\Arguments;
2122
use Cake\Console\ConsoleIo;
2223
use Cake\Console\ConsoleOptionParser;
24+
use Cake\ORM\Query\SelectQuery;
2325
use Cake\Utility\Inflector;
2426
use RuntimeException;
2527

@@ -30,14 +32,9 @@ class PolicyCommand extends SimpleBakeCommand
3032
{
3133
/**
3234
* Path to Policy directory
33-
*
34-
* @var string
3535
*/
3636
public string $pathFragment = 'Policy/';
3737

38-
/**
39-
* @var string
40-
*/
4138
protected string $type;
4239

4340
/**
@@ -91,13 +88,13 @@ public function templateData(Arguments $arguments): array
9188
$imports = [];
9289
$className = $data['namespace'] . '\\' . $name;
9390
if ($type === 'table') {
94-
$className = "{$data['namespace']}\Model\\Table\\{$name}{$suffix}";
95-
$imports[] = 'Cake\ORM\Query\SelectQuery';
91+
$className = sprintf('%s\Model\Table\%s%s', $data['namespace'], $name, $suffix);
92+
$imports[] = SelectQuery::class;
9693
} elseif ($type === 'entity') {
97-
$className = "{$data['namespace']}\Model\\Entity\\{$name}";
94+
$className = sprintf('%s\Model\Entity\%s', $data['namespace'], $name);
9895
$imports[] = $className;
9996
}
100-
$imports[] = 'Authorization\\IdentityInterface';
97+
$imports[] = IdentityInterface::class;
10198

10299
$variable = Inflector::variable($name);
103100
if ($variable === 'user') {

src/Controller/Component/AuthorizationComponent.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function authorize(mixed $resource, ?string $action = null): void
7373
}
7474

7575
if (is_object($resource)) {
76-
$name = get_class($resource);
76+
$name = $resource::class;
7777
} elseif (is_string($resource)) {
7878
$name = $resource;
7979
} else {
@@ -131,7 +131,7 @@ protected function performCheck(
131131
}
132132

133133
$identity = $this->getIdentity($request);
134-
if ($identity === null) {
134+
if (!$identity instanceof IdentityInterface) {
135135
return $this->getService($request)->{$method}(null, $action, $resource);
136136
}
137137

@@ -156,7 +156,7 @@ public function applyScope(mixed $resource, ?string $action = null, mixed ...$op
156156
$action = $this->getDefaultAction($request);
157157
}
158158
$identity = $this->getIdentity($request);
159-
if ($identity === null) {
159+
if (!$identity instanceof IdentityInterface) {
160160
return $this->getService($request)->applyScope(null, $action, $resource);
161161
}
162162

@@ -233,7 +233,7 @@ protected function getService(ServerRequestInterface $request): AuthorizationSer
233233
$serviceAttribute = $this->getConfig('serviceAttribute');
234234
$service = $request->getAttribute($serviceAttribute);
235235
if (!$service instanceof AuthorizationServiceInterface) {
236-
$type = is_object($service) ? get_class($service) : gettype($service);
236+
$type = get_debug_type($service);
237237
throw new InvalidArgumentException(sprintf(
238238
'Expected that `%s` would be an instance of %s, but got %s',
239239
$serviceAttribute,
@@ -261,7 +261,7 @@ protected function getIdentity(ServerRequestInterface $request): ?IdentityInterf
261261
return $identity;
262262
}
263263
if (!$identity instanceof IdentityInterface) {
264-
$type = is_object($identity) ? get_class($identity) : gettype($identity);
264+
$type = get_debug_type($identity);
265265
throw new InvalidArgumentException(sprintf(
266266
'Expected that `%s` would be an instance of %s, but got %s',
267267
$identityAttribute,
@@ -328,7 +328,7 @@ protected function getDefaultAction(ServerRequest $request): string
328328
return $action;
329329
}
330330
if (!is_string($name)) {
331-
$type = is_object($name) ? get_class($name) : gettype($name);
331+
$type = get_debug_type($name);
332332
$message = sprintf('Invalid action type for `%s`. Expected `string` or `null`, got `%s`.', $action, $type);
333333
throw new UnexpectedValueException($message);
334334
}

src/Exception/ForbiddenException.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ class ForbiddenException extends Exception
3333

3434
/**
3535
* Policy check result.
36-
*
37-
* @var \Authorization\Policy\ResultInterface|null
3836
*/
3937
protected ?ResultInterface $result = null;
4038

src/IdentityDecorator.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ class IdentityDecorator implements IdentityInterface
3939

4040
/**
4141
* Authorization Service
42-
*
43-
* @var \Authorization\AuthorizationServiceInterface
4442
*/
4543
protected AuthorizationServiceInterface $authorization;
4644

@@ -105,7 +103,7 @@ public function getOriginalData(): ArrayAccess|array
105103
public function __call(string $method, array $args): mixed
106104
{
107105
if (!is_object($this->identity)) {
108-
throw new BadMethodCallException("Cannot call `{$method}`. Identity data is not an object.");
106+
throw new BadMethodCallException(sprintf('Cannot call `%s`. Identity data is not an object.', $method));
109107
}
110108

111109
if (!method_exists($this->identity, $method)) {
@@ -163,11 +161,7 @@ public function offsetExists(mixed $offset): bool
163161
*/
164162
public function offsetGet(mixed $offset): mixed
165163
{
166-
if (isset($this->identity[$offset])) {
167-
return $this->identity[$offset];
168-
}
169-
170-
return null;
164+
return $this->identity[$offset] ?? null;
171165
}
172166

173167
/**

src/Middleware/AuthorizationMiddleware.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,11 @@ class AuthorizationMiddleware implements MiddlewareInterface
6969

7070
/**
7171
* Authorization service or application instance.
72-
*
73-
* @var \Authorization\AuthorizationServiceInterface|\Authorization\AuthorizationServiceProviderInterface
7472
*/
7573
protected AuthorizationServiceInterface|AuthorizationServiceProviderInterface $subject;
7674

7775
/**
7876
* The container instance from the application
79-
*
80-
* @var \Cake\Core\ContainerInterface|null
8177
*/
8278
protected ?ContainerInterface $container = null;
8379

@@ -190,10 +186,8 @@ protected function buildIdentity(
190186

191187
if (is_callable($class)) {
192188
$identity = $class($service, $identity);
193-
} else {
194-
if (!$identity instanceof IdentityInterface) {
195-
$identity = new $class($service, $identity);
196-
}
189+
} elseif (!$identity instanceof IdentityInterface) {
190+
$identity = new $class($service, $identity);
197191
}
198192

199193
if (!$identity instanceof IdentityInterface) {

src/Middleware/UnauthorizedHandler/HandlerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function create(string $name): HandlerInterface
4444
$message = sprintf(
4545
'Handler should implement `%s`, got `%s`.',
4646
HandlerInterface::class,
47-
get_class($instance),
47+
$instance::class,
4848
);
4949
throw new RuntimeException($message);
5050
}

0 commit comments

Comments
 (0)