Skip to content

Commit cfcaf01

Browse files
authored
Merge pull request #320 from cakephp/phpstan
Improve type declarations
2 parents 880c07b + 7e36cf0 commit cfcaf01

15 files changed

+34
-66
lines changed

.phive/phars.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phive xmlns="https://phar.io/phive">
3-
<phar name="phpstan" version="2.1.33" installed="2.1.33" location="./tools/phpstan" copy="false"/>
3+
<phar name="phpstan" version="2.1.40" installed="2.1.40" location="./tools/phpstan" copy="false"/>
44
</phive>

phpstan-baseline.neon

Lines changed: 0 additions & 31 deletions
This file was deleted.

phpstan.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
includes:
2-
- phpstan-baseline.neon
3-
41
parameters:
52
level: 8
63
treatPhpDocTypesAsCertain: false
74
paths:
85
- src/
9-
ignoreErrors:
10-
- identifier: missingType.generics
11-
- identifier: missingType.iterableValue

src/AuthorizationService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ protected function getCanHandler(mixed $policy, string $action): Closure
147147
new MissingMethodException([$method, $action, get_class($policy)]),
148148
);
149149

150+
/** @phpstan-ignore callable.nonCallable */
150151
return [$policy, $method](...);
151152
}
152153

@@ -167,6 +168,7 @@ protected function getScopeHandler(mixed $policy, string $action): Closure
167168
new MissingMethodException([$method, $action, get_class($policy)]),
168169
);
169170

171+
/** @phpstan-ignore callable.nonCallable */
170172
return [$policy, $method](...);
171173
}
172174

src/Controller/Component/AuthorizationComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function canResult(mixed $resource, ?string $action = null): ResultInterf
118118
* @param mixed $resource The resource to check authorization on.
119119
* @param string|null $action The action to check authorization for.
120120
* @param string $method The method to use, either "can" or "canResult".
121-
* @return \Authorization\Policy\ResultInterface|bool
121+
* @return ($method is 'can' ? bool : \Authorization\Policy\ResultInterface)
122122
*/
123123
protected function performCheck(
124124
mixed $resource,

src/Exception/ForbiddenException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ForbiddenException extends Exception
4242
* Constructor
4343
*
4444
* @param \Authorization\Policy\ResultInterface|null $result Policy check result.
45-
* @param array|string $message Either the string of the error message, or an array of attributes
45+
* @param array<string>|string $message Either the string of the error message, or an array of attributes
4646
* that are made available in the view, and sprintf()'d into Exception::$_messageTemplate
4747
* @param int|null $code The code of the error, is also the HTTP status code for the error.
4848
* @param \Throwable|null $previous the previous exception.

src/Identity.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525

2626
class Identity extends IdentityDecorator implements AuthenIdentityInterface
2727
{
28-
/**
29-
* Identity data
30-
*
31-
* @var \Authentication\IdentityInterface
32-
*/
33-
protected ArrayAccess|array $identity;
34-
3528
/**
3629
* Constructor
3730
*
@@ -51,6 +44,8 @@ public function __construct(AuthorizationServiceInterface $service, AuthenIdenti
5144
*/
5245
public function getIdentifier(): string|int|array|null
5346
{
47+
assert($this->identity instanceof AuthenIdentityInterface);
48+
5449
return $this->identity->getIdentifier();
5550
}
5651

@@ -59,6 +54,8 @@ public function getIdentifier(): string|int|array|null
5954
*/
6055
public function getOriginalData(): ArrayAccess|array
6156
{
57+
assert($this->identity instanceof AuthenIdentityInterface);
58+
6259
return $this->identity->getOriginalData();
6360
}
6461
}

src/IdentityDecorator.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class IdentityDecorator implements IdentityInterface
3333
/**
3434
* Identity data
3535
*
36-
* @var \ArrayAccess|array
36+
* @var \ArrayAccess<string, mixed>|array<string, mixed>
3737
*/
3838
protected ArrayAccess|array $identity;
3939

@@ -48,7 +48,7 @@ class IdentityDecorator implements IdentityInterface
4848
* Constructor
4949
*
5050
* @param \Authorization\AuthorizationServiceInterface $service The authorization service.
51-
* @param \ArrayAccess|array $identity Identity data
51+
* @param \ArrayAccess<string, mixed>|array<string, mixed> $identity Identity data
5252
*/
5353
public function __construct(AuthorizationServiceInterface $service, ArrayAccess|array $identity)
5454
{
@@ -86,8 +86,7 @@ public function applyScope(string $action, mixed $resource, mixed ...$optionalAr
8686
public function getOriginalData(): ArrayAccess|array
8787
{
8888
if (
89-
$this->identity
90-
&& !is_array($this->identity)
89+
is_object($this->identity)
9190
&& method_exists($this->identity, 'getOriginalData')
9291
) {
9392
return $this->identity->getOriginalData();
@@ -100,18 +99,25 @@ public function getOriginalData(): ArrayAccess|array
10099
* Delegate unknown methods to decorated identity.
101100
*
102101
* @param string $method The method being invoked.
103-
* @param array $args The arguments for the method.
102+
* @param array<mixed> $args The arguments for the method.
104103
* @return mixed
105104
*/
106105
public function __call(string $method, array $args): mixed
107106
{
108107
if (!is_object($this->identity)) {
109108
throw new BadMethodCallException("Cannot call `{$method}`. Identity data is not an object.");
110109
}
111-
$call = [$this->identity, $method];
110+
111+
if (!method_exists($this->identity, $method)) {
112+
throw new BadMethodCallException(sprintf(
113+
'Method `%s` does not exist on `%s`.',
114+
$method,
115+
$this->identity::class,
116+
));
117+
}
112118

113119
/** @phpstan-ignore callable.nonCallable */
114-
return $call(...$args);
120+
return [$this->identity, $method](...$args);
115121
}
116122

117123
/**

src/IdentityInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* and uses ArrayAccess to expose public properties of the wrapped identity
2727
* implementation.
2828
*
29-
* @extends \ArrayAccess<array-key, mixed>
29+
* @extends \ArrayAccess<string, mixed>
3030
*/
3131
interface IdentityInterface extends ArrayAccess
3232
{
@@ -64,7 +64,7 @@ public function applyScope(string $action, mixed $resource, mixed ...$optionalAr
6464
* If the decorated identity implements `getOriginalData()`
6565
* that method should be invoked to expose the original data.
6666
*
67-
* @return \ArrayAccess|array
67+
* @return \ArrayAccess<string, mixed>|array<string, mixed>
6868
*/
6969
public function getOriginalData(): ArrayAccess|array;
7070
}

src/Middleware/AuthorizationMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class AuthorizationMiddleware implements MiddlewareInterface
5858
* check authorization. It is intended as a development aid and not to be relied upon
5959
* in production. Defaults to `true`.
6060
*
61-
* @var array
61+
* @var array<string, mixed>
6262
*/
6363
protected array $_defaultConfig = [
6464
'identityDecorator' => null,
@@ -179,7 +179,7 @@ protected function getAuthorizationService(
179179
* Builds the identity object.
180180
*
181181
* @param \Authorization\AuthorizationServiceInterface $service Authorization service.
182-
* @param \ArrayAccess|array $identity Identity data
182+
* @param \ArrayAccess<string, mixed>|array<string, mixed> $identity Identity data
183183
* @return \Authorization\IdentityInterface
184184
*/
185185
protected function buildIdentity(

0 commit comments

Comments
 (0)