Skip to content

Commit

Permalink
Fixed errors reported by phpstan (#94)
Browse files Browse the repository at this point in the history
* Fixed errors reported by phpstan

* Used Symfony\Component\Security\Core\Security in UserHelper
  • Loading branch information
ciastektk authored Dec 3, 2021
1 parent 7963fe8 commit 37a8d52
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 115 deletions.
85 changes: 0 additions & 85 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ parameters:
count: 1
path: src/bundle/Command/UserAttributesUpdateCommand.php

-
message: "#^Method EzSystems\\\\EzRecommendationClientBundle\\\\Controller\\\\ContentController\\:\\:getQuery\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/bundle/Controller/ContentController.php

-
message: "#^PHPDoc tag @param has invalid value \\(\\\\Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag ParameterBag \\$parameterBag\\)\\: Unexpected token \"ParameterBag\", expected variable at offset 65$#"
count: 1
Expand All @@ -50,11 +45,6 @@ parameters:
count: 1
path: src/bundle/Controller/ContentController.php

-
message: "#^Method EzSystems\\\\EzRecommendationClientBundle\\\\Controller\\\\ContentTypeController\\:\\:getQuery\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/bundle/Controller/ContentTypeController.php

-
message: "#^Method EzSystems\\\\EzRecommendationClientBundle\\\\Controller\\\\ContentTypeController\\:\\:prepareContentByContentTypeIds\\(\\) has parameter \\$contentTypeIds with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -375,16 +365,6 @@ parameters:
count: 1
path: src/lib/Event/Listener/LoginListener.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Event\\\\RecommendationResponseEvent\\:\\:__construct\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Event/RecommendationResponseEvent.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Event\\\\RecommendationResponseEvent\\:\\:getParameterBag\\(\\) return type has no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Event/RecommendationResponseEvent.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Event\\\\RecommendationResponseEvent\\:\\:getParameterBag\\(\\) should return Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag but returns Psr\\\\Http\\\\Message\\\\ResponseInterface\\|null\\.$#"
count: 1
Expand Down Expand Up @@ -425,11 +405,6 @@ parameters:
count: 1
path: src/lib/Event/Subscriber/RecommendationEventSubscriber.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Event\\\\Subscriber\\\\RecommendationEventSubscriber\\:\\:getRecommendationRequest\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Event/Subscriber/RecommendationEventSubscriber.php

-
message: "#^Parameter \\#1 \\$parameters of class EzSystems\\\\EzRecommendationClient\\\\Request\\\\BasicRecommendationRequest constructor expects array\\<string\\>, array\\<string, mixed\\> given\\.$#"
count: 1
Expand Down Expand Up @@ -820,11 +795,6 @@ parameters:
count: 1
path: src/lib/Helper/ExportProcessRunnerHelper.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ExportProcessRunnerHelper\\:\\:run\\(\\) return type has no value type specified in iterable type Symfony\\\\Component\\\\Process\\\\Process\\.$#"
count: 1
path: src/lib/Helper/ExportProcessRunnerHelper.php

-
message: "#^Property EzSystems\\\\EzRecommendationClient\\\\Helper\\\\ExportProcessRunnerHelper\\:\\:\\$phpPath \\(string\\) does not accept string\\|false\\.$#"
count: 1
Expand Down Expand Up @@ -905,61 +875,6 @@ parameters:
count: 1
path: src/lib/Helper/SiteAccessHelper.php

-
message: "#^Cannot call method getUser\\(\\) on Symfony\\\\Component\\\\Security\\\\Core\\\\Authentication\\\\Token\\\\TokenInterface\\|null\\.$#"
count: 1
path: src/lib/Helper/UserHelper.php

-
message: "#^Cannot call method getUsername\\(\\) on Symfony\\\\Component\\\\Security\\\\Core\\\\Authentication\\\\Token\\\\TokenInterface\\|null\\.$#"
count: 1
path: src/lib/Helper/UserHelper.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Mapper\\\\ExportRequestMapper\\:\\:getCustomerId\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Mapper/ExportRequestMapper.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Mapper\\\\ExportRequestMapper\\:\\:getFields\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Mapper/ExportRequestMapper.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Mapper\\\\ExportRequestMapper\\:\\:getImage\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Mapper/ExportRequestMapper.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Mapper\\\\ExportRequestMapper\\:\\:getLang\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Mapper/ExportRequestMapper.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Mapper\\\\ExportRequestMapper\\:\\:getLicenseKey\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Mapper/ExportRequestMapper.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Mapper\\\\ExportRequestMapper\\:\\:getPath\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Mapper/ExportRequestMapper.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Mapper\\\\ExportRequestMapper\\:\\:getSiteAccess\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Mapper/ExportRequestMapper.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Mapper\\\\ExportRequestMapper\\:\\:getVisibility\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Mapper/ExportRequestMapper.php

-
message: "#^Method EzSystems\\\\EzRecommendationClient\\\\Mapper\\\\ExportRequestMapper\\:\\:getWebHook\\(\\) has parameter \\$parameterBag with no value type specified in iterable type Symfony\\\\Component\\\\HttpFoundation\\\\ParameterBag\\.$#"
count: 1
path: src/lib/Mapper/ExportRequestMapper.php

-
message: "#^Property EzSystems\\\\EzRecommendationClient\\\\SPI\\\\Content\\:\\:\\$fields \\(array\\<string\\>\\) does not accept string\\|null\\.$#"
count: 1
Expand Down
8 changes: 3 additions & 5 deletions src/lib/Event/Listener/LoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\API\Repository\UserService as UserServiceInterface;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use eZ\Publish\Core\MVC\Symfony\Security\UserInterface;
use EzSystems\EzRecommendationClient\Client\EzRecommendationClientInterface;
use EzSystems\EzRecommendationClient\Value\Parameters;
use EzSystems\EzRecommendationClient\Value\Session as RecommendationSession;
Expand Down Expand Up @@ -122,13 +123,10 @@ private function getNotificationEndpoint(): string
private function getUser(TokenInterface $authenticationToken): string
{
$user = $authenticationToken->getUser();

if (\is_string($user)) {
return $user;
} elseif (method_exists($user, 'getAPIUser')) {
if ($user instanceof UserInterface) {
return (string) $user->getAPIUser()->id;
}

return (string) $authenticationToken->getUsername();
return $authenticationToken->getUserIdentifier();
}
}
41 changes: 17 additions & 24 deletions src/lib/Helper/UserHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,38 @@
namespace EzSystems\EzRecommendationClient\Helper;

use eZ\Publish\Core\MVC\Symfony\Security\UserInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;

final class UserHelper
{
/** @var \Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface */
private $authorizationChecker;

/** @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface */
private $tokenStorage;

public function __construct(
AuthorizationCheckerInterface $authorizationChecker,
TokenStorageInterface $tokenStorage
) {
$this->authorizationChecker = $authorizationChecker;
$this->tokenStorage = $tokenStorage;
/** @var \Symfony\Component\Security\Core\Security */
private $security;

public function __construct(Security $security)
{
$this->security = $security;
}

/**
* @throws \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException
*/
public function getCurrentUser(): string
public function getCurrentUser(): ?string
{
if (!$this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY') // user has just logged in
&& !$this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') // user has logged in using remember_me cookie
if (!$this->security->isGranted('IS_AUTHENTICATED_FULLY') // user has just logged in
&& !$this->security->isGranted('IS_AUTHENTICATED_REMEMBERED') // user has logged in using remember_me cookie
) {
return '';
return null;
}

$authenticationToken = $this->tokenStorage->getToken();
$user = $authenticationToken->getUser();
$user = $this->security->getUser();
if (null === $user) {
return null;
}

if (is_string($user)) {
return $user;
} elseif ($user instanceof UserInterface) {
if ($user instanceof UserInterface) {
return (string) $user->getAPIUser()->id;
}

return (string) $authenticationToken->getUsername();
return $user->getUserIdentifier();
}
}
3 changes: 2 additions & 1 deletion tests/lib/Event/Listener/LoginListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
Expand Down Expand Up @@ -69,7 +70,7 @@ protected function setUp(): void
$this->userService = $this->createMock(UserService::class);
$this->configResolver = $this->createMock(ConfigResolverInterface::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->token = $this->createMock(TokenInterface::class);
$this->token = $this->createMock(AbstractToken::class);
$this->loginListener = new LoginListener(
$this->authorizationChecker,
$this->session,
Expand Down

0 comments on commit 37a8d52

Please sign in to comment.