Skip to content

Commit

Permalink
Merge pull request #2222 from tarlepp/chore(deps)/phpstan-upgrade
Browse files Browse the repository at this point in the history
Chore(deps) - PHPStan update
  • Loading branch information
tarlepp committed Mar 1, 2023
2 parents f44b9bc + 3e5c10e commit d0e4d2d
Show file tree
Hide file tree
Showing 47 changed files with 764 additions and 794 deletions.
9 changes: 2 additions & 7 deletions src/EventSubscriber/AuthenticationFailureSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Throwable;
use function assert;

/**
* Class AuthenticationFailureSubscriber
Expand Down Expand Up @@ -56,13 +54,10 @@ public static function getSubscribedEvents(): array
public function onAuthenticationFailure(AuthenticationFailureEvent $event): void
{
$token = $event->getException()->getToken();
$user = $token?->getUser();

// Fetch user entity
if ($token !== null && $token->getUser() !== null) {
$user = $token->getUser();

assert($user instanceof UserInterface);

if ($token !== null && $user !== null) {
$identifier = $user->getUserIdentifier();

$this->loginLogger->setUser($this->userRepository->loadUserByIdentifier($identifier, false));
Expand Down
6 changes: 5 additions & 1 deletion src/Rest/ResponseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ public function handleFormError(FormInterface $form): void
{
$errors = [];

/** @var FormError $error */
/**
* @phpstan-ignore-next-line
*
* @var FormError $error
*/
foreach ($form->getErrors(true) as $error) {
$name = $error->getOrigin()?->getName() ?? '';

Expand Down
4 changes: 1 addition & 3 deletions src/Rest/RestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public function getSerializerContext(): array

public function getRepository(): BaseRepositoryInterface
{
$exception = new UnexpectedValueException('Repository not set on constructor');

return property_exists($this, 'repository') ? $this->repository ?? throw $exception : throw $exception;
return $this->repository;
}

public function getValidator(): ValidatorInterface
Expand Down
6 changes: 5 additions & 1 deletion src/Utils/Tests/PhpUnitUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ public static function recursiveFileSearch(string $folder, string $pattern): arr
$dir = new RecursiveDirectoryIterator($folder);
$ite = new RecursiveIteratorIterator($dir);

/** @var array<int, string> $files */
/**
* @phpstan-ignore-next-line
*
* @var array<int, string> $files
*/
$files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
$fileList = [];

Expand Down
8 changes: 1 addition & 7 deletions src/Utils/Tests/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
use Throwable;
use UnexpectedValueException;
use function array_merge;
use function gc_collect_cycles;
use function gc_enable;
use function getenv;
use function is_string;

/**
* Class WebTestCase
Expand Down Expand Up @@ -139,11 +137,7 @@ public function getFastestHeaders(): array
$output = [];

if (getenv('ENV_TEST_CHANNEL_READABLE')) {
$testChannel = getenv('ENV_TEST_CHANNEL_READABLE');

if (!is_string($testChannel)) {
throw new UnexpectedValueException('Could not get `ENV_TEST_CHANNEL_READABLE` environment variable.');
}
$testChannel = (string)getenv('ENV_TEST_CHANNEL_READABLE');

$output = [
'X-FASTEST-ENV-TEST-CHANNEL-READABLE' => $testChannel,
Expand Down
3 changes: 0 additions & 3 deletions tests/E2E/Controller/HealthzControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public function testThatHealthzRouteReturns200(): void
public function testThatHealthzRouteDoesNotMakeRequestLog(): void
{
$resource = self::getContainer()->get(LogRequestResource::class);

self::assertInstanceOf(LogRequestResource::class, $resource);

$expectedLogCount = $resource->count();

$client = $this->getTestClient();
Expand Down
4 changes: 0 additions & 4 deletions tests/E2E/Controller/VersionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ public function testThatVersionRouteReturns200(): void
public function testThatVersionRouteDoesNotMakeRequestLog(): void
{
$resource = self::getContainer()->get(LogRequestResource::class);

self::assertInstanceOf(LogRequestResource::class, $resource);

$expectedLogCount = $resource->count();

$client = $this->getTestClient();
Expand All @@ -65,7 +62,6 @@ public function testThatApiVersionIsAddedToResponseHeaders(): void
$client->request('GET', '/version');

$response = $client->getResponse();

$version = $response->headers->get('X-API-VERSION');

self::assertNotNull($version);
Expand Down
2 changes: 0 additions & 2 deletions tests/E2E/Controller/v1/Profile/GroupsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ public static function dataProviderTestThatGroupsActionReturnExpectedWithValidAp
{
$rolesService = self::getContainer()->get(RolesService::class);

self::assertInstanceOf(RolesService::class, $rolesService);

if (getenv('USE_ALL_USER_COMBINATIONS') === 'yes') {
foreach ($rolesService->getRoles() as $role) {
yield [str_pad($rolesService->getShort($role), 40, '_')];
Expand Down
2 changes: 0 additions & 2 deletions tests/E2E/Controller/v1/Profile/IndexControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ public static function dataProviderTestThatProfileActionReturnsExpectedWithValid
{
$rolesService = self::getContainer()->get(RolesService::class);

self::assertInstanceOf(RolesService::class, $rolesService);

if (getenv('USE_ALL_USER_COMBINATIONS') === 'yes') {
foreach ($rolesService->getRoles() as $role) {
yield [str_pad($rolesService->getShort($role), 40, '_')];
Expand Down
2 changes: 0 additions & 2 deletions tests/E2E/Controller/v1/Profile/RolesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,6 @@ public static function dataProviderTestThatRolesActionReturnsExpectedWithValidAp
{
$rolesService = self::getContainer()->get(RolesService::class);

self::assertInstanceOf(RolesService::class, $rolesService);

if (getenv('USE_ALL_USER_COMBINATIONS') === 'yes') {
foreach ($rolesService->getRoles() as $role) {
yield [str_pad($rolesService->getShort($role), 40, '_')];
Expand Down
4 changes: 0 additions & 4 deletions tests/E2E/Controller/v1/User/UserRolesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ public static function dataProviderTestThatGetUserRolesReturns200ForUserHimself(
{
$rolesService = self::getContainer()->get(RolesServiceInterface::class);

self::assertInstanceOf(RolesServiceInterface::class, $rolesService);

yield [
LoadUserData::$uuids['john'],
'john',
Expand Down Expand Up @@ -231,8 +229,6 @@ public static function dataProviderTestThatGetRolesReturns200ForRootRoleUser():
{
$rolesService = self::getContainer()->get(RolesServiceInterface::class);

self::assertInstanceOf(RolesServiceInterface::class, $rolesService);

yield [LoadUserData::$uuids['john'], '[]'];

yield [
Expand Down
6 changes: 1 addition & 5 deletions tests/E2E/Rest/ResourceLifeCycleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ public static function dataProviderTestThatModifiedEntityIsNotFlushedIfLifeCycle
*/
private function getRepository(): RoleRepository
{
$resource = self::getContainer()->get(ResourceForLifeCycleTests::class);

self::assertInstanceOf(ResourceForLifeCycleTests::class, $resource);

return $resource->getRepository();
return self::getContainer()->get(ResourceForLifeCycleTests::class)->getRepository();
}
}
16 changes: 8 additions & 8 deletions tests/Functional/Repository/HealthzRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

namespace App\Tests\Functional\Repository;

use App\Entity\Healthz;
use App\Repository\HealthzRepository;
use App\Utils\Tests\PhpUnitUtil;
use DateTimeImmutable;
use DateTimeZone;
use PHPUnit\Framework\Attributes\Depends;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Throwable;
Expand Down Expand Up @@ -55,8 +56,11 @@ public function testThatReadValueMethodReturnsExpectedWithEmptyDatabase(): void
#[Depends('testThatReadValueMethodReturnsExpectedWithEmptyDatabase')]
public function testThatCreateValueReturnsExpected(): void
{
/** @noinspection UnnecessaryAssertionInspection */
self::assertInstanceOf(Healthz::class, $this->getRepository()->create());
self::assertEqualsWithDelta(
(new DateTimeImmutable('now', new DateTimeZone('utc')))->getTimestamp(),
$this->getRepository()->create()->getCreatedAt()->getTimestamp(),
1,
);
}

/**
Expand All @@ -82,10 +86,6 @@ public function testThatCleanupMethodClearsDatabaseReturnsExpected(): void
*/
private function getRepository(): HealthzRepository
{
$repository = self::getContainer()->get(HealthzRepository::class);

self::assertInstanceOf(HealthzRepository::class, $repository);

return $repository;
return self::getContainer()->get(HealthzRepository::class);
}
}
9 changes: 4 additions & 5 deletions tests/Functional/Repository/LogLoginFailureRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace App\Tests\Functional\Repository;

use App\Entity\LogLoginFailure;
use App\Entity\User;
use App\Repository\LogLoginFailureRepository;
use App\Repository\UserRepository;
use PHPUnit\Framework\Attributes\TestDox;
Expand All @@ -32,15 +31,15 @@ public function testThatClearMethodWorksAssExpected(): void
{
self::bootKernel();

/** @psalm-var UserRepository $userRepository */
$userRepository = self::getContainer()->get(UserRepository::class);
$logLoginFailureRepository = self::getContainer()->get(LogLoginFailureRepository::class);

self::assertInstanceOf(UserRepository::class, $userRepository);
self::assertInstanceOf(LogLoginFailureRepository::class, $logLoginFailureRepository);
/** @psalm-var LogLoginFailureRepository $logLoginFailureRepository */
$logLoginFailureRepository = self::getContainer()->get(LogLoginFailureRepository::class);

$user = $userRepository->find('20000000-0000-1000-8000-000000000001');

self::assertInstanceOf(User::class, $user);
self::assertNotNull($user);

$entity = new LogLoginFailure($user);

Expand Down
1 change: 0 additions & 1 deletion tests/Functional/Repository/LogRequestRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function testThatCleanHistoryReturnsExpected(): void
{
$repository = self::getContainer()->get(LogRequestRepository::class);

self::assertInstanceOf(LogRequestRepository::class, $repository);
self::assertSame(0, $repository->cleanHistory());
}
}
1 change: 0 additions & 1 deletion tests/Functional/Repository/RoleRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function testThatResetMethodDeletesAllRecords(): void
{
$repository = self::getContainer()->get(RoleRepository::class);

self::assertInstanceOf(RoleRepository::class, $repository);
self::assertSame(5, $repository->countAdvanced());
self::assertSame(5, $repository->reset());
self::assertSame(0, $repository->countAdvanced());
Expand Down
6 changes: 4 additions & 2 deletions tests/Functional/Repository/UserRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,18 @@ public function testThatResetMethodDeletesAllRecords(): void
self::assertSame(0, $this->getRepository()->countAdvanced());
}

/**
* @throws Throwable
*/
private function getRepository(): UserRepository
{
static $cache;

if ($cache === null) {
self::bootKernel();

/** @psalm-var UserRepository $cache */
$cache = self::getContainer()->get(UserRepository::class);

self::assertInstanceOf(UserRepository::class, $cache);
}

return $cache;
Expand Down
40 changes: 20 additions & 20 deletions tests/Functional/Security/Provider/ApiKeyUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public function testThatLoadUserByIdentifierWorksAsExpected(string $token, Strin
{
$apiKeyUser = $this->getApiKeyUserProvider()->loadUserByIdentifier($token);

self::assertInstanceOf(ApiKeyUser::class, $apiKeyUser);
self::assertSame($roles->getArrayCopy(), $apiKeyUser->getRoles());
}

Expand Down Expand Up @@ -119,11 +118,7 @@ public function testThatSupportsClassReturnsExpected(bool $expected, string $cla
*/
public static function dataProviderTestThatGetApiKeyReturnsExpected(): array
{
self::bootKernel();

$rolesService = static::getContainer()->get(RolesService::class);

self::assertInstanceOf(RolesService::class, $rolesService);
[, $rolesService] = self::getServices();

$iterator = static fn (string $role): array => [$rolesService->getShort($role)];

Expand All @@ -138,16 +133,9 @@ public static function dataProviderTestThatGetApiKeyReturnsExpected(): array
*/
public static function dataProviderTestThatLoadUserByIdentifierWorksAsExpected(): array
{
self::bootKernel();

$managerRegistry = static::getContainer()->get('doctrine');
$rolesService = static::getContainer()->get(RolesService::class);

self::assertInstanceOf(ManagerRegistry::class, $managerRegistry);
self::assertInstanceOf(RolesService::class, $rolesService);
[$managerRegistry, $rolesService] = self::getServices();

$repositoryClass = ApiKeyRepository::class;
$repository = new $repositoryClass($managerRegistry);
$repository = new ApiKeyRepository($managerRegistry);

$iterator = static fn (ApiKey $apiKey): array => [
$apiKey->getToken(),
Expand All @@ -170,16 +158,28 @@ public static function dataProviderTestThatSupportsClassReturnsExpected(): Gener
* @throws Throwable
*/
private function getApiKeyUserProvider(): ApiKeyUserProvider
{
[$managerRegistry, $rolesService] = self::getServices();
$repository = ApiKeyRepository::class;

return new ApiKeyUserProvider(new $repository($managerRegistry), $rolesService);
}

/**
* @throws Throwable
*
* @return array{0: ManagerRegistry, 1: RolesService}
*/
private static function getServices(): array
{
self::bootKernel();

/** @psalm-var ManagerRegistry $managerRegistry */
$managerRegistry = static::getContainer()->get('doctrine');
$rolesService = static::getContainer()->get(RolesService::class);
$repository = ApiKeyRepository::class;

self::assertInstanceOf(ManagerRegistry::class, $managerRegistry);
self::assertInstanceOf(RolesService::class, $rolesService);
/** @psalm-var RolesService $rolesService */
$rolesService = static::getContainer()->get(RolesService::class);

return new ApiKeyUserProvider(new $repository($managerRegistry), $rolesService);
return [$managerRegistry, $rolesService];
}
}
Loading

0 comments on commit d0e4d2d

Please sign in to comment.