From 98967eee34044b9ea38789ffd4fb3dd492d63623 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Sun, 29 Mar 2026 16:19:39 +0200 Subject: [PATCH] add rector --- composer.json | 3 + rector.php | 55 +++++++++++++ src/AuthorizationPlugin.php | 3 +- src/AuthorizationService.php | 8 +- src/Command/PolicyCommand.php | 15 ++-- .../Component/AuthorizationComponent.php | 12 +-- src/Exception/ForbiddenException.php | 2 - src/IdentityDecorator.php | 10 +-- src/Middleware/AuthorizationMiddleware.php | 10 +-- .../UnauthorizedHandler/HandlerFactory.php | 2 +- .../UnauthorizedHandler/RedirectHandler.php | 9 +-- .../Exception/MissingMethodException.php | 2 - .../Exception/MissingPolicyException.php | 6 +- src/Policy/MapResolver.php | 6 +- src/Policy/OrmResolver.php | 24 ++---- src/Policy/Result.php | 4 - tests/Fixture/ArticlesFixture.php | 2 - tests/TestCase/AuthorizationServiceTest.php | 78 +++++++++---------- tests/TestCase/Command/PolicyCommandTest.php | 18 ++--- .../Component/AuthorizationComponentTest.php | 73 ++++++++--------- tests/TestCase/IdentityDecoratorTest.php | 22 +++--- .../AuthorizationMiddlewareTest.php | 46 +++++------ .../RequestAuthorizationMiddlewareTest.php | 8 +- .../CakeRedirectHandlerTest.php | 12 +-- .../ExceptionHandlerTest.php | 2 +- .../HandlerFactoryTest.php | 6 +- .../RedirectHandlerTest.php | 14 ++-- tests/TestCase/Policy/MapResolverTest.php | 18 ++--- tests/TestCase/Policy/OrmResolverTest.php | 23 +++--- .../Policy/ResolverCollectionTest.php | 6 +- tests/bootstrap.php | 6 +- .../TestApp/Http/TestRequestHandler.php | 5 +- tests/test_app/TestApp/Identity.php | 4 +- .../test_app/TestApp/Policy/ArticlePolicy.php | 18 ++--- .../TestApp/Policy/ArticlesTablePolicy.php | 2 +- .../TestApp/Policy/MagicCallPolicy.php | 8 +- .../test_app/TestApp/Policy/RequestPolicy.php | 13 +--- 37 files changed, 276 insertions(+), 279 deletions(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 871f5f7e..485ccec4 100644 --- a/composer.json +++ b/composer.json @@ -73,6 +73,9 @@ "stan": "@phpstan", "stan-baseline": "tools/phpstan --generate-baseline", "stan-setup": "phive install", + "rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"~2.3.1\" && mv composer.backup composer.json", + "rector-check": "vendor/bin/rector process --dry-run", + "rector-fix": "vendor/bin/rector process", "test": "phpunit", "test-coverage": "phpunit --coverage-clover=clover.xml" }, diff --git a/rector.php b/rector.php new file mode 100644 index 00000000..e9c2d8df --- /dev/null +++ b/rector.php @@ -0,0 +1,55 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + + ->withCache( + cacheClass: FileCacheStorage::class, + cacheDirectory: $cacheDir, + ) + + ->withPhpSets() + ->withAttributesSets() + + ->withSets([ + SetList::CODE_QUALITY, + SetList::CODING_STYLE, + SetList::DEAD_CODE, + SetList::EARLY_RETURN, + SetList::INSTANCEOF, + SetList::TYPE_DECLARATION, + ]) + + ->withSkip([ + __DIR__ . '/tests/comparisons', + ClassPropertyAssignToConstructorPromotionRector::class, + CatchExceptionNameMatchingTypeRector::class, + ClosureToArrowFunctionRector::class, + RemoveUselessReturnTagRector::class, + CompactToVariablesRector::class, + ReturnTypeFromStrictFluentReturnRector::class, + SplitDoubleAssignRector::class, + NewlineAfterStatementRector::class, + ExplicitBoolCompareRector::class, + TypedPropertyFromCreateMockAssignRector::class, + ]); diff --git a/src/AuthorizationPlugin.php b/src/AuthorizationPlugin.php index 2277e0a8..8d0b6929 100644 --- a/src/AuthorizationPlugin.php +++ b/src/AuthorizationPlugin.php @@ -17,6 +17,7 @@ namespace Authorization; use Authorization\Command\PolicyCommand; +use Bake\Command\SimpleBakeCommand; use Cake\Console\CommandCollection; use Cake\Core\BasePlugin; @@ -33,7 +34,7 @@ class AuthorizationPlugin extends BasePlugin */ public function console(CommandCollection $commands): CommandCollection { - if (class_exists('Bake\Command\SimpleBakeCommand')) { + if (class_exists(SimpleBakeCommand::class)) { $commands->add('bake policy', PolicyCommand::class); } diff --git a/src/AuthorizationService.php b/src/AuthorizationService.php index 41b97ae3..53200126 100644 --- a/src/AuthorizationService.php +++ b/src/AuthorizationService.php @@ -29,15 +29,11 @@ class AuthorizationService implements AuthorizationServiceInterface { /** * Authorization policy resolver. - * - * @var \Authorization\Policy\ResolverInterface */ protected ResolverInterface $resolver; /** * Track whether authorization was checked. - * - * @var bool */ protected bool $authorizationChecked = false; @@ -144,7 +140,7 @@ protected function getCanHandler(mixed $policy, string $action): Closure assert( method_exists($policy, $method) || method_exists($policy, '__call'), - new MissingMethodException([$method, $action, get_class($policy)]), + new MissingMethodException([$method, $action, $policy::class]), ); /** @phpstan-ignore callable.nonCallable */ @@ -165,7 +161,7 @@ protected function getScopeHandler(mixed $policy, string $action): Closure assert( method_exists($policy, $method) || method_exists($policy, '__call'), - new MissingMethodException([$method, $action, get_class($policy)]), + new MissingMethodException([$method, $action, $policy::class]), ); /** @phpstan-ignore callable.nonCallable */ diff --git a/src/Command/PolicyCommand.php b/src/Command/PolicyCommand.php index 5577d860..6bbb6f57 100644 --- a/src/Command/PolicyCommand.php +++ b/src/Command/PolicyCommand.php @@ -16,10 +16,12 @@ */ namespace Authorization\Command; +use Authorization\IdentityInterface; use Bake\Command\SimpleBakeCommand; use Cake\Console\Arguments; use Cake\Console\ConsoleIo; use Cake\Console\ConsoleOptionParser; +use Cake\ORM\Query\SelectQuery; use Cake\Utility\Inflector; use RuntimeException; @@ -30,14 +32,9 @@ class PolicyCommand extends SimpleBakeCommand { /** * Path to Policy directory - * - * @var string */ public string $pathFragment = 'Policy/'; - /** - * @var string - */ protected string $type; /** @@ -91,13 +88,13 @@ public function templateData(Arguments $arguments): array $imports = []; $className = $data['namespace'] . '\\' . $name; if ($type === 'table') { - $className = "{$data['namespace']}\Model\\Table\\{$name}{$suffix}"; - $imports[] = 'Cake\ORM\Query\SelectQuery'; + $className = sprintf('%s\Model\Table\%s%s', $data['namespace'], $name, $suffix); + $imports[] = SelectQuery::class; } elseif ($type === 'entity') { - $className = "{$data['namespace']}\Model\\Entity\\{$name}"; + $className = sprintf('%s\Model\Entity\%s', $data['namespace'], $name); $imports[] = $className; } - $imports[] = 'Authorization\\IdentityInterface'; + $imports[] = IdentityInterface::class; $variable = Inflector::variable($name); if ($variable === 'user') { diff --git a/src/Controller/Component/AuthorizationComponent.php b/src/Controller/Component/AuthorizationComponent.php index 37b4bcfb..c87a5e81 100644 --- a/src/Controller/Component/AuthorizationComponent.php +++ b/src/Controller/Component/AuthorizationComponent.php @@ -73,7 +73,7 @@ public function authorize(mixed $resource, ?string $action = null): void } if (is_object($resource)) { - $name = get_class($resource); + $name = $resource::class; } elseif (is_string($resource)) { $name = $resource; } else { @@ -131,7 +131,7 @@ protected function performCheck( } $identity = $this->getIdentity($request); - if ($identity === null) { + if (!$identity instanceof IdentityInterface) { return $this->getService($request)->{$method}(null, $action, $resource); } @@ -156,7 +156,7 @@ public function applyScope(mixed $resource, ?string $action = null, mixed ...$op $action = $this->getDefaultAction($request); } $identity = $this->getIdentity($request); - if ($identity === null) { + if (!$identity instanceof IdentityInterface) { return $this->getService($request)->applyScope(null, $action, $resource); } @@ -233,7 +233,7 @@ protected function getService(ServerRequestInterface $request): AuthorizationSer $serviceAttribute = $this->getConfig('serviceAttribute'); $service = $request->getAttribute($serviceAttribute); if (!$service instanceof AuthorizationServiceInterface) { - $type = is_object($service) ? get_class($service) : gettype($service); + $type = get_debug_type($service); throw new InvalidArgumentException(sprintf( 'Expected that `%s` would be an instance of %s, but got %s', $serviceAttribute, @@ -261,7 +261,7 @@ protected function getIdentity(ServerRequestInterface $request): ?IdentityInterf return $identity; } if (!$identity instanceof IdentityInterface) { - $type = is_object($identity) ? get_class($identity) : gettype($identity); + $type = get_debug_type($identity); throw new InvalidArgumentException(sprintf( 'Expected that `%s` would be an instance of %s, but got %s', $identityAttribute, @@ -328,7 +328,7 @@ protected function getDefaultAction(ServerRequest $request): string return $action; } if (!is_string($name)) { - $type = is_object($name) ? get_class($name) : gettype($name); + $type = get_debug_type($name); $message = sprintf('Invalid action type for `%s`. Expected `string` or `null`, got `%s`.', $action, $type); throw new UnexpectedValueException($message); } diff --git a/src/Exception/ForbiddenException.php b/src/Exception/ForbiddenException.php index 4ad27085..1d14416f 100644 --- a/src/Exception/ForbiddenException.php +++ b/src/Exception/ForbiddenException.php @@ -33,8 +33,6 @@ class ForbiddenException extends Exception /** * Policy check result. - * - * @var \Authorization\Policy\ResultInterface|null */ protected ?ResultInterface $result = null; diff --git a/src/IdentityDecorator.php b/src/IdentityDecorator.php index 77d8da2b..424ca38d 100644 --- a/src/IdentityDecorator.php +++ b/src/IdentityDecorator.php @@ -39,8 +39,6 @@ class IdentityDecorator implements IdentityInterface /** * Authorization Service - * - * @var \Authorization\AuthorizationServiceInterface */ protected AuthorizationServiceInterface $authorization; @@ -105,7 +103,7 @@ public function getOriginalData(): ArrayAccess|array public function __call(string $method, array $args): mixed { if (!is_object($this->identity)) { - throw new BadMethodCallException("Cannot call `{$method}`. Identity data is not an object."); + throw new BadMethodCallException(sprintf('Cannot call `%s`. Identity data is not an object.', $method)); } if (!method_exists($this->identity, $method)) { @@ -163,11 +161,7 @@ public function offsetExists(mixed $offset): bool */ public function offsetGet(mixed $offset): mixed { - if (isset($this->identity[$offset])) { - return $this->identity[$offset]; - } - - return null; + return $this->identity[$offset] ?? null; } /** diff --git a/src/Middleware/AuthorizationMiddleware.php b/src/Middleware/AuthorizationMiddleware.php index ecdc8def..324474f6 100644 --- a/src/Middleware/AuthorizationMiddleware.php +++ b/src/Middleware/AuthorizationMiddleware.php @@ -69,15 +69,11 @@ class AuthorizationMiddleware implements MiddlewareInterface /** * Authorization service or application instance. - * - * @var \Authorization\AuthorizationServiceInterface|\Authorization\AuthorizationServiceProviderInterface */ protected AuthorizationServiceInterface|AuthorizationServiceProviderInterface $subject; /** * The container instance from the application - * - * @var \Cake\Core\ContainerInterface|null */ protected ?ContainerInterface $container = null; @@ -190,10 +186,8 @@ protected function buildIdentity( if (is_callable($class)) { $identity = $class($service, $identity); - } else { - if (!$identity instanceof IdentityInterface) { - $identity = new $class($service, $identity); - } + } elseif (!$identity instanceof IdentityInterface) { + $identity = new $class($service, $identity); } if (!$identity instanceof IdentityInterface) { diff --git a/src/Middleware/UnauthorizedHandler/HandlerFactory.php b/src/Middleware/UnauthorizedHandler/HandlerFactory.php index 409beb49..403348f9 100644 --- a/src/Middleware/UnauthorizedHandler/HandlerFactory.php +++ b/src/Middleware/UnauthorizedHandler/HandlerFactory.php @@ -44,7 +44,7 @@ public static function create(string $name): HandlerInterface $message = sprintf( 'Handler should implement `%s`, got `%s`.', HandlerInterface::class, - get_class($instance), + $instance::class, ); throw new RuntimeException($message); } diff --git a/src/Middleware/UnauthorizedHandler/RedirectHandler.php b/src/Middleware/UnauthorizedHandler/RedirectHandler.php index ccc53578..9a302c25 100644 --- a/src/Middleware/UnauthorizedHandler/RedirectHandler.php +++ b/src/Middleware/UnauthorizedHandler/RedirectHandler.php @@ -108,12 +108,8 @@ protected function getUrl(ServerRequestInterface $request, array $options): stri if ($uri->getQuery()) { $redirect .= '?' . $uri->getQuery(); } - $query = urlencode($options['queryParam']) . '=' . urlencode($redirect); - if (str_contains($url, '?')) { - $query = '&' . $query; - } else { - $query = '?' . $query; - } + $query = urlencode((string)$options['queryParam']) . '=' . urlencode($redirect); + $query = str_contains((string)$url, '?') ? '&' . $query : '?' . $query; $url .= $query; } @@ -122,7 +118,6 @@ protected function getUrl(ServerRequestInterface $request, array $options): stri } /** - * @param \Psr\Http\Message\ServerRequestInterface $request * @param array $options * @return bool */ diff --git a/src/Policy/Exception/MissingMethodException.php b/src/Policy/Exception/MissingMethodException.php index a5f9f496..22b1893e 100644 --- a/src/Policy/Exception/MissingMethodException.php +++ b/src/Policy/Exception/MissingMethodException.php @@ -22,8 +22,6 @@ class MissingMethodException extends Exception { /** * Template string that has attributes sprintf()'ed into it. - * - * @var string */ protected string $_messageTemplate = 'Method `%s` for invoking action `%s` has not been defined in `%s`.'; } diff --git a/src/Policy/Exception/MissingPolicyException.php b/src/Policy/Exception/MissingPolicyException.php index 484e510c..8885ba12 100644 --- a/src/Policy/Exception/MissingPolicyException.php +++ b/src/Policy/Exception/MissingPolicyException.php @@ -24,8 +24,6 @@ class MissingPolicyException extends Exception { /** * Template string that has attributes sprintf()'ed into it. - * - * @var string */ protected string $_messageTemplate = 'Policy for `%s` has not been defined.'; @@ -38,13 +36,13 @@ class MissingPolicyException extends Exception public function __construct(object|string|array $resource, ?int $code = null, ?Throwable $previous = null) { if (is_object($resource)) { - $resourceClass = get_class($resource); + $resourceClass = $resource::class; if ( method_exists($resource, 'getRepository') && $resource->getRepository() && $resource->getRepository() instanceof RepositoryInterface ) { - $repositoryClass = get_class($resource->getRepository()); + $repositoryClass = $resource->getRepository()::class; $resource = sprintf($this->_messageTemplate, $resourceClass); $queryMessage = ' This resource looks like a `Query`. If you are using `OrmResolver`, ' . 'you should create a new policy class for your `%s` class in `src/Policy/`.'; diff --git a/src/Policy/MapResolver.php b/src/Policy/MapResolver.php index 10c2f085..170ed713 100644 --- a/src/Policy/MapResolver.php +++ b/src/Policy/MapResolver.php @@ -35,8 +35,6 @@ class MapResolver implements ResolverInterface /** * The DIC instance from the application - * - * @var \Cake\Core\ContainerInterface|null */ protected ?ContainerInterface $container; @@ -101,7 +99,7 @@ public function getPolicy($resource): mixed throw new InvalidArgumentException($message); } - $class = get_class($resource); + $class = $resource::class; if (!isset($this->map[$class])) { throw new MissingPolicyException($resource); @@ -117,7 +115,7 @@ public function getPolicy($resource): mixed return $policy; } - if ($this->container && $this->container->has($policy)) { + if ($this->container instanceof ContainerInterface && $this->container->has($policy)) { return $this->container->get($policy); } diff --git a/src/Policy/OrmResolver.php b/src/Policy/OrmResolver.php index 882eb977..d52aedd1 100644 --- a/src/Policy/OrmResolver.php +++ b/src/Policy/OrmResolver.php @@ -32,8 +32,6 @@ class OrmResolver implements ResolverInterface { /** * Application namespace. - * - * @var string */ protected string $appNamespace = 'App'; @@ -46,8 +44,6 @@ class OrmResolver implements ResolverInterface /** * The DIC instance from the application - * - * @var \Cake\Core\ContainerInterface|null */ protected ?ContainerInterface $container; @@ -86,7 +82,7 @@ public function getPolicy(mixed $resource): mixed } if ($resource instanceof QueryInterface) { $repo = $resource->getRepository(); - if ($repo === null) { + if (!$repo instanceof RepositoryInterface) { throw new RuntimeException('No repository set for the query.'); } @@ -104,7 +100,7 @@ public function getPolicy(mixed $resource): mixed */ protected function getEntityPolicy(EntityInterface $entity): mixed { - $class = get_class($entity); + $class = $entity::class; $entityNamespace = '\Model\Entity\\'; $namespace = str_replace('\\', '/', substr($class, 0, (int)strpos($class, $entityNamespace))); $name = substr($class, strpos($class, $entityNamespace) + strlen($entityNamespace)); @@ -120,7 +116,7 @@ protected function getEntityPolicy(EntityInterface $entity): mixed */ protected function getRepositoryPolicy(RepositoryInterface $table): mixed { - $class = get_class($table); + $class = $table::class; $tableNamespace = '\Model\Table\\'; $namespace = str_replace('\\', '/', substr($class, 0, (int)strpos($class, $tableNamespace))); $name = substr($class, strpos($class, $tableNamespace) + strlen($tableNamespace)); @@ -157,13 +153,11 @@ protected function findPolicy(string $class, string $name, string $namespace): m throw new MissingPolicyException([$class]); } - if ($this->container && $this->container->has($policyClass)) { - $policy = $this->container->get($policyClass); - } else { - $policy = new $policyClass(); + if ($this->container instanceof ContainerInterface && $this->container->has($policyClass)) { + return $this->container->get($policyClass); } - return $policy; + return new $policyClass(); } /** @@ -174,10 +168,6 @@ protected function findPolicy(string $class, string $name, string $namespace): m */ protected function getNamespace(string $namespace): string { - if (isset($this->overrides[$namespace])) { - return $this->overrides[$namespace]; - } - - return $namespace; + return $this->overrides[$namespace] ?? $namespace; } } diff --git a/src/Policy/Result.php b/src/Policy/Result.php index 528e4509..510f06a1 100644 --- a/src/Policy/Result.php +++ b/src/Policy/Result.php @@ -23,15 +23,11 @@ class Result implements ResultInterface { /** * Check status. - * - * @var bool */ protected bool $status; /** * Failure reason. - * - * @var string|null */ protected ?string $reason = null; diff --git a/tests/Fixture/ArticlesFixture.php b/tests/Fixture/ArticlesFixture.php index 7ed080fb..a9573625 100644 --- a/tests/Fixture/ArticlesFixture.php +++ b/tests/Fixture/ArticlesFixture.php @@ -25,8 +25,6 @@ class ArticlesFixture extends TestFixture { /** * records property - * - * @var array */ public array $records = [ ['author_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y'], diff --git a/tests/TestCase/AuthorizationServiceTest.php b/tests/TestCase/AuthorizationServiceTest.php index af910a19..c68d6d47 100644 --- a/tests/TestCase/AuthorizationServiceTest.php +++ b/tests/TestCase/AuthorizationServiceTest.php @@ -37,7 +37,7 @@ class AuthorizationServiceTest extends TestCase { - public function testNullUserCan() + public function testNullUserCan(): void { $resolver = new MapResolver([ Article::class => ArticlePolicy::class, @@ -54,7 +54,7 @@ public function testNullUserCan() $this->assertTrue($result); } - public function testCan() + public function testCan(): void { $resolver = new MapResolver([ Article::class => ArticlePolicy::class, @@ -70,7 +70,7 @@ public function testCan() $this->assertTrue($result); } - public function testCanWithAdditionalParams() + public function testCanWithAdditionalParams(): void { $resolver = new MapResolver([ Article::class => ArticlePolicy::class, @@ -82,7 +82,7 @@ public function testCanWithAdditionalParams() 'role' => 'admin', ]); - $innerService = function () { + $innerService = function (): bool { return true; }; @@ -90,7 +90,7 @@ public function testCanWithAdditionalParams() $this->assertTrue($result); } - public function testCanWithAdditionalNamedParams() + public function testCanWithAdditionalNamedParams(): void { $resolver = new MapResolver([ Article::class => ArticlePolicy::class, @@ -102,11 +102,11 @@ public function testCanWithAdditionalNamedParams() 'role' => 'admin', ]); - $innerService1 = function () { + $innerService1 = function (): bool { return true; }; - $innerService2 = function () { + $innerService2 = function (): bool { return false; }; @@ -114,7 +114,7 @@ public function testCanWithAdditionalNamedParams() $this->assertFalse($result); } - public function testCanWithResult() + public function testCanWithResult(): void { $resolver = new MapResolver([ Article::class => ArticlePolicy::class, @@ -130,7 +130,7 @@ public function testCanWithResult() $this->assertInstanceOf(ResultInterface::class, $result); } - public function testCanWithResultAndAdditionalParams() + public function testCanWithResultAndAdditionalParams(): void { $resolver = new MapResolver([ Article::class => ArticlePolicy::class, @@ -142,7 +142,7 @@ public function testCanWithResultAndAdditionalParams() 'role' => 'admin', ]); - $innerService = function () { + $innerService = function (): bool { return true; }; @@ -150,7 +150,7 @@ public function testCanWithResultAndAdditionalParams() $this->assertInstanceOf(ResultInterface::class, $result); } - public function testCanWithResultAndAdditionalNamedParams() + public function testCanWithResultAndAdditionalNamedParams(): void { $resolver = new MapResolver([ Article::class => ArticlePolicy::class, @@ -162,11 +162,11 @@ public function testCanWithResultAndAdditionalNamedParams() 'role' => 'admin', ]); - $innerService1 = function () { + $innerService1 = function (): bool { return true; }; - $innerService2 = function () { + $innerService2 = function (): bool { return false; }; @@ -174,7 +174,7 @@ public function testCanWithResultAndAdditionalNamedParams() $this->assertInstanceOf(ResultInterface::class, $result); } - public function testAuthorizationCheckedWithCan() + public function testAuthorizationCheckedWithCan(): void { $resolver = new MapResolver([ Article::class => ArticlePolicy::class, @@ -190,7 +190,7 @@ public function testAuthorizationCheckedWithCan() $this->assertTrue($service->authorizationChecked()); } - public function testCallingMagicCanCallPolicy() + public function testCallingMagicCanCallPolicy(): void { $resolver = new MapResolver([ Article::class => MagicCallPolicy::class, @@ -207,7 +207,7 @@ public function testCallingMagicCanCallPolicy() $this->assertFalse($service->can($user, 'cantDoThis', $article)); } - public function testCallingMagicScopeCallPolicy() + public function testCallingMagicScopeCallPolicy(): void { $resolver = new MapResolver([ Article::class => MagicCallPolicy::class, @@ -224,7 +224,7 @@ public function testCallingMagicScopeCallPolicy() $this->assertFalse($service->applyScope($user, 'somethingElse', $article)); } - public function testAuthorizationCheckedWithApplyScope() + public function testAuthorizationCheckedWithApplyScope(): void { $resolver = new MapResolver([ Article::class => ArticlePolicy::class, @@ -241,7 +241,7 @@ public function testAuthorizationCheckedWithApplyScope() $this->assertTrue($service->authorizationChecked()); } - public function testSkipAuthorization() + public function testSkipAuthorization(): void { $resolver = new MapResolver([]); $service = new AuthorizationService($resolver); @@ -251,7 +251,7 @@ public function testSkipAuthorization() $this->assertTrue($service->authorizationChecked()); } - public function testApplyScope() + public function testApplyScope(): void { $resolver = new MapResolver([ Article::class => ArticlePolicy::class, @@ -268,7 +268,7 @@ public function testApplyScope() $this->assertSame($article->user_id, $user->getOriginalData()['id']); } - public function testApplyScopeMethodMissing() + public function testApplyScopeMethodMissing(): void { $this->expectException(MissingMethodException::class); @@ -285,7 +285,7 @@ public function testApplyScopeMethodMissing() $service->applyScope($user, 'nope', $article); } - public function testApplyScopeAdditionalArguments() + public function testApplyScopeAdditionalArguments(): void { $service = new AuthorizationService(new OrmResolver()); $user = new IdentityDecorator($service, [ @@ -317,16 +317,16 @@ public function testApplyScopeAdditionalArguments() $this->assertSame($query, $result); } - public function testBeforeFalse() + public function testBeforeFalse(): void { $entity = new Article(); $policy = new class implements BeforePolicyInterface { - public function before($identity, $resource, $action): bool|ResultInterface|null + public function before($identity, $resource, $action): bool { return false; } - public function canAdd($user, $entity) + public function canAdd($user, $entity): never { throw new ExpectationFailedException('This method should not be called'); } @@ -346,16 +346,16 @@ public function canAdd($user, $entity) $this->assertFalse($result); } - public function testBeforeTrue() + public function testBeforeTrue(): void { $entity = new Article(); $policy = new class implements BeforePolicyInterface { - public function before($identity, $resource, $action): bool|ResultInterface|null + public function before($identity, $resource, $action): bool { return true; } - public function canAdd($user, $entity) + public function canAdd($user, $entity): never { throw new ExpectationFailedException('This method should not be called'); } @@ -375,7 +375,7 @@ public function canAdd($user, $entity) $this->assertTrue($result); } - public function testBeforeNull() + public function testBeforeNull(): void { $entity = new Article(); $policy = new class implements BeforePolicyInterface { @@ -404,16 +404,16 @@ public function canAdd($user, $entity): bool $this->assertTrue($result); } - public function testBeforeResultTrue() + public function testBeforeResultTrue(): void { $entity = new Article(); $policy = new class implements BeforePolicyInterface { - public function before($identity, $resource, $action): bool|ResultInterface|null + public function before($identity, $resource, $action): ResultInterface { return new Result(true); } - public function canAdd($user, $entity) + public function canAdd($user, $entity): never { throw new ExpectationFailedException('This method should not be called'); } @@ -433,16 +433,16 @@ public function canAdd($user, $entity) $this->assertTrue($result); } - public function testBeforeResultFalse() + public function testBeforeResultFalse(): void { $entity = new Article(); $policy = new class implements BeforePolicyInterface { - public function before($identity, $resource, $action): bool|ResultInterface|null + public function before($identity, $resource, $action): ResultInterface { return new Result(false); } - public function canAdd($user, $entity) + public function canAdd($user, $entity): never { throw new ExpectationFailedException('This method should not be called'); } @@ -462,7 +462,7 @@ public function canAdd($user, $entity) $this->assertFalse($result); } - public function testBeforeScopeNonNull() + public function testBeforeScopeNonNull(): void { $entity = new Article(); $policy = new class implements BeforeScopeInterface { @@ -471,7 +471,7 @@ public function beforeScope(?IdentityInterface $identity, mixed $resource, strin return 'foo'; } - public function scopeIndex(IdentityInterface $user, QueryInterface $query) + public function scopeIndex(IdentityInterface $user, QueryInterface $query): never { throw new ExpectationFailedException('This method should not be called'); } @@ -491,7 +491,7 @@ public function scopeIndex(IdentityInterface $user, QueryInterface $query) $this->assertEquals('foo', $result); } - public function testBeforeScopeNull() + public function testBeforeScopeNull(): void { $entity = new Article(); $policy = new class implements BeforeScopeInterface { @@ -500,7 +500,7 @@ public function beforeScope(?IdentityInterface $identity, mixed $resource, strin return null; } - public function scopeIndex(IdentityInterface $user, EntityInterface $entity) + public function scopeIndex(IdentityInterface $user, EntityInterface $entity): string { return 'bar'; } @@ -520,7 +520,7 @@ public function scopeIndex(IdentityInterface $user, EntityInterface $entity) $this->assertEquals('bar', $result); } - public function testMissingMethod() + public function testMissingMethod(): void { $entity = new Article(); diff --git a/tests/TestCase/Command/PolicyCommandTest.php b/tests/TestCase/Command/PolicyCommandTest.php index 15da657f..27218ac2 100644 --- a/tests/TestCase/Command/PolicyCommandTest.php +++ b/tests/TestCase/Command/PolicyCommandTest.php @@ -45,16 +45,16 @@ class PolicyCommandTest extends TestCase * * @return void */ - public function setUp(): void + protected function setUp(): void { parent::setUp(); Router::reload(); - $this->comparisonDir = dirname(dirname(__DIR__)) . DS . 'comparisons' . DS; + $this->comparisonDir = dirname(__DIR__, 2) . DS . 'comparisons' . DS; $this->loadPlugins(['TestPlugin']); } - public function tearDown(): void + protected function tearDown(): void { parent::tearDown(); @@ -64,7 +64,7 @@ public function tearDown(): void } } - public function testMainDefaultToEntity() + public function testMainDefaultToEntity(): void { $this->generatedFile = APP . 'Policy/BookmarkPolicy.php'; @@ -74,7 +74,7 @@ public function testMainDefaultToEntity() $this->assertOutputContains('Creating file ' . $this->generatedFile); } - public function testMainEntityType() + public function testMainEntityType(): void { $this->generatedFile = APP . 'Policy/BookmarkPolicy.php'; @@ -88,7 +88,7 @@ public function testMainEntityType() ); } - public function testMainObjectType() + public function testMainObjectType(): void { $this->generatedFile = APP . 'Policy/ThingPolicy.php'; @@ -102,7 +102,7 @@ public function testMainObjectType() ); } - public function testMainTableType() + public function testMainTableType(): void { $this->generatedFile = APP . 'Policy/BookmarksTablePolicy.php'; @@ -116,7 +116,7 @@ public function testMainTableType() ); } - public function testMainPluginEntity() + public function testMainPluginEntity(): void { $this->generatedFile = ROOT . 'Plugin/TestPlugin/src/Policy/UserPolicy.php'; @@ -130,7 +130,7 @@ public function testMainPluginEntity() ); } - public function testMainPluginTable() + public function testMainPluginTable(): void { $this->generatedFile = ROOT . 'Plugin/TestPlugin/src/Policy/UsersTablePolicy.php'; diff --git a/tests/TestCase/Controller/Component/AuthorizationComponentTest.php b/tests/TestCase/Controller/Component/AuthorizationComponentTest.php index 69272a47..b1d17e68 100644 --- a/tests/TestCase/Controller/Component/AuthorizationComponentTest.php +++ b/tests/TestCase/Controller/Component/AuthorizationComponentTest.php @@ -68,7 +68,7 @@ class AuthorizationComponentTest extends TestCase * * @return void */ - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -87,7 +87,7 @@ public function setUp(): void $this->Auth = new AuthorizationComponent($this->ComponentRegistry); } - public function testNullIdentityForbiddenException() + public function testNullIdentityForbiddenException(): void { $this->expectException(ForbiddenException::class); $this->expectExceptionMessage('Identity is not authorized to perform `view` on `TestApp\Model\Entity\Article`.'); @@ -107,7 +107,7 @@ public function testNullIdentityForbiddenException() $auth->authorize($article); } - public function testNullIdentityAllowed() + public function testNullIdentityAllowed(): void { $service = new AuthorizationService(new OrmResolver()); $request = new ServerRequest([ @@ -124,14 +124,14 @@ public function testNullIdentityAllowed() $this->assertNull($auth->authorize($article)); } - public function testAuthorizeUnresolvedPolicy() + public function testAuthorizeUnresolvedPolicy(): void { $this->expectException(MissingPolicyException::class); $this->Auth->authorize(new stdClass()); } - public function testAuthorizeFailedCheck() + public function testAuthorizeFailedCheck(): void { $this->expectException(ForbiddenException::class); @@ -139,7 +139,7 @@ public function testAuthorizeFailedCheck() $this->Auth->authorize($article); } - public function testAuthorizeFailedCheckWithResult() + public function testAuthorizeFailedCheckWithResult(): void { $this->expectException(ForbiddenException::class); @@ -155,7 +155,7 @@ public function testAuthorizeFailedCheckWithResult() } } - public function testAuthorizeFailedCheckStringResolver() + public function testAuthorizeFailedCheckStringResolver(): void { // Reset the system to use the string resolver $service = new AuthorizationService(new StringResolver()); @@ -177,13 +177,13 @@ public function testAuthorizeFailedCheckStringResolver() $this->Auth->authorize('ArticlesTable'); } - public function testAuthorizeSuccessCheckImplicitAction() + public function testAuthorizeSuccessCheckImplicitAction(): void { $article = new Article(['user_id' => 1]); $this->assertNull($this->Auth->authorize($article)); } - public function testAuthorizeSuccessCheckMappedAction() + public function testAuthorizeSuccessCheckMappedAction(): void { $policy = $this->createMock(ArticlePolicy::class); $service = new AuthorizationService(new MapResolver([ @@ -207,7 +207,7 @@ public function testAuthorizeSuccessCheckMappedAction() $this->assertNull($this->Auth->authorize($article)); } - public function testAuthorizeSuccessCheckStringResolver() + public function testAuthorizeSuccessCheckStringResolver(): void { // Reset the system to use the string resolver $service = new AuthorizationService(new StringResolver()); @@ -227,13 +227,13 @@ public function testAuthorizeSuccessCheckStringResolver() $this->assertNull($this->Auth->authorize('ArticlesTable')); } - public function testAuthorizeSuccessfulCheckWithResult() + public function testAuthorizeSuccessfulCheckWithResult(): void { $article = new Article(['user_id' => 1]); $this->assertNull($this->Auth->authorize($article, 'publish')); } - public function testApplyScopeImplicitAction() + public function testApplyScopeImplicitAction(): void { $articles = new ArticlesTable(); $query = $this->createMock(QueryInterface::class); @@ -253,7 +253,7 @@ public function testApplyScopeImplicitAction() $this->assertSame($query, $result); } - public function testApplyScopeNoUser() + public function testApplyScopeNoUser(): void { $this->request = $this->request ->withoutAttribute('identity'); @@ -280,7 +280,7 @@ public function testApplyScopeNoUser() $this->assertSame($query, $result); } - public function testApplyScopeMappedAction() + public function testApplyScopeMappedAction(): void { $articles = new ArticlesTable(); $query = $this->createMock(QueryInterface::class); @@ -301,7 +301,7 @@ public function testApplyScopeMappedAction() $this->assertSame($query, $result); } - public function testApplyScopExplicitAction() + public function testApplyScopExplicitAction(): void { $articles = new ArticlesTable(); $query = $this->createMock(QueryInterface::class); @@ -321,7 +321,7 @@ public function testApplyScopExplicitAction() $this->assertSame($query, $result); } - public function testApplyScopeAdditionalArguments() + public function testApplyScopeAdditionalArguments(): void { $articles = new ArticlesTable(); $query = $this->createMock(QueryInterface::class); @@ -343,13 +343,13 @@ public function testApplyScopeAdditionalArguments() $this->assertSame($query, $result); } - public function testAuthorizeSuccessCheckExplicitAction() + public function testAuthorizeSuccessCheckExplicitAction(): void { $article = new Article(['user_id' => 1]); $this->assertNull($this->Auth->authorize($article, 'edit')); } - public function testAuthorizeBadIdentity() + public function testAuthorizeBadIdentity(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessageMatches('/Expected that `identity` would be/'); @@ -361,7 +361,7 @@ public function testAuthorizeBadIdentity() $this->Auth->authorize($article); } - public function testAuthorizeModelSuccess() + public function testAuthorizeModelSuccess(): void { $service = new AuthorizationService(new OrmResolver()); $identity = new IdentityDecorator($service, ['can_edit' => true]); @@ -372,7 +372,7 @@ public function testAuthorizeModelSuccess() $this->assertNull($result); } - public function testAuthorizeModelFailure() + public function testAuthorizeModelFailure(): void { $service = new AuthorizationService(new OrmResolver()); $identity = new IdentityDecorator($service, ['can_edit' => false]); @@ -386,7 +386,7 @@ public function testAuthorizeModelFailure() $this->Auth->authorizeAction(); } - public function testAuthorizeModelAllDisabled() + public function testAuthorizeModelAllDisabled(): void { $policy = $this->createMock(ArticlesTablePolicy::class); $service = new AuthorizationService(new MapResolver([ @@ -404,7 +404,7 @@ public function testAuthorizeModelAllDisabled() $this->assertNull($result); } - public function testAuthorizeModelActionEnabled() + public function testAuthorizeModelActionEnabled(): void { $service = new AuthorizationService(new OrmResolver()); $identity = new IdentityDecorator($service, ['can_edit' => true]); @@ -416,7 +416,7 @@ public function testAuthorizeModelActionEnabled() $this->assertNull($result); } - public function testAuthorizeModelMapped() + public function testAuthorizeModelMapped(): void { $policy = $this->createMock(ArticlesTablePolicy::class); $service = new AuthorizationService(new MapResolver([ @@ -436,11 +436,12 @@ public function testAuthorizeModelMapped() $this->Auth->setConfig('authorizeModel', ['edit']); $this->Auth->setConfig('actionMap', ['edit' => 'modify']); + $result = $this->Auth->authorizeAction(); $this->assertNull($result); } - public function testAuthorizeModelInvalid() + public function testAuthorizeModelInvalid(): void { $service = new AuthorizationService(new OrmResolver()); $identity = new IdentityDecorator($service, ['can_edit' => true]); @@ -455,7 +456,7 @@ public function testAuthorizeModelInvalid() $this->Auth->authorizeAction(); } - public function testImplementedEvents() + public function testImplementedEvents(): void { $events = $this->Auth->implementedEvents(); $this->assertEquals([ @@ -463,7 +464,7 @@ public function testImplementedEvents() ], $events); } - public function testImplementedCustom() + public function testImplementedCustom(): void { $this->Auth->setConfig('authorizationEvent', 'Controller.initialize'); $events = $this->Auth->implementedEvents(); @@ -472,7 +473,7 @@ public function testImplementedCustom() ], $events); } - public function testSkipAuthorization() + public function testSkipAuthorization(): void { $service = $this->Controller->getRequest()->getAttribute('authorization'); $this->assertFalse($service->authorizationChecked()); @@ -481,7 +482,7 @@ public function testSkipAuthorization() $this->assertTrue($service->authorizationChecked()); } - public function testSkipAuthorizationBadService() + public function testSkipAuthorizationBadService(): void { $this->Controller->setRequest($this->request ->withAttribute('authorization', 'derp')); @@ -491,7 +492,7 @@ public function testSkipAuthorizationBadService() $this->Auth->skipAuthorization(); } - public function testAuthorizeNotSkipped() + public function testAuthorizeNotSkipped(): void { $service = $this->Controller->getRequest()->getAttribute('authorization'); @@ -499,7 +500,7 @@ public function testAuthorizeNotSkipped() $this->assertFalse($service->authorizationChecked()); } - public function testAuthorizeActionSkipped() + public function testAuthorizeActionSkipped(): void { $service = $this->Controller->getRequest()->getAttribute('authorization'); @@ -508,7 +509,7 @@ public function testAuthorizeActionSkipped() $this->assertTrue($service->authorizationChecked()); } - public function testCan() + public function testCan(): void { $article = new Article(['user_id' => 1]); $this->assertTrue($this->Auth->can($article)); @@ -519,7 +520,7 @@ public function testCan() $this->assertFalse($this->Auth->can($article, 'delete')); } - public function testCanWithoutUser() + public function testCanWithoutUser(): void { $this->request = $this->request ->withoutAttribute('identity'); @@ -533,14 +534,14 @@ public function testCanWithoutUser() $this->assertTrue($auth->can($article, 'view')); } - public function testCanWithResult() + public function testCanWithResult(): void { $article = new Article(['user_id' => 1]); $result = $this->Auth->canResult($article, 'publish'); $this->assertInstanceOf(ResultInterface::class, $result); } - public function testAuthorizeModel() + public function testAuthorizeModel(): void { $this->Auth->authorizeModel('foo', 'bar'); $this->assertEquals(['foo', 'bar'], $this->Auth->getConfig('authorizeModel')); @@ -549,7 +550,7 @@ public function testAuthorizeModel() $this->assertEquals(['foo', 'bar', 'baz'], $this->Auth->getConfig('authorizeModel')); } - public function testMapAction() + public function testMapAction(): void { $this->Auth->mapAction('foo', 'bar'); $this->assertEquals(['foo' => 'bar'], $this->Auth->getConfig('actionMap')); @@ -558,7 +559,7 @@ public function testMapAction() $this->assertEquals(['foo' => 'bar', 'bar' => 'baz'], $this->Auth->getConfig('actionMap')); } - public function testMapActions() + public function testMapActions(): void { $this->Auth->mapActions(['foo' => 'bar']); $this->assertEquals(['foo' => 'bar'], $this->Auth->getConfig('actionMap')); diff --git a/tests/TestCase/IdentityDecoratorTest.php b/tests/TestCase/IdentityDecoratorTest.php index be2a2fb0..0452b9a5 100644 --- a/tests/TestCase/IdentityDecoratorTest.php +++ b/tests/TestCase/IdentityDecoratorTest.php @@ -17,7 +17,7 @@ */ class IdentityDecoratorTest extends TestCase { - public static function constructorDataProvider() + public static function constructorDataProvider(): array { return [ 'array' => [ @@ -33,14 +33,14 @@ public static function constructorDataProvider() } #[DataProvider('constructorDataProvider')] - public function testConstructorAccepted($data) + public function testConstructorAccepted(ArrayObject|Article|array $data): void { $auth = $this->createStub(AuthorizationServiceInterface::class); $identity = new IdentityDecorator($auth, $data); $this->assertSame($data['id'], $identity['id']); } - public function testCanDelegation() + public function testCanDelegation(): void { $resource = new stdClass(); $auth = $this->createMock(AuthorizationServiceInterface::class); @@ -53,7 +53,7 @@ public function testCanDelegation() $this->assertTrue($identity->can('update', $resource)); } - public function testApplyScopeDelegation() + public function testApplyScopeDelegation(): void { $resource = new stdClass(); $auth = $this->createMock(AuthorizationServiceInterface::class); @@ -66,7 +66,7 @@ public function testApplyScopeDelegation() $this->assertTrue($identity->applyScope('update', $resource)); } - public function testApplyScopeAdditionalParams() + public function testApplyScopeAdditionalParams(): void { $resource = new stdClass(); $auth = $this->createMock(AuthorizationServiceInterface::class); @@ -79,7 +79,7 @@ public function testApplyScopeAdditionalParams() $this->assertTrue($identity->applyScope('update', $resource, 'first argument', false)); } - public function testCall() + public function testCall(): void { $data = new Article(['id' => 1]); $auth = $this->createStub(AuthorizationServiceInterface::class); @@ -88,7 +88,7 @@ public function testCall() $this->assertTrue($identity->isDirty('id'), 'method is callable though.'); } - public function testCallArray() + public function testCallArray(): void { $this->expectException(BadMethodCallException::class); $data = ['id' => 1]; @@ -97,7 +97,7 @@ public function testCallArray() $identity->boom(); } - public function testArrayAccessImplementation() + public function testArrayAccessImplementation(): void { $data = new ArrayObject(['id' => 1]); $auth = $this->createStub(AuthorizationServiceInterface::class); @@ -114,7 +114,7 @@ public function testArrayAccessImplementation() $this->assertSame(99, $identity['id'], 'Properties can be set.'); } - public function testGetOriginalData() + public function testGetOriginalData(): void { $data = ['id' => 2]; $auth = $this->createStub(AuthorizationServiceInterface::class); @@ -122,7 +122,7 @@ public function testGetOriginalData() $this->assertSame($data, $identity->getOriginalData()); } - public function testGetOriginalDataWrappedObjectHasOriginalData() + public function testGetOriginalDataWrappedObjectHasOriginalData(): void { $data = ['id' => 2]; $auth = $this->createStub(AuthorizationServiceInterface::class); @@ -131,7 +131,7 @@ public function testGetOriginalDataWrappedObjectHasOriginalData() $this->assertSame($data, $identity->getOriginalData()); } - public function testGetProperty() + public function testGetProperty(): void { $data = new Article(['id' => 2]); $auth = $this->createStub(AuthorizationServiceInterface::class); diff --git a/tests/TestCase/Middleware/AuthorizationMiddlewareTest.php b/tests/TestCase/Middleware/AuthorizationMiddlewareTest.php index c7ffeed8..bae1bd52 100644 --- a/tests/TestCase/Middleware/AuthorizationMiddlewareTest.php +++ b/tests/TestCase/Middleware/AuthorizationMiddlewareTest.php @@ -40,11 +40,11 @@ class AuthorizationMiddlewareTest extends TestCase { - public function testInvokeService() + public function testInvokeService(): void { $service = $this->createStub(AuthorizationServiceInterface::class); $request = new ServerRequest(); - $handler = new TestRequestHandler(function ($request) use ($service) { + $handler = new TestRequestHandler(function ($request) use ($service): Response { $this->assertInstanceOf(ServerRequestInterface::class, $request); $this->assertSame($service, $request->getAttribute('authorization')); $this->assertNull($request->getAttribute('identity')); @@ -56,7 +56,7 @@ public function testInvokeService() $middleware->process($request, $handler); } - public function testInvokeAuthorizationRequiredError() + public function testInvokeAuthorizationRequiredError(): void { $this->expectException(AuthorizationRequiredException::class); @@ -78,7 +78,7 @@ public function testInvokeAuthorizationRequiredError() $this->assertSame($service, $request->getAttribute('authorization')); } - public function testInvokeApp() + public function testInvokeApp(): void { $service = $this->createStub(AuthorizationServiceInterface::class); $provider = $this->createMock(AuthorizationServiceProviderInterface::class); @@ -91,7 +91,7 @@ public function testInvokeApp() ->willReturn($service); $request = new ServerRequest(); - $handler = new TestRequestHandler(function ($request) use ($service) { + $handler = new TestRequestHandler(function ($request) use ($service): Response { $this->assertInstanceOf(RequestInterface::class, $request); $this->assertSame($service, $request->getAttribute('authorization')); $this->assertNull($request->getAttribute('identity')); @@ -103,7 +103,7 @@ public function testInvokeApp() $middleware->process($request, $handler); } - public function testInvokeServiceWithIdentity() + public function testInvokeServiceWithIdentity(): void { // phpcs:ignore $identity = new \Authentication\Identity([ @@ -112,7 +112,7 @@ public function testInvokeServiceWithIdentity() $service = $this->createStub(AuthorizationServiceInterface::class); $request = (new ServerRequest())->withAttribute('identity', $identity); - $handler = new TestRequestHandler(function ($request) use ($service) { + $handler = new TestRequestHandler(function ($request) use ($service): Response { $this->assertInstanceOf(RequestInterface::class, $request); $this->assertSame($service, $request->getAttribute('authorization')); $identity = $request->getAttribute('identity'); @@ -128,7 +128,7 @@ public function testInvokeServiceWithIdentity() $middleware->process($request, $handler); } - public function testIdentityInstance() + public function testIdentityInstance(): void { $service = $this->createStub(AuthorizationServiceInterface::class); $identity = new IdentityDecorator($service, [ @@ -136,7 +136,7 @@ public function testIdentityInstance() ]); $request = (new ServerRequest())->withAttribute('identity', $identity); - $handler = new TestRequestHandler(function ($request) use ($service, $identity) { + $handler = new TestRequestHandler(function ($request) use ($service, $identity): Response { $this->assertInstanceOf(RequestInterface::class, $request); $this->assertSame($service, $request->getAttribute('authorization')); $this->assertInstanceOf(IdentityInterface::class, $request->getAttribute('identity')); @@ -149,7 +149,7 @@ public function testIdentityInstance() $middleware->process($request, $handler); } - public function testCustomIdentity() + public function testCustomIdentity(): void { $identity = [ 'id' => 1, @@ -157,7 +157,7 @@ public function testCustomIdentity() $service = $this->createStub(AuthorizationServiceInterface::class); $request = (new ServerRequest())->withAttribute('user', $identity); - $handler = new TestRequestHandler(function ($request) use ($service) { + $handler = new TestRequestHandler(function ($request) use ($service): Response { $this->assertInstanceOf(RequestInterface::class, $request); $this->assertSame($service, $request->getAttribute('authorization')); $this->assertInstanceOf(IdentityInterface::class, $request->getAttribute('user')); @@ -167,7 +167,7 @@ public function testCustomIdentity() }); $middleware = new AuthorizationMiddleware($service, [ - 'identityDecorator' => function ($service, $identity) { + 'identityDecorator' => function ($service, $identity): IdentityDecorator { return new IdentityDecorator($service, $identity); }, 'identityAttribute' => 'user', @@ -177,7 +177,7 @@ public function testCustomIdentity() $middleware->process($request, $handler); } - public function testCustomIdentityDecorator() + public function testCustomIdentityDecorator(): void { $identity = new Identity([ 'id' => 1, @@ -185,7 +185,7 @@ public function testCustomIdentityDecorator() $service = $this->createStub(AuthorizationServiceInterface::class); $request = (new ServerRequest())->withAttribute('identity', $identity); - $handler = new TestRequestHandler(function ($request) use ($service, $identity) { + $handler = new TestRequestHandler(function ($request) use ($service, $identity): Response { $this->assertInstanceOf(RequestInterface::class, $request); $this->assertSame($service, $request->getAttribute('authorization')); $this->assertInstanceOf(IdentityInterface::class, $request->getAttribute('identity')); @@ -206,7 +206,7 @@ public function testCustomIdentityDecorator() $middleware->process($request, $handler); } - public function testInvalidIdentity() + public function testInvalidIdentity(): void { $identity = [ 'id' => 1, @@ -227,11 +227,11 @@ public function testInvalidIdentity() $middleware->process($request, $handler); } - public function testUnauthorizedHandler() + public function testUnauthorizedHandler(): void { $service = $this->createStub(AuthorizationServiceInterface::class); $request = new ServerRequest(); - $handler = new TestRequestHandler(function () { + $handler = new TestRequestHandler(function (): void { throw new Exception(); }); @@ -241,11 +241,11 @@ public function testUnauthorizedHandler() $middleware->process($request, $handler); } - public function testUnauthorizedHandlerSuppress() + public function testUnauthorizedHandlerSuppress(): void { $service = $this->createStub(AuthorizationServiceInterface::class); $request = new ServerRequest(); - $handler = new TestRequestHandler(function () { + $handler = new TestRequestHandler(function (): void { throw new Exception(); }); @@ -258,11 +258,11 @@ public function testUnauthorizedHandlerSuppress() $this->assertSame(200, $result->getStatusCode()); } - public function testUnauthorizedHandlerRequireAuthz() + public function testUnauthorizedHandlerRequireAuthz(): void { $service = $this->createStub(AuthorizationServiceInterface::class); $request = new ServerRequest(); - $handler = new TestRequestHandler(function () { + $handler = new TestRequestHandler(function (): void { throw new Exception(); }); @@ -275,7 +275,7 @@ public function testUnauthorizedHandlerRequireAuthz() $this->assertSame(200, $result->getStatusCode()); } - public function testMiddlewareInjectsServiceIntoDIC() + public function testMiddlewareInjectsServiceIntoDIC(): void { $request = ServerRequestFactory::fromGlobals( ['REQUEST_URI' => '/testpath'], @@ -292,7 +292,7 @@ public function testMiddlewareInjectsServiceIntoDIC() $this->assertInstanceOf(AuthorizationService::class, $container->get(AuthorizationService::class)); } - public function testMiddlewareInjectsServiceIntoDICViaCustomContainerInstance() + public function testMiddlewareInjectsServiceIntoDICViaCustomContainerInstance(): void { $request = ServerRequestFactory::fromGlobals( ['REQUEST_URI' => '/testpath'], diff --git a/tests/TestCase/Middleware/RequestAuthorizationMiddlewareTest.php b/tests/TestCase/Middleware/RequestAuthorizationMiddlewareTest.php index 2fc8057f..88eb8b5d 100644 --- a/tests/TestCase/Middleware/RequestAuthorizationMiddlewareTest.php +++ b/tests/TestCase/Middleware/RequestAuthorizationMiddlewareTest.php @@ -32,7 +32,7 @@ */ class RequestAuthorizationMiddlewareTest extends TestCase { - public function testRuntimeExceptionWhenServiceIsMissing() + public function testRuntimeExceptionWhenServiceIsMissing(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Authorization\Middleware\RequestAuthorizationMiddleware could not find the authorization service in the request attribute. Make sure you added the AuthorizationMiddleware before this middleware or that you somehow else added the service to the requests `authorization` attribute.'); @@ -43,7 +43,7 @@ public function testRuntimeExceptionWhenServiceIsMissing() $middleware->process($request, $handler); } - public function testInvokeService() + public function testInvokeService(): void { $request = (new ServerRequest([ 'url' => '/articles/index', @@ -77,7 +77,7 @@ public function testInvokeService() $middleware->process($request, $handler); } - public function testInvokeServiceWithResult() + public function testInvokeServiceWithResult(): void { $request = (new ServerRequest([ 'url' => '/articles/index', @@ -122,7 +122,7 @@ public function testInvokeServiceWithResult() } } - public function testUnauthorizedHandlerSuppress() + public function testUnauthorizedHandlerSuppress(): void { $request = (new ServerRequest([ 'url' => '/articles/index', diff --git a/tests/TestCase/Middleware/UnauthorizedHandler/CakeRedirectHandlerTest.php b/tests/TestCase/Middleware/UnauthorizedHandler/CakeRedirectHandlerTest.php index 29ac57a9..156f02c6 100644 --- a/tests/TestCase/Middleware/UnauthorizedHandler/CakeRedirectHandlerTest.php +++ b/tests/TestCase/Middleware/UnauthorizedHandler/CakeRedirectHandlerTest.php @@ -25,7 +25,7 @@ class CakeRedirectHandlerTest extends TestCase { - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -39,7 +39,7 @@ public function setUp(): void $builder->connect('/{controller}/{action}'); } - public function testHandleRedirectionDefault() + public function testHandleRedirectionDefault(): void { $handler = new CakeRedirectHandler(); @@ -57,7 +57,7 @@ public function testHandleRedirectionDefault() $this->assertSame('/login?redirect=%2Fadmin%2Fdashboard', $response->getHeaderLine('Location')); } - public function testHandleRedirectionNamed() + public function testHandleRedirectionNamed(): void { $handler = new CakeRedirectHandler(); @@ -79,7 +79,7 @@ public function testHandleRedirectionNamed() $this->assertSame('/login?redirect=%2Fadmin%2Fdashboard', $response->getHeaderLine('Location')); } - public function testHandleRedirectionWithQuery() + public function testHandleRedirectionWithQuery(): void { $handler = new CakeRedirectHandler(); @@ -104,7 +104,7 @@ public function testHandleRedirectionWithQuery() $this->assertSame('/login?foo=bar&redirect=%2F', $response->getHeaderLine('Location')); } - public function testHandleRedirectionNoQuery() + public function testHandleRedirectionNoQuery(): void { $handler = new CakeRedirectHandler(); @@ -124,7 +124,7 @@ public function testHandleRedirectionNoQuery() $this->assertSame('/login', $response->getHeaderLine('Location')); } - public function testHandleRedirectWithBasePath() + public function testHandleRedirectWithBasePath(): void { $handler = new CakeRedirectHandler(); $exception = new Exception(); diff --git a/tests/TestCase/Middleware/UnauthorizedHandler/ExceptionHandlerTest.php b/tests/TestCase/Middleware/UnauthorizedHandler/ExceptionHandlerTest.php index a62b7cc1..dc099e4f 100644 --- a/tests/TestCase/Middleware/UnauthorizedHandler/ExceptionHandlerTest.php +++ b/tests/TestCase/Middleware/UnauthorizedHandler/ExceptionHandlerTest.php @@ -23,7 +23,7 @@ class ExceptionHandlerTest extends TestCase { - public function testHandle() + public function testHandle(): void { $handler = new ExceptionHandler(); diff --git a/tests/TestCase/Middleware/UnauthorizedHandler/HandlerFactoryTest.php b/tests/TestCase/Middleware/UnauthorizedHandler/HandlerFactoryTest.php index 7427dd53..35f5d97c 100644 --- a/tests/TestCase/Middleware/UnauthorizedHandler/HandlerFactoryTest.php +++ b/tests/TestCase/Middleware/UnauthorizedHandler/HandlerFactoryTest.php @@ -23,13 +23,13 @@ class HandlerFactoryTest extends TestCase { - public function testCreate() + public function testCreate(): void { $handler = HandlerFactory::create('Suppress'); $this->assertInstanceOf(SuppressHandler::class, $handler); } - public function testCreateMissing() + public function testCreateMissing(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Handler `Foo` does not exist.'); @@ -37,7 +37,7 @@ public function testCreateMissing() HandlerFactory::create('Foo'); } - public function testCreateInvalid() + public function testCreateInvalid(): void { $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Handler should implement `Authorization\Middleware\UnauthorizedHandler\HandlerInterface`, got `stdClass`.'); diff --git a/tests/TestCase/Middleware/UnauthorizedHandler/RedirectHandlerTest.php b/tests/TestCase/Middleware/UnauthorizedHandler/RedirectHandlerTest.php index 9eda1319..5099c979 100644 --- a/tests/TestCase/Middleware/UnauthorizedHandler/RedirectHandlerTest.php +++ b/tests/TestCase/Middleware/UnauthorizedHandler/RedirectHandlerTest.php @@ -27,7 +27,7 @@ class RedirectHandlerTest extends TestCase { - public function testHandleRedirection() + public function testHandleRedirection(): void { $handler = new RedirectHandler(); @@ -46,7 +46,7 @@ public function testHandleRedirection() $this->assertSame('/login?redirect=%2F', $response->getHeaderLine('Location')); } - public function testHandleRedirectionWithQuery() + public function testHandleRedirectionWithQuery(): void { $handler = new RedirectHandler(); @@ -70,7 +70,7 @@ public function testHandleRedirectionWithQuery() $this->assertSame('/login?foo=bar&redirect=%2Fpath%3Fkey%3Dvalue', $response->getHeaderLine('Location')); } - public function testHandleRedirectionNoQuery() + public function testHandleRedirectionNoQuery(): void { $handler = new RedirectHandler(); @@ -91,7 +91,7 @@ public function testHandleRedirectionNoQuery() $this->assertSame('/users/login', $response->getHeaderLine('Location')); } - public static function httpMethodProvider() + public static function httpMethodProvider(): array { return [ ['POST'], @@ -104,7 +104,7 @@ public static function httpMethodProvider() } #[DataProvider('httpMethodProvider')] - public function testHandleRedirectionIgnoreNonIdempotentMethods($method) + public function testHandleRedirectionIgnoreNonIdempotentMethods(string $method): void { $handler = new RedirectHandler(); @@ -128,7 +128,7 @@ public function testHandleRedirectionIgnoreNonIdempotentMethods($method) $this->assertSame('/login?foo=bar', $response->getHeaderLine('Location')); } - public function testHandleRedirectWithBasePath() + public function testHandleRedirectWithBasePath(): void { $handler = new RedirectHandler(); $exception = new Exception(); @@ -152,7 +152,7 @@ public function testHandleRedirectWithBasePath() ); } - public function testHandleException() + public function testHandleException(): void { $handler = new RedirectHandler(); diff --git a/tests/TestCase/Policy/MapResolverTest.php b/tests/TestCase/Policy/MapResolverTest.php index 1e7a10f3..abbf2f57 100644 --- a/tests/TestCase/Policy/MapResolverTest.php +++ b/tests/TestCase/Policy/MapResolverTest.php @@ -29,7 +29,7 @@ class MapResolverTest extends TestCase { - public function testGetPolicyClassName() + public function testGetPolicyClassName(): void { $resolver = new MapResolver(); @@ -39,7 +39,7 @@ public function testGetPolicyClassName() $this->assertInstanceOf(ArticlePolicy::class, $result); } - public function testGetPolicyObject() + public function testGetPolicyObject(): void { $resolver = new MapResolver(); $policy = new ArticlePolicy(); @@ -50,13 +50,13 @@ public function testGetPolicyObject() $this->assertSame($policy, $result); } - public function testGetPolicyCallable() + public function testGetPolicyCallable(): void { $resolver = new MapResolver(); $resource = new Article(); $policy = new ArticlePolicy(); - $resolver->map(Article::class, function ($arg1, $arg2) use ($policy, $resolver, $resource) { + $resolver->map(Article::class, function ($arg1, $arg2) use ($policy, $resolver, $resource): ArticlePolicy { $this->assertSame($resource, $arg1); $this->assertSame($resolver, $arg2); @@ -67,7 +67,7 @@ public function testGetPolicyCallable() $this->assertSame($policy, $result); } - public function testMapMissingResource() + public function testMapMissingResource(): void { $resolver = new MapResolver(); @@ -77,7 +77,7 @@ public function testMapMissingResource() $resolver->map('Foo', 'Bar'); } - public function testMapMissingPolicy() + public function testMapMissingPolicy(): void { $resolver = new MapResolver(); @@ -87,7 +87,7 @@ public function testMapMissingPolicy() $resolver->map(Article::class, 'Foo'); } - public function testGetPolicyPrimitive() + public function testGetPolicyPrimitive(): void { $resolver = new MapResolver(); @@ -97,7 +97,7 @@ public function testGetPolicyPrimitive() $resolver->getPolicy('Foo'); } - public function testGetPolicyMissing() + public function testGetPolicyMissing(): void { $resolver = new MapResolver(); @@ -107,7 +107,7 @@ public function testGetPolicyMissing() $resolver->getPolicy(new Article()); } - public function testGetPolicyViaDIC() + public function testGetPolicyViaDIC(): void { $container = new Container(); $container->add(TestService::class); diff --git a/tests/TestCase/Policy/OrmResolverTest.php b/tests/TestCase/Policy/OrmResolverTest.php index b3801d0c..b8d19c7e 100644 --- a/tests/TestCase/Policy/OrmResolverTest.php +++ b/tests/TestCase/Policy/OrmResolverTest.php @@ -21,6 +21,7 @@ use Authorization\Policy\Exception\MissingPolicyException; use Authorization\Policy\OrmResolver; use Cake\Core\Container; +use Cake\Datasource\RepositoryInterface; use Cake\ORM\Entity; use Cake\ORM\Locator\LocatorAwareTrait; use Cake\TestSuite\TestCase; @@ -41,7 +42,7 @@ class OrmResolverTest extends TestCase protected array $fixtures = ['plugin.Authorization.Articles']; - public function testGetPolicyUnknownObject() + public function testGetPolicyUnknownObject(): void { $this->expectException(MissingPolicyException::class); @@ -50,7 +51,7 @@ public function testGetPolicyUnknownObject() $resolver->getPolicy($entity); } - public function testGetPolicyUnknownEntity() + public function testGetPolicyUnknownEntity(): void { $this->expectException(MissingPolicyException::class); @@ -59,7 +60,7 @@ public function testGetPolicyUnknownEntity() $resolver->getPolicy($entity); } - public function testGetPolicyDefinedEntity() + public function testGetPolicyDefinedEntity(): void { $article = new Article(); $resolver = new OrmResolver('TestApp'); @@ -67,7 +68,7 @@ public function testGetPolicyDefinedEntity() $this->assertInstanceOf(ArticlePolicy::class, $policy); } - public function testGetPolicyDefinedPluginEntityAppOveride() + public function testGetPolicyDefinedPluginEntityAppOveride(): void { $bookmark = new Bookmark(); $resolver = new OrmResolver('TestApp'); @@ -76,7 +77,7 @@ public function testGetPolicyDefinedPluginEntityAppOveride() $this->assertStringContainsString('TestApp\Policy\TestPlugin', BookmarkPolicy::class, 'class has moved'); } - public function testGetPolicyDefinedPluginEntityPluginOveride() + public function testGetPolicyDefinedPluginEntityPluginOveride(): void { $bookmark = new Tag(); $resolver = new OrmResolver('TestApp', [ @@ -89,7 +90,7 @@ public function testGetPolicyDefinedPluginEntityPluginOveride() $this->assertStringNotContainsString('TestPlugin', OverrideTagPolicy::class, 'class has moved'); } - public function testGetPolicyDefinedPluginEntity() + public function testGetPolicyDefinedPluginEntity(): void { $bookmark = new Tag(); $resolver = new OrmResolver('TestApp'); @@ -99,7 +100,7 @@ public function testGetPolicyDefinedPluginEntity() $this->assertStringNotContainsString('TestApp', TagPolicy::class, 'class has moved'); } - public function testGetPolicyDefinedTable() + public function testGetPolicyDefinedTable(): void { $articles = $this->fetchTable('Articles'); $resolver = new OrmResolver('TestApp'); @@ -107,7 +108,7 @@ public function testGetPolicyDefinedTable() $this->assertInstanceOf(ArticlesTablePolicy::class, $policy); } - public function testGetPolicyQueryForDefinedTable() + public function testGetPolicyQueryForDefinedTable(): void { $articles = $this->fetchTable('Articles'); $resolver = new OrmResolver('TestApp'); @@ -115,16 +116,16 @@ public function testGetPolicyQueryForDefinedTable() $this->assertInstanceOf(ArticlesTablePolicy::class, $policy); } - public function testGetPolicyUnknownTable() + public function testGetPolicyUnknownTable(): void { $this->expectException(MissingPolicyException::class); - $articles = $this->createStub('Cake\Datasource\RepositoryInterface'); + $articles = $this->createStub(RepositoryInterface::class); $resolver = new OrmResolver('TestApp'); $resolver->getPolicy($articles); } - public function testGetPolicyViaDIC() + public function testGetPolicyViaDIC(): void { $container = new Container(); $container->add(TestService::class); diff --git a/tests/TestCase/Policy/ResolverCollectionTest.php b/tests/TestCase/Policy/ResolverCollectionTest.php index 4991f95a..28dc9957 100644 --- a/tests/TestCase/Policy/ResolverCollectionTest.php +++ b/tests/TestCase/Policy/ResolverCollectionTest.php @@ -26,7 +26,7 @@ class ResolverCollectionTest extends TestCase { - public function testEmptyCollection() + public function testEmptyCollection(): void { $collection = new ResolverCollection(); @@ -35,7 +35,7 @@ public function testEmptyCollection() $collection->getPolicy(new Article()); } - public function testMissingPolicy() + public function testMissingPolicy(): void { $resource = new Article(); @@ -54,7 +54,7 @@ public function testMissingPolicy() $collection->getPolicy($resource); } - public function testGetPolicy() + public function testGetPolicy(): void { $resource = new Article(); $policy = new ArticlePolicy(); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7b5758cf..bf47189c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -21,10 +21,10 @@ use Cake\TestSuite\Fixture\SchemaLoader; use function Cake\Core\env; -$findRoot = function ($root) { +$findRoot = function ($root): string { do { $lastRoot = $root; - $root = dirname($root); + $root = dirname((string)$root); if (is_dir($root . '/vendor/cakephp/cakephp')) { return $root; } @@ -35,7 +35,7 @@ unset($findRoot); chdir($root); -require_once 'vendor/autoload.php'; +require_once __DIR__ . '/../vendor/autoload.php'; define('ROOT', $root . DS . 'tests' . DS . 'test_app' . DS); define('APP', ROOT . 'TestApp' . DS); diff --git a/tests/test_app/TestApp/Http/TestRequestHandler.php b/tests/test_app/TestApp/Http/TestRequestHandler.php index e01bf810..1b0ed05b 100644 --- a/tests/test_app/TestApp/Http/TestRequestHandler.php +++ b/tests/test_app/TestApp/Http/TestRequestHandler.php @@ -21,13 +21,16 @@ class TestRequestHandler implements RequestHandlerInterface { + /** + * @var callable + */ public $callable; public $request; public function __construct(?callable $callable = null) { - $this->callable = $callable ?: function ($request) { + $this->callable = $callable ?: function ($request): Response { return new Response(); }; } diff --git a/tests/test_app/TestApp/Identity.php b/tests/test_app/TestApp/Identity.php index b332237f..4434c4dc 100644 --- a/tests/test_app/TestApp/Identity.php +++ b/tests/test_app/TestApp/Identity.php @@ -13,12 +13,12 @@ public function __construct($identity) $this->identity = $identity; } - public function setService(AuthorizationServiceInterface $service) + public function setService(AuthorizationServiceInterface $service): void { $this->authorization = $service; } - public function getService() + public function getService(): AuthorizationServiceInterface { return $this->authorization; } diff --git a/tests/test_app/TestApp/Policy/ArticlePolicy.php b/tests/test_app/TestApp/Policy/ArticlePolicy.php index cc5b5a68..d5e219ab 100644 --- a/tests/test_app/TestApp/Policy/ArticlePolicy.php +++ b/tests/test_app/TestApp/Policy/ArticlePolicy.php @@ -12,8 +12,6 @@ class ArticlePolicy { /** * A service class injected via DIC - * - * @var \TestApp\Service\TestService|null */ protected ?TestService $service; @@ -28,7 +26,7 @@ public function __construct(?TestService $service = null) * @param \Authorization\IdentityInterface $user * @return bool */ - public function canAdd($user) + public function canAdd($user): bool { return in_array($user['role'], ['admin', 'author']); } @@ -37,7 +35,6 @@ public function canAdd($user) * Edit articles if you're an admin or author * * @param \Authorization\IdentityInterface $user - * @param \TestApp\Model\Entity\Article $article * @return bool */ public function canEdit($user, Article $article) @@ -57,7 +54,6 @@ public function canEdit($user, Article $article) * Modify articles if you're an admin or author * * @param \Authorization\IdentityInterface $user - * @param \TestApp\Model\Entity\Article $article * @return bool */ public function canModify($user, Article $article) @@ -73,7 +69,6 @@ public function canModify($user, Article $article) * Delete only own articles or any if you're an admin * * @param \Authorization\IdentityInterface $user - * @param \TestApp\Model\Entity\Article $article * @return bool */ public function canDelete($user, Article $article) @@ -89,10 +84,9 @@ public function canDelete($user, Article $article) * Scope method for index * * @param \Authorization\IdentityInterface $user - * @param \TestApp\Model\Entity\Article $article * @return bool */ - public function scopeIndex($user, Article $article) + public function scopeIndex($user, Article $article): Article { $article->user_id = $user->getOriginalData()['id']; @@ -105,10 +99,9 @@ public function scopeIndex($user, Article $article) * This test "null" user cases * * @param \Authorization\IdentityInterface|null $user - * @param \TestApp\Model\Entity\Article $article * @return bool */ - public function canView($user, Article $article) + public function canView($user, Article $article): bool { if ($article->get('visibility') !== 'public' && empty($user)) { return false; @@ -123,10 +116,9 @@ public function canView($user, Article $article) * This tests Result objects. * * @param \Authorization\IdentityInterface|null $user - * @param Article $article * @return Result */ - public function canPublish($user, Article $article) + public function canPublish($user, Article $article): Result { if ($article->get('visibility') === 'public') { return new Result(false, 'public'); @@ -140,7 +132,7 @@ public function canWithService($user, Article $article, Closure $service) return $service(); } - public function canWithMultipleServices($user, Article $article, Closure $service1, Closure $service2) + public function canWithMultipleServices($user, Article $article, Closure $service1, Closure $service2): bool { return $service1() && $service2(); } diff --git a/tests/test_app/TestApp/Policy/ArticlesTablePolicy.php b/tests/test_app/TestApp/Policy/ArticlesTablePolicy.php index 7eaf7b6c..66f723da 100644 --- a/tests/test_app/TestApp/Policy/ArticlesTablePolicy.php +++ b/tests/test_app/TestApp/Policy/ArticlesTablePolicy.php @@ -25,7 +25,7 @@ public function canModify(IdentityInterface $identity) public function scopeEdit(?IdentityInterface $user, QueryInterface $query) { - if ($user === null) { + if (!$user instanceof IdentityInterface) { return $query->where([ 'visibility' => 'public', ]); diff --git a/tests/test_app/TestApp/Policy/MagicCallPolicy.php b/tests/test_app/TestApp/Policy/MagicCallPolicy.php index 4e1a4607..cbe69b24 100644 --- a/tests/test_app/TestApp/Policy/MagicCallPolicy.php +++ b/tests/test_app/TestApp/Policy/MagicCallPolicy.php @@ -15,16 +15,12 @@ class MagicCallPolicy * @param array $arguments Arguments * @return mixed */ - public function __call($name, $arguments) + public function __call(string $name, array $arguments) { if ($name === 'canDoThat') { return true; } - if ($name === 'scopeThis') { - return true; - } - - return false; + return $name === 'scopeThis'; } } diff --git a/tests/test_app/TestApp/Policy/RequestPolicy.php b/tests/test_app/TestApp/Policy/RequestPolicy.php index 5d5a06ff..1f9b628f 100644 --- a/tests/test_app/TestApp/Policy/RequestPolicy.php +++ b/tests/test_app/TestApp/Policy/RequestPolicy.php @@ -23,16 +23,9 @@ class RequestPolicy implements RequestPolicyInterface */ public function canAccess(?IdentityInterface $identity, ServerRequest $request): ResultInterface|bool { - if ( - $request->getParam('controller') === 'Articles' - && $request->getParam('action') === 'index' - ) { - return true; - } - // More checks here - - return false; + return $request->getParam('controller') === 'Articles' + && $request->getParam('action') === 'index'; } /** @@ -42,7 +35,7 @@ public function canAccess(?IdentityInterface $identity, ServerRequest $request): * @param \Cake\Http\ServerRequest $request Request * @return \Authorization\Policy\ResultInterface|bool */ - public function canEnter(?IdentityInterface $identity, ServerRequest $request) + public function canEnter(?IdentityInterface $identity, ServerRequest $request): Result { if ( $request->getParam('controller') === 'Articles'