Skip to content

Commit

Permalink
Refactor code according PHPStan rules
Browse files Browse the repository at this point in the history
  • Loading branch information
coldic3 committed Aug 23, 2024
1 parent 7aba299 commit c7e463c
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 35 deletions.
8 changes: 2 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ parameters:
excludePaths:
- src/Core/Infrastructure/Symfony/Kernel.php
ignoreErrors:
- '/^Method Panda\\\w+\\Infrastructure\\ApiState\\Processor\\\w+Processor::process\(\) has no return type specified\.$/'
- '/^Method Panda\\\w+\\Infrastructure\\ApiState\\Processor\\\w+Processor::process\(\) has parameter \$(context|uriVariables) with no value type specified in iterable type array\.$/'
- '/^Method Panda\\\w+\\Infrastructure\\ApiState\\Processor\\\w+Processor::process\(\) has parameter \$data with no type specified\.$/'
- '/^Method Panda\\\w+\\Infrastructure\\ApiState\\Provider\\\w+Provider::provide\(\) has parameter \$(context|uriVariables) with no value type specified in iterable type array\.$/'
- '/^Method Panda\\\w+\\Infrastructure\\ApiSerializer\\\w+Normalizer::normalize\(\) has parameter \$context with no value type specified in iterable type array\.$/'
- '/^Method Panda\\\w+\\Infrastructure\\ApiSerializer\\\w+Normalizer::supportsNormalization\(\) has parameter \$context with no value type specified in iterable type array\.$/'
- '/^Method Panda\\\w+\\Infrastructure\\ApiSerializer\\\w+Normalizer::normalize\(\) return type has no value type specified in iterable type array\.$/'
Expand All @@ -18,8 +14,8 @@ parameters:
-
message: '/^Generator expects value type object, mixed given\.$/'
path: src/Core/Infrastructure/Doctrine/Orm/DoctrineCollectionIterator.php

checkGenericClassInNonGenericObjectType: false
-
identifier: missingType.generics

services:
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Panda\Account\Domain\Model\User;
use Panda\Account\Infrastructure\ApiResource\UserResource;
use Panda\Core\Application\Query\QueryBusInterface;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class UserProvider implements ProviderInterface
{
Expand All @@ -19,8 +21,10 @@ public function __construct(private QueryBusInterface $queryBus)

public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?UserResource
{
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

/** @var User|null $model */
$model = $this->queryBus->ask(new FindUserQuery($uriVariables['id']));
$model = $this->queryBus->ask(new FindUserQuery($id));

if (null === $model) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Panda\Exchange\Application\Command\ExchangeRateLive\UpdateExchangeRateLiveCommand;
use Panda\Exchange\Domain\Model\ExchangeRateLiveInterface;
use Panda\Exchange\Infrastructure\ApiResource\ExchangeRateLiveResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class ExchangeRateLiveProcessor implements ProcessorInterface
Expand All @@ -27,18 +28,22 @@ public function process($data, Operation $operation, array $uriVariables = [], a
Assert::isInstanceOf($data, ExchangeRateLiveResource::class);

if ($operation instanceof DeleteOperationInterface) {
$this->commandBus->dispatch(new DeleteExchangeRateLiveCommand($uriVariables['id']));
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

$this->commandBus->dispatch(new DeleteExchangeRateLiveCommand($id));

return null;
}

$command = !isset($uriVariables['id'])
Assert::nullOrIsInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

$command = null === $id
? new CreateExchangeRateLiveCommand(
(string) $data->baseTicker,
(string) $data->quoteTicker,
(float) $data->rate,
)
: new UpdateExchangeRateLiveCommand($uriVariables['id'], (float) $data->rate);
: new UpdateExchangeRateLiveCommand($id, (float) $data->rate);

/** @var ExchangeRateLiveInterface $model */
$model = $this->commandBus->dispatch($command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Panda\Exchange\Application\Command\ExchangeRateLog\DeleteExchangeRateLogCommand;
use Panda\Exchange\Domain\Model\ExchangeRateLogInterface;
use Panda\Exchange\Infrastructure\ApiResource\ExchangeRateLogResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class ExchangeRateLogProcessor implements ProcessorInterface
Expand All @@ -26,7 +27,9 @@ public function process($data, Operation $operation, array $uriVariables = [], a
Assert::isInstanceOf($data, ExchangeRateLogResource::class);

if ($operation instanceof DeleteOperationInterface) {
$this->commandBus->dispatch(new DeleteExchangeRateLogCommand($uriVariables['id']));
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

$this->commandBus->dispatch(new DeleteExchangeRateLogCommand($id));

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Panda\Exchange\Domain\Model\ExchangeRateLive;
use Panda\Exchange\Infrastructure\ApiResource\ExchangeRateLiveResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class ExchangeRateLiveProvider implements ProviderInterface
{
Expand All @@ -31,7 +32,9 @@ public function __construct(
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (!$operation instanceof CollectionOperationInterface) {
return $this->provideItem($uriVariables['id']);
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

return $this->provideItem($id);
}

$offset = $limit = null;
Expand All @@ -41,10 +44,14 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
$limit = $this->pagination->getLimit($operation, $context);
}

/** @phpstan-ignore-next-line false positive */
$baseTicker = isset($context['filters']['baseTicker'])
/** @phpstan-ignore-next-line false positive */
? (string) $context['filters']['baseTicker']
: null;
/** @phpstan-ignore-next-line false positive */
$quoteTicker = isset($context['filters']['quoteTicker'])
/** @phpstan-ignore-next-line false positive */
? (string) $context['filters']['quoteTicker']
: null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Panda\Exchange\Domain\Model\ExchangeRateLog;
use Panda\Exchange\Infrastructure\ApiResource\ExchangeRateLogResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class ExchangeRateLogProvider implements ProviderInterface
{
Expand All @@ -31,7 +32,9 @@ public function __construct(
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (!$operation instanceof CollectionOperationInterface) {
return $this->provideItem($uriVariables['id']);
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

return $this->provideItem($id);
}

$offset = $limit = null;
Expand All @@ -41,10 +44,14 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
$limit = $this->pagination->getLimit($operation, $context);
}

/** @phpstan-ignore-next-line false positive */
$baseTicker = isset($context['filters']['baseTicker'])
/** @phpstan-ignore-next-line false positive */
? (string) $context['filters']['baseTicker']
: null;
/** @phpstan-ignore-next-line false positive */
$quoteTicker = isset($context['filters']['quoteTicker'])
/** @phpstan-ignore-next-line false positive */
? (string) $context['filters']['quoteTicker']
: null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public function supportsNormalization(mixed $data, ?string $format = null, array
;
}

// public function getSupportedTypes(?string $format): array
// {
// return [
// ResourceRepresentation::class => true,
// QuantityRepresentation::class => true,
// ReportEntryRepresentation::class => true,
// ReportFileRepresentation::class => true,
// ];
// }
// public function getSupportedTypes(?string $format): array
// {
// return [
// ResourceRepresentation::class => true,
// QuantityRepresentation::class => true,
// ReportEntryRepresentation::class => true,
// ReportFileRepresentation::class => true,
// ];
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Panda\Portfolio\Application\Command\Portfolio\ChangeDefaultPortfolioCommand;
use Panda\Portfolio\Domain\Model\Portfolio\PortfolioInterface;
use Panda\Portfolio\Infrastructure\ApiResource\PortfolioResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class PortfolioChangeDefaultProcessor implements ProcessorInterface
Expand All @@ -22,8 +23,9 @@ public function process($data, Operation $operation, array $uriVariables = [], a
{
/** @var PortfolioResource $data */
Assert::isInstanceOf($data, PortfolioResource::class);
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

$command = new ChangeDefaultPortfolioCommand($uriVariables['id']);
$command = new ChangeDefaultPortfolioCommand($id);

/** @var PortfolioInterface $model */
$model = $this->commandBus->dispatch($command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Panda\Portfolio\Application\Command\Portfolio\UpdatePortfolioCommand;
use Panda\Portfolio\Domain\Model\Portfolio\PortfolioInterface;
use Panda\Portfolio\Infrastructure\ApiResource\PortfolioResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class PortfolioUpdateProcessor implements ProcessorInterface
Expand All @@ -22,8 +23,9 @@ public function process($data, Operation $operation, array $uriVariables = [], a
{
/** @var PortfolioResource $data */
Assert::isInstanceOf($data, PortfolioResource::class);
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

$command = new UpdatePortfolioCommand($uriVariables['id'], (string) $data->name);
$command = new UpdatePortfolioCommand($id, (string) $data->name);

/** @var PortfolioInterface $model */
$model = $this->commandBus->dispatch($command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Panda\Portfolio\Domain\Model\Portfolio\Portfolio;
use Panda\Portfolio\Infrastructure\ApiResource\PortfolioResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class PortfolioProvider implements ProviderInterface
{
Expand All @@ -31,7 +32,9 @@ public function __construct(
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (!$operation instanceof CollectionOperationInterface) {
return $this->provideItem($uriVariables['id']);
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

return $this->provideItem($id);
}

$offset = $limit = null;
Expand Down
5 changes: 1 addition & 4 deletions src/Report/Application/Action/DownloadReportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public function __construct(

public function __invoke(Request $request): Response
{
Assert::notNull(
/** @var string|null $id */
$id = $request->get('id')
);
Assert::string($id = $request->get('id'));

$report = $this->reportRepository->findById(Uuid::fromString($id));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Panda\Report\Domain\Model\Report\Report;
use Panda\Report\Infrastructure\ApiResource\ReportResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class ReportProvider implements ProviderInterface
{
Expand All @@ -31,7 +32,9 @@ public function __construct(
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (!$operation instanceof CollectionOperationInterface) {
return $this->provideItem($uriVariables['id']);
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

return $this->provideItem($id);
}

$offset = $limit = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Panda\Trade\Application\Command\Asset\ChangeAssetTickerCommand;
use Panda\Trade\Domain\Model\Asset\AssetInterface;
use Panda\Trade\Infrastructure\ApiResource\AssetResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class AssetChangeTickerProcessor implements ProcessorInterface
Expand All @@ -24,8 +25,9 @@ public function process($data, Operation $operation, array $uriVariables = [], a
/** @var AssetResource $data */
Assert::isInstanceOf($data, AssetResource::class);
Assert::notNull($data->ticker);
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

$command = new ChangeAssetTickerCommand($uriVariables['id'], $data->ticker);
$command = new ChangeAssetTickerCommand($id, $data->ticker);

/** @var AssetInterface $model */
$model = $this->commandBus->dispatch($command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Panda\Trade\Application\Command\Asset\UpdateAssetCommand;
use Panda\Trade\Domain\Model\Asset\AssetInterface;
use Panda\Trade\Infrastructure\ApiResource\AssetResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class AssetProcessor implements ProcessorInterface
Expand All @@ -27,14 +28,18 @@ public function process($data, Operation $operation, array $uriVariables = [], a
Assert::isInstanceOf($data, AssetResource::class);

if ($operation instanceof DeleteOperationInterface) {
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

$this->commandBus->dispatch(new DeleteAssetCommand($uriVariables['id']));

return null;
}

$command = !isset($uriVariables['id'])
Assert::nullOrIsInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

$command = null === $id
? new CreateAssetCommand((string) $data->ticker, (string) $data->name)
: new UpdateAssetCommand($uriVariables['id'], $data->name)
: new UpdateAssetCommand($id, $data->name)
;

/** @var AssetInterface $model */
Expand Down
5 changes: 4 additions & 1 deletion src/Trade/Infrastructure/ApiState/Provider/AssetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Panda\Trade\Domain\Model\Asset\Asset;
use Panda\Trade\Infrastructure\ApiResource\AssetResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class AssetProvider implements ProviderInterface
{
Expand All @@ -31,7 +32,9 @@ public function __construct(
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (!$operation instanceof CollectionOperationInterface) {
return $this->provideItem($uriVariables['id']);
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

return $this->provideItem($id);
}

$offset = $limit = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Panda\Trade\Domain\Model\Transaction\Transaction;
use Panda\Trade\Infrastructure\ApiResource\TransactionResource;
use Symfony\Component\Uid\Uuid;
use Webmozart\Assert\Assert;

final readonly class TransactionProvider implements ProviderInterface
{
Expand All @@ -31,7 +32,9 @@ public function __construct(
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
if (!$operation instanceof CollectionOperationInterface) {
return $this->provideItem($uriVariables['id']);
Assert::isInstanceOf($id = $uriVariables['id'] ?? null, Uuid::class);

return $this->provideItem($id);
}

$offset = $limit = null;
Expand All @@ -43,16 +46,20 @@ public function provide(Operation $operation, array $uriVariables = [], array $c

$afterConcludedAt = \DateTimeImmutable::createFromFormat(
'U',
/** @phpstan-ignore-next-line false positive */
(string) ($context['filters']['concludedAt']['after'] ?? null)
);
$beforeConcludedAt = \DateTimeImmutable::createFromFormat(
'U',
/** @phpstan-ignore-next-line false positive */
(string) ($context['filters']['concludedAt']['before'] ?? null)
);

/** @var DoctrineCollectionIterator<Transaction> $models */
$models = $this->queryBus->ask(new FindTransactionsQuery(
/** @phpstan-ignore-next-line false positive */
$context['filters']['fromOperation.asset.id'] ?? null,
/** @phpstan-ignore-next-line false positive */
$context['filters']['toOperation.asset.id'] ?? null,
false === $afterConcludedAt ? null : $afterConcludedAt,
false === $beforeConcludedAt ? null : $beforeConcludedAt,
Expand Down

0 comments on commit c7e463c

Please sign in to comment.