Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ jobs:
matrix:
include:
- php-version: '8.1'
symfony-version: '5.4.*'
symfony-version: '6.4.*'
- php-version: '8.4'
symfony-version: '6.4.*'
- php-version: '8.2'
symfony-version: '5.4.*'
- php-version: '8.1'
symfony-version: '6.3.*'
- php-version: '8.2'
symfony-version: '6.3.*'
symfony-version: '7.4.*'
- php-version: '8.4'
symfony-version: '7.4.*'
- php-version: '8.4'
symfony-version: '8.0.*'
steps:
- name: "Checkout"
uses: actions/checkout@v2
Expand All @@ -41,8 +43,8 @@ jobs:
- name: "Setup env & install dependencies"
uses: ./.github/actions/install
with:
php-version: '8.1'
symfony-version: '6.1.*'
php-version: '8.4'
symfony-version: '8.0.*'
- name: "Run static analyzis with phpstan/phpstan"
run: vendor/bin/phpstan analyze

Expand All @@ -55,8 +57,8 @@ jobs:
- name: "Setup env & install dependencies"
uses: ./.github/actions/install
with:
php-version: '8.1'
symfony-version: '6.1.*'
php-version: '8.4'
symfony-version: '8.0.*'
- name: "Run checkstyle with symplify/easy-coding-standard"
run: vendor/bin/ecs

Expand All @@ -69,8 +71,8 @@ jobs:
- name: "Setup env & install dependencies"
uses: ./.github/actions/install
with:
php-version: '8.1'
symfony-version: '6.1.*'
php-version: '8.4'
symfony-version: '8.0.*'
coverage-mode: 'xdebug'
- name: "Run tests with phpunit/phpunit"
env:
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"require": {
"php": "^8.1",
"ext-openssl": "*",
"symfony/framework-bundle": "^5.4|^6.0",
"doctrine/orm": "^2.7",
"doctrine/doctrine-bundle": "^2.0",
"symfony/framework-bundle": "^6.4|^7.4|^8.0",
"doctrine/common": "^3.0",
"doctrine/orm": "^3.0",
"doctrine/doctrine-bundle": "^2.0|^3.0",
"yokai/dependency-injection": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpspec/prophecy-phpunit": "^2.0",
"symfony/yaml": "^5.4|^6.0",
"symfony/yaml": "^6.4|^7.4|^8.0",
"phpstan/phpstan": "^1.7",
"symplify/easy-coding-standard": "^11.3"
},
Expand Down
35 changes: 0 additions & 35 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:addDefaultsIfNotSet\\(\\)\\.$#"
count: 2
path: src/DependencyInjection/Configuration.php

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#"
count: 2
path: src/DependencyInjection/Configuration.php

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:useAttributeAsKey\\(\\)\\.$#"
count: 1
path: src/DependencyInjection/Configuration.php

-
message: "#^Method Yokai\\\\SecurityTokenBundle\\\\Entity\\\\Token\\:\\:getLastUsage\\(\\) should return Yokai\\\\SecurityTokenBundle\\\\Entity\\\\TokenUsage\\|null but returns mixed\\.$#"
count: 1
path: src/Entity/Token.php

-
message: "#^Method Yokai\\\\SecurityTokenBundle\\\\Entity\\\\Token\\:\\:getUsages\\(\\) should return array\\<Yokai\\\\SecurityTokenBundle\\\\Entity\\\\TokenUsage\\> but returns array\\.$#"
count: 1
path: src/Entity/Token.php

-
message: "#^Property Yokai\\\\SecurityTokenBundle\\\\Entity\\\\Token\\:\\:\\$id is never written, only read\\.$#"
count: 1
Expand All @@ -35,13 +10,3 @@ parameters:
count: 1
path: src/Entity/TokenUsage.php

-
message: "#^Cannot cast mixed to string\\.$#"
count: 1
path: src/Manager/ChainUserManager.php

-
message: "#^Cannot cast mixed to string\\.$#"
count: 1
path: src/Manager/DoctrineUserManager.php

5 changes: 3 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ includes:
- phpstan-baseline.neon
parameters:
level: max
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- identifier: missingType.generics
- identifier: missingType.iterableValue
paths:
- src/
2 changes: 1 addition & 1 deletion src/Archive/ArchivistInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ interface ArchivistInterface
*
* @return integer
*/
public function archive(string $purpose = null): int;
public function archive(string|null $purpose = null): int;
}
2 changes: 1 addition & 1 deletion src/Archive/DeleteArchivist.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(EntityRepository $tokenRepository)
$this->tokenRepository = $tokenRepository;
}

public function archive(string $purpose = null): int
public function archive(string|null $purpose = null): int
{
$builder = $this->tokenRepository->createQueryBuilder('token')
->delete($this->tokenRepository->getClassName(), 'token');
Expand Down
8 changes: 4 additions & 4 deletions src/Entity/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Token
private $keepUntil;

/**
* @var Collection<TokenUsage>
* @var Collection<int, TokenUsage>
*/
private $usages;

Expand Down Expand Up @@ -192,15 +192,15 @@ public function getUsages(): array
return $this->usages->toArray();
}

public function getLastUsage(): ?TokenUsage
public function getLastUsage(): TokenUsage|null
{
return $this->usages->last();
return $this->usages->last() ?: null;
}

/**
* @throws LogicException
*/
public function consume(array $information, DateTime $date = null): void
public function consume(array $information, DateTime|null $date = null): void
{
if ($this->isConsumed()) {
throw new LogicException(
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/TokenUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TokenUsage
/**
* @param array<string, mixed> $information
*/
public function __construct(Token $token, array $information, DateTime $createdAt = null)
public function __construct(Token $token, array $information, DateTime|null $createdAt = null)
{
$this->token = $token;
$this->information = $information;
Expand Down
3 changes: 1 addition & 2 deletions src/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function tokenCreated(Token $token): TokenCreatedEvent
return $event;
}

public function consumeToken(Token $token, DateTime $at = null, array $information = []): ConsumeTokenEvent
public function consumeToken(Token $token, DateTime|null $at = null, array $information = []): ConsumeTokenEvent
{
$this->eventDispatcher->dispatch(
$event = new ConsumeTokenEvent($token, $at, $information)
Expand All @@ -71,7 +71,6 @@ public function tokenConsumed(Token $token): TokenConsumedEvent
return $event;
}


public function tokenTotallyConsumed(Token $token): TokenTotallyConsumedEvent
{
$this->eventDispatcher->dispatch(
Expand Down
14 changes: 11 additions & 3 deletions src/Manager/ChainUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,18 @@ private function getManagerForUser($user): UserManagerInterface
$tries[] = get_class($manager);
}

if (is_object($user) && !method_exists($user, '__toString')) {
$userAsString = sprintf('%s::%s', get_class($user), spl_object_hash($user));
if (is_object($user)) {
if (!method_exists($user, '__toString')) {
$userAsString = sprintf('%s::%s', get_class($user), spl_object_hash($user));
} else {
$userAsString = (string)$user;
}
} else {
$userAsString = (string)$user;
if (is_scalar($user)) {
$userAsString = (string)$user;
} else {
$userAsString = get_debug_type($user);
}
}

throw new \InvalidArgumentException(
Expand Down
14 changes: 10 additions & 4 deletions src/Manager/DoctrineUserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ public function get(string $class, string $id)
public function getClass($user): string
{
/** @var object $user */
/** @var class-string $class */
$class = ClassUtils::getClass($user);

return $class;
return ClassUtils::getClass($user);
}

public function getId($user): string
Expand All @@ -71,7 +69,15 @@ public function getId($user): string
throw new \InvalidArgumentException('Entities with composite ids are not supported');
}

return (string) reset($identifiers);
$identifier = reset($identifiers);
if (is_scalar($identifier)
|| $identifier === null
|| (is_object($identifier) && method_exists($identifier, '__toString'))
) {
return (string)$identifier;
}

throw new \InvalidArgumentException('Entities with non stringable ids are not supported');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Manager/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function create(string $purpose, $user, array $payload = []): Token
return $token;
}

public function consume(Token $token, DateTime $at = null): void
public function consume(Token $token, DateTime|null $at = null): void
{
$event = $this->eventDispatcher->consumeToken($token, $at, $this->informationGuesser->get());

Expand Down
2 changes: 1 addition & 1 deletion src/Manager/TokenManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function create(string $purpose, $user, array $payload = []): Token;
* @param Token $token The token to consume
* @param DateTime|null $at The date/time at which the token was consumed (defaults to now)
*/
public function consume(Token $token, DateTime $at = null): void;
public function consume(Token $token, DateTime|null $at = null): void;

/**
* Get the user associated to a token.
Expand Down
16 changes: 10 additions & 6 deletions src/Repository/DoctrineORMTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function get(string $value, string $purpose): Token
[
'value' => $value,
'purpose' => $purpose,
]
],
);

if (!$token instanceof Token) {
Expand All @@ -67,7 +67,7 @@ public function findExisting(string $userClass, string $userId, string $purpose)
'userClass' => $userClass,
'userId' => $userId,
'purpose' => $purpose,
]
],
);
if (!$token instanceof Token) {
return null;
Expand All @@ -81,14 +81,12 @@ public function findExisting(string $userClass, string $userId, string $purpose)

public function create(Token $token): void
{
$this->manager->persist($token);
$this->manager->flush($token);
$this->save($token);
}

public function update(Token $token): void
{
$this->manager->persist($token);
$this->manager->flush($token);
$this->save($token);
}

public function exists(string $value, string $purpose): bool
Expand All @@ -107,4 +105,10 @@ public function exists(string $value, string $purpose): bool

return intval($result) > 0;
}

private function save(Token $token): void
{
$this->manager->persist($token);
$this->manager->flush();
}
}
21 changes: 10 additions & 11 deletions tests/Command/ArchiveTokenCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Yokai\SecurityTokenBundle\Tests\Command;

use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;
Expand All @@ -19,10 +18,8 @@
*/
class ArchiveTokenCommandTest extends KernelTestCase
{
use ProphecyTrait;

/**
* @var ArchivistInterface|ObjectProphecy
* @var MockObject<ArchivistInterface>
*/
private $archivist;

Expand All @@ -33,10 +30,10 @@ class ArchiveTokenCommandTest extends KernelTestCase

protected function setUp(): void
{
$this->archivist = $this->prophesize(ArchivistInterface::class);
$this->archivist = $this->createMock(ArchivistInterface::class);

self::bootKernel();
self::$kernel->getContainer()->set('yokai_security_token.archivist', $this->archivist->reveal());
self::$kernel->getContainer()->set('yokai_security_token.archivist', $this->archivist);

$this->application = new Application(self::$kernel);
}
Expand Down Expand Up @@ -75,8 +72,9 @@ public function it_archive_every_token_when_run_without_options_with_confirmatio
{
$command = $this->command();

$this->archivist->archive(null)
->shouldBeCalledTimes(1)
$this->archivist->expects(self::once())
->method('archive')
->with(null)
->willReturn(10);

$output = $this->runCommand($command);
Expand All @@ -91,8 +89,9 @@ public function it_archive_partial_tokens_when_run_with_options(): void
{
$command = $this->command();

$this->archivist->archive('init_password')
->shouldBeCalledTimes(1)
$this->archivist->expects(self::once())
->method('archive')
->with('init_password')
->willReturn(10);

$output = $this->runCommand($command, ['purpose' => 'init_password']);
Expand Down
Loading
Loading