Skip to content

Commit c2e1a2e

Browse files
authored
IBX-10507: Allowed nullable type declarations for method parameters across the codebase (#199)
1 parent 859941a commit c2e1a2e

File tree

12 files changed

+36
-108
lines changed

12 files changed

+36
-108
lines changed

phpstan-baseline-7.4.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ parameters:
5454
count: 4
5555
path: src/lib/Server/Controller/User.php
5656

57-
-
58-
message: '#^Parameter \#1 \$message of method Psr\\Log\\LoggerInterface\:\:error\(\) expects string, Symfony\\Component\\Security\\Core\\User\\UserInterface\|null given\.$#'
59-
identifier: argument.type
60-
count: 1
61-
path: src/lib/Server/Security/RestAuthenticator.php
62-
6357
-
6458
message: '#^Parameter \#1 \$str of function base64_encode expects string, string\|false given\.$#'
6559
identifier: argument.type

phpstan-baseline-gte-8.0.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ parameters:
6666
count: 4
6767
path: src/lib/Server/Controller/User.php
6868

69-
-
70-
message: '#^Parameter \#1 \$message of method Psr\\Log\\LoggerInterface\:\:error\(\) expects string\|Stringable, Symfony\\Component\\Security\\Core\\User\\UserInterface\|null given\.$#'
71-
identifier: argument.type
72-
count: 1
73-
path: src/lib/Server/Security/RestAuthenticator.php
7469

7570
-
7671
message: '#^Parameter \#1 \$string of function base64_encode expects string, string\|false given\.$#'

phpstan-baseline.neon

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,6 @@ parameters:
150150
count: 1
151151
path: src/bundle/DependencyInjection/Security/RestSessionBasedFactory.php
152152

153-
-
154-
message: '#^Cannot call method isTokenValid\(\) on Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface\|null\.$#'
155-
identifier: method.nonObject
156-
count: 1
157-
path: src/bundle/EventListener/CsrfListener.php
158-
159153
-
160154
message: '#^Method Ibexa\\Bundle\\Rest\\EventListener\\CsrfListener\:\:getSubscribedEvents\(\) return type has no value type specified in iterable type array\.$#'
161155
identifier: missingType.iterableValue
@@ -2130,24 +2124,6 @@ parameters:
21302124
count: 1
21312125
path: src/lib/Server/Controller/SessionController.php
21322126

2133-
-
2134-
message: '#^Property Ibexa\\Rest\\Server\\Controller\\SessionController\:\:\$csrfTokenManager \(Ibexa\\Rest\\Server\\Security\\CsrfTokenManager\) does not accept Ibexa\\Rest\\Server\\Security\\CsrfTokenManager\|null\.$#'
2135-
identifier: assign.propertyType
2136-
count: 1
2137-
path: src/lib/Server/Controller/SessionController.php
2138-
2139-
-
2140-
message: '#^Property Ibexa\\Rest\\Server\\Controller\\SessionController\:\:\$csrfTokenStorage \(Symfony\\Component\\Security\\Csrf\\TokenStorage\\TokenStorageInterface\) does not accept Symfony\\Component\\Security\\Csrf\\TokenStorage\\TokenStorageInterface\|null\.$#'
2141-
identifier: assign.propertyType
2142-
count: 1
2143-
path: src/lib/Server/Controller/SessionController.php
2144-
2145-
-
2146-
message: '#^Property Ibexa\\Rest\\Server\\Controller\\SessionController\:\:\$csrfTokenStorage is never read, only written\.$#'
2147-
identifier: property.onlyWritten
2148-
count: 1
2149-
path: src/lib/Server/Controller/SessionController.php
2150-
21512127
-
21522128
message: '#^Method Ibexa\\Rest\\Server\\Controller\\Trash\:\:deleteTrashItem\(\) has parameter \$trashItemId with no type specified\.$#'
21532129
identifier: missingType.parameter
@@ -4392,12 +4368,6 @@ parameters:
43924368
count: 1
43934369
path: src/lib/Server/Security/RestAuthenticator.php
43944370

4395-
-
4396-
message: '#^Ternary operator condition is always true\.$#'
4397-
identifier: ternary.alwaysTrue
4398-
count: 1
4399-
path: src/lib/Server/Security/RestAuthenticator.php
4400-
44014371
-
44024372
message: '#^Method Ibexa\\Rest\\Server\\Security\\RestLogoutHandler\:\:logout\(\) has no return type specified\.$#'
44034373
identifier: missingType.return
@@ -4506,24 +4476,12 @@ parameters:
45064476
count: 1
45074477
path: src/lib/Server/Values/RestContent.php
45084478

4509-
-
4510-
message: '#^Property Ibexa\\Rest\\Server\\Values\\RestContentType\:\:\$fieldDefinitions \(array\<Ibexa\\Contracts\\Core\\Repository\\Values\\ContentType\\FieldDefinition\>\) does not accept array\<Ibexa\\Contracts\\Core\\Repository\\Values\\ContentType\\FieldDefinition\>\|null\.$#'
4511-
identifier: assign.propertyType
4512-
count: 1
4513-
path: src/lib/Server/Values/RestContentType.php
4514-
45154479
-
45164480
message: '#^Property Ibexa\\Rest\\Server\\Values\\RestLocationUpdateStruct\:\:\$hidden \(bool\) does not accept bool\|null\.$#'
45174481
identifier: assign.propertyType
45184482
count: 1
45194483
path: src/lib/Server/Values/RestLocationUpdateStruct.php
45204484

4521-
-
4522-
message: '#^Property Ibexa\\Rest\\Server\\Values\\RoleAssignment\:\:\$limitation \(Ibexa\\Contracts\\Core\\Repository\\Values\\User\\Limitation\\RoleLimitation\) does not accept Ibexa\\Contracts\\Core\\Repository\\Values\\User\\Limitation\\RoleLimitation\|null\.$#'
4523-
identifier: assign.propertyType
4524-
count: 1
4525-
path: src/lib/Server/Values/RoleAssignment.php
4526-
45274485
-
45284486
message: '#^Access to an undefined property Ibexa\\Rest\\Server\\Values\\SeeOther\:\:\$redirectUri\.$#'
45294487
identifier: property.notFound

src/bundle/EventListener/CsrfListener.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(
5656
EventDispatcherInterface $eventDispatcher,
5757
$csrfEnabled,
5858
$csrfTokenIntention,
59-
CsrfTokenManagerInterface $csrfTokenManager = null
59+
?CsrfTokenManagerInterface $csrfTokenManager = null
6060
) {
6161
$this->eventDispatcher = $eventDispatcher;
6262
$this->csrfEnabled = $csrfEnabled;
@@ -167,6 +167,10 @@ protected function isSessionRoute($route)
167167
*/
168168
protected function checkCsrfToken(Request $request)
169169
{
170+
if ($this->csrfTokenManager === null) {
171+
return false;
172+
}
173+
170174
if (!$request->headers->has(self::CSRF_TOKEN_HEADER)) {
171175
return false;
172176
}

src/contracts/Security/AuthorizationHeaderRESTRequestMatcher.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ final class AuthorizationHeaderRESTRequestMatcher extends RequestMatcher
1717

1818
public function __construct(
1919
?string $headerName = null,
20-
string $path = null,
21-
string $host = null,
20+
?string $path = null,
21+
?string $host = null,
2222
$methods = null,
2323
$ips = null,
2424
array $attributes = [],
2525
$schemes = null,
26-
int $port = null
26+
?int $port = null
2727
) {
2828
parent::__construct($path, $host, $methods, $ips, $attributes, $schemes, $port);
2929
$this->headerName = $headerName;

src/lib/Input/Handler/Xml.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Ibexa\Contracts\Rest\Exceptions;
1010
use Ibexa\Contracts\Rest\Input\Handler;
11+
use InvalidArgumentException;
1112

1213
/**
1314
* Input format handler base class.
@@ -150,6 +151,9 @@ protected function convertDom(\DOMNode $node)
150151
];
151152
$isArray = true;
152153
} elseif (!is_string($current[$tagName])) {
154+
if (!is_array($current[$tagName])) {
155+
throw new InvalidArgumentException('Current tag name is not an array as expected.');
156+
}
153157
$current[$tagName][] = $this->convertDom($childNode);
154158
}
155159

src/lib/Server/Controller/SessionController.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,31 @@
1919
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
2020
use Symfony\Component\Security\Core\Exception\AuthenticationException;
2121
use Symfony\Component\Security\Csrf\CsrfToken;
22-
use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;
2322

2423
class SessionController extends Controller
2524
{
26-
/** @var \Ibexa\Core\MVC\Symfony\Security\Authentication\AuthenticatorInterface|null */
27-
private $authenticator;
25+
private ?AuthenticatorInterface $authenticator;
2826

29-
/** @var \Ibexa\Rest\Server\Security\CsrfTokenManager */
30-
private $csrfTokenManager;
27+
private ?CsrfTokenManager $csrfTokenManager;
3128

32-
/** @var string */
33-
private $csrfTokenIntention;
29+
private string $csrfTokenIntention;
3430

35-
/** @var \Ibexa\Contracts\Core\Repository\PermissionResolver */
36-
private $permissionResolver;
31+
private PermissionResolver $permissionResolver;
3732

38-
/** @var \Ibexa\Contracts\Core\Repository\UserService */
39-
private $userService;
40-
41-
/** @var \Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface */
42-
private $csrfTokenStorage;
33+
private UserService $userService;
4334

4435
public function __construct(
4536
$tokenIntention,
4637
PermissionResolver $permissionResolver,
4738
UserService $userService,
4839
?AuthenticatorInterface $authenticator = null,
49-
CsrfTokenManager $csrfTokenManager = null,
50-
TokenStorageInterface $csrfTokenStorage = null
40+
?CsrfTokenManager $csrfTokenManager = null
5141
) {
5242
$this->authenticator = $authenticator;
5343
$this->csrfTokenIntention = $tokenIntention;
5444
$this->csrfTokenManager = $csrfTokenManager;
5545
$this->permissionResolver = $permissionResolver;
5646
$this->userService = $userService;
57-
$this->csrfTokenStorage = $csrfTokenStorage;
5847
}
5948

6049
/**

src/lib/Server/Security/CsrfTokenManager.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class CsrfTokenManager extends BaseCsrfTokenManager
2525
private $namespace;
2626

2727
public function __construct(
28-
TokenGeneratorInterface $generator = null,
29-
TokenStorageInterface $storage = null,
30-
RequestStack $requestStack = null
28+
?TokenGeneratorInterface $generator = null,
29+
?TokenStorageInterface $storage = null,
30+
?RequestStack $requestStack = null
3131
) {
3232
$this->storage = $storage ?: new NativeSessionTokenStorage();
3333
$this->namespace = $this->resolveNamespace($requestStack);
@@ -54,7 +54,7 @@ public function hasToken($tokenId)
5454
*
5555
* @return string
5656
*/
57-
private function resolveNamespace(RequestStack $requestStack = null)
57+
private function resolveNamespace(?RequestStack $requestStack = null)
5858
{
5959
if ($requestStack !== null && ($request = $requestStack->getMainRequest())) {
6060
return $request->isSecure() ? 'https-' : '';

src/lib/Server/Security/RestAuthenticator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function __construct(
7575
$providerKey,
7676
EventDispatcherInterface $dispatcher,
7777
ConfigResolverInterface $configResolver,
78-
LoggerInterface $logger = null
78+
?LoggerInterface $logger = null
7979
) {
8080
$this->tokenStorage = $tokenStorage;
8181
$this->authenticationManager = $authenticationManager;
@@ -128,7 +128,7 @@ public function authenticate(Request $request)
128128
$user = $token->getUser();
129129
if (!$user instanceof IbexaUser) {
130130
if ($this->logger) {
131-
$this->logger->error('REST: Authenticated user must be Ibexa\\Core\\MVC\\Symfony\\Security\\User, got ' . is_string($user) ? $user : get_class($user));
131+
$this->logger->error('REST: Authenticated user must be Ibexa\\Core\\MVC\\Symfony\\Security\\User, got ' . (is_string($user) ? $user : get_class($user)));
132132
}
133133

134134
$e = new InvalidUserTypeException('Authenticated user is not an Ibexa User.');
@@ -169,7 +169,7 @@ private function attemptAuthentication(Request $request)
169169
*
170170
* @return bool
171171
*/
172-
private function isUserConflict(IbexaUser $user, TokenInterface $previousToken = null)
172+
private function isUserConflict(IbexaUser $user, ?TokenInterface $previousToken = null)
173173
{
174174
if ($previousToken === null || !$previousToken instanceof UsernamePasswordToken) {
175175
return false;

src/lib/Server/Values/RestContent.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ class RestContent extends RestValue
6363
*/
6464
public function __construct(
6565
ContentInfo $contentInfo,
66-
Location $mainLocation = null,
67-
Content $currentVersion = null,
68-
ContentType $contentType = null,
69-
array $relations = null,
66+
?Location $mainLocation = null,
67+
?Content $currentVersion = null,
68+
?ContentType $contentType = null,
69+
?array $relations = null,
7070
$path = null
7171
) {
7272
$this->contentInfo = $contentInfo;

0 commit comments

Comments
 (0)