From 38baa37e7e2cf080da4a3041a45830815edf87a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Fri, 16 Jul 2021 11:20:14 +0200 Subject: [PATCH] Let PHPStan check array and iterable types & generics --- phpstan.neon | 2 -- src/batch-box-spout/src/FlatFileReader.php | 8 +++-- src/batch-box-spout/src/FlatFileWriter.php | 8 +++-- .../src/DoctrineDBALJobExecutionStorage.php | 34 +++++++++++++++---- .../src/DoctrineDBALQueryReader.php | 1 + .../src/JobExecutionRowNormalizer.php | 24 +++++++++++++ .../src/CommandRunner.php | 10 +++--- .../src/RunJobCommand.php | 12 ++++--- .../YokaiBatchExtension.php | 4 +++ .../src/LaunchJobMessage.php | 8 ++++- .../src/DenormalizeItemProcessor.php | 5 ++- .../src/NormalizeItemProcessor.php | 5 ++- .../src/SkipInvalidItemProcessor.php | 5 ++- src/batch/src/BatchStatus.php | 4 +-- .../Exception/UnexpectedValueException.php | 2 +- src/batch/src/Factory/JobExecutionFactory.php | 5 +-- src/batch/src/Failure.php | 15 +++----- .../src/Job/Item/ExpandProcessedItem.php | 30 +++++++++++++--- .../src/Job/Item/InvalidItemException.php | 7 ++-- src/batch/src/Job/Item/ItemJob.php | 4 ++- .../src/Job/Item/ItemReaderInterface.php | 1 + .../src/Job/Item/ItemWriterInterface.php | 1 + .../Job/Item/Processor/ArrayMapProcessor.php | 1 + .../Job/Item/Reader/StaticIterableReader.php | 5 ++- src/batch/src/JobExecution.php | 4 +-- src/batch/src/JobParameters.php | 11 +++--- .../src/Launcher/JobLauncherInterface.php | 2 ++ src/batch/src/Launcher/SimpleJobLauncher.php | 3 ++ .../Serializer/JsonJobExecutionSerializer.php | 18 ++++++++++ src/batch/src/Storage/Query.php | 16 +++++++++ src/batch/src/Storage/QueryBuilder.php | 15 ++++++++ src/batch/src/Summary.php | 16 +++++---- .../SequenceJobExecutionIdGenerator.php | 9 +++++ .../Test/Job/Item/Reader/TestDebugReader.php | 6 ++++ .../Test/Job/Item/Writer/InMemoryWriter.php | 12 +++++++ .../Test/Job/Item/Writer/TestDebugWriter.php | 6 ++++ .../Test/Launcher/BufferingJobLauncher.php | 7 ++++ .../Trigger/Scheduler/CallbackScheduler.php | 4 +++ .../src/Trigger/Scheduler/ScheduledJob.php | 7 ++++ src/batch/src/Warning.php | 9 +++++ 40 files changed, 279 insertions(+), 67 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 7ed149e8..24fb77a4 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,5 @@ parameters: level: max - checkMissingIterableValueType: false - checkGenericClassInNonGenericObjectType: false paths: - src/batch/src/ - src/batch-box-spout/src/ diff --git a/src/batch-box-spout/src/FlatFileReader.php b/src/batch-box-spout/src/FlatFileReader.php index 87a0c59b..89e668b4 100644 --- a/src/batch-box-spout/src/FlatFileReader.php +++ b/src/batch-box-spout/src/FlatFileReader.php @@ -38,7 +38,7 @@ final class FlatFileReader implements private string $type; /** - * @var array + * @phpstan-var array{delimiter?: string, enclosure?: string} */ private array $options; @@ -48,7 +48,7 @@ final class FlatFileReader implements private string $headersMode; /** - * @var array|null + * @phpstan-var list|null */ private ?array $headers; @@ -57,6 +57,10 @@ final class FlatFileReader implements */ private ?string $filePath; + /** + * @phpstan-param array{delimiter?: string, enclosure?: string} $options + * @phpstan-param list|null $headers + */ public function __construct( string $type, array $options = [], diff --git a/src/batch-box-spout/src/FlatFileWriter.php b/src/batch-box-spout/src/FlatFileWriter.php index 42548208..7c676f96 100644 --- a/src/batch-box-spout/src/FlatFileWriter.php +++ b/src/batch-box-spout/src/FlatFileWriter.php @@ -33,7 +33,7 @@ final class FlatFileWriter implements private string $type; /** - * @var array|null + * @phpstan-var list|null */ private ?array $headers; @@ -53,10 +53,14 @@ final class FlatFileWriter implements private ?string $filePath; /** - * @var array + * @phpstan-var array{delimiter?: string, enclosure?: string} */ private array $options; + /** + * @phpstan-param list|null $headers + * @phpstan-param array{delimiter?: string, enclosure?: string} $options + */ public function __construct(string $type, array $headers = null, string $filePath = null, array $options = []) { $this->type = $type; diff --git a/src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php b/src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php index ffeb61fb..9f440905 100644 --- a/src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php +++ b/src/batch-doctrine-dbal/src/DoctrineDBALJobExecutionStorage.php @@ -11,6 +11,7 @@ use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Types\Types; use Doctrine\Persistence\ConnectionRegistry; +use Generator; use Yokai\Batch\Exception\CannotStoreJobExecutionException; use Yokai\Batch\Exception\JobExecutionNotFoundException; use Yokai\Batch\Exception\RuntimeException; @@ -36,11 +37,11 @@ final class DoctrineDBALJobExecutionStorage implements QueryableJobExecutionStor */ private string $table; + private JobExecutionRowNormalizer $normalizer; + /** - * @var JobExecutionRowNormalizer|null + * @phpstan-param array{connection?: string, table?: string} $options */ - private ?JobExecutionRowNormalizer $normalizer = null; - public function __construct(ConnectionRegistry $doctrine, array $options) { $options = array_filter($options) + self::DEFAULT_OPTIONS; @@ -230,6 +231,9 @@ private function getSchema(): Schema return $schema; } + /** + * @phpstan-return array + */ private function types(): array { return [ @@ -247,6 +251,9 @@ private function types(): array ]; } + /** + * @phpstan-return array + */ private function identity(JobExecution $execution): array { return [ @@ -255,6 +262,9 @@ private function identity(JobExecution $execution): array ]; } + /** + * @phpstan-return array + */ private function fetchRow(string $jobName, string $id): array { $qb = $this->connection->createQueryBuilder(); @@ -289,7 +299,13 @@ private function fetchRow(string $jobName, string $id): array } } - private function queryList(string $query, array $parameters, array $types): iterable + /** + * @phpstan-param array $parameters + * @phpstan-param array $types + * + * @phpstan-return Generator + */ + private function queryList(string $query, array $parameters, array $types): Generator { /** @var Result $statement */ $statement = $this->connection->executeQuery($query, $parameters, $types); @@ -301,11 +317,17 @@ private function queryList(string $query, array $parameters, array $types): iter $statement->free(); } + /** + * @phpstan-return array + */ private function toRow(JobExecution $jobExecution): array { return $this->getNormalizer()->toRow($jobExecution); } + /** + * @phpstan-param array $row + */ private function fromRow(array $row): JobExecution { return $this->getNormalizer()->fromRow($row); @@ -313,9 +335,7 @@ private function fromRow(array $row): JobExecution private function getNormalizer(): JobExecutionRowNormalizer { - if ($this->normalizer === null) { - $this->normalizer = new JobExecutionRowNormalizer($this->connection->getDatabasePlatform()); - } + $this->normalizer ??= new JobExecutionRowNormalizer($this->connection->getDatabasePlatform()); return $this->normalizer; } diff --git a/src/batch-doctrine-dbal/src/DoctrineDBALQueryReader.php b/src/batch-doctrine-dbal/src/DoctrineDBALQueryReader.php index aff5801c..bf3fa58b 100644 --- a/src/batch-doctrine-dbal/src/DoctrineDBALQueryReader.php +++ b/src/batch-doctrine-dbal/src/DoctrineDBALQueryReader.php @@ -40,6 +40,7 @@ public function __construct(ConnectionRegistry $doctrine, string $sql, string $c /** * @inheritdoc + * @phpstan-return Generator> */ public function read(): Generator { diff --git a/src/batch-doctrine-dbal/src/JobExecutionRowNormalizer.php b/src/batch-doctrine-dbal/src/JobExecutionRowNormalizer.php index ccc6e69d..1eb26a26 100644 --- a/src/batch-doctrine-dbal/src/JobExecutionRowNormalizer.php +++ b/src/batch-doctrine-dbal/src/JobExecutionRowNormalizer.php @@ -31,6 +31,9 @@ public function __construct(AbstractPlatform $platform) $this->platform = $platform; } + /** + * @phpstan-return array + */ public function toRow(JobExecution $jobExecution): array { return [ @@ -48,6 +51,9 @@ public function toRow(JobExecution $jobExecution): array ]; } + /** + * @phpstan-param array $data + */ public function fromRow(array $data, JobExecution $parent = null): JobExecution { $data['status'] = intval($data['status']); @@ -93,6 +99,9 @@ public function fromRow(array $data, JobExecution $parent = null): JobExecution return $jobExecution; } + /** + * @phpstan-return array + */ public function toChildRow(JobExecution $jobExecution): array { return [ @@ -112,6 +121,9 @@ public function toChildRow(JobExecution $jobExecution): array * @param array|string $value * * @return array + * + * @phpstan-param array|string $value + * @phpstan-return array */ private function jsonFromString($value): array { @@ -135,6 +147,9 @@ private function dateFromString(?string $date): ?DateTimeImmutable return DateTimeImmutable::createFromFormat($this->platform->getDateTimeFormatString(), $date) ?: null; } + /** + * @phpstan-return array + */ private function failureToArray(Failure $failure): array { return [ @@ -146,6 +161,9 @@ private function failureToArray(Failure $failure): array ]; } + /** + * @phpstan-param array $array + */ private function failureFromArray(array $array): Failure { return new Failure( @@ -157,6 +175,9 @@ private function failureFromArray(array $array): Failure ); } + /** + * @phpstan-return array + */ private function warningToArray(Warning $warning): array { return [ @@ -166,6 +187,9 @@ private function warningToArray(Warning $warning): array ]; } + /** + * @phpstan-param array $array + */ private function warningFromArray(array $array): Warning { return new Warning($array['message'], $array['parameters'], $array['context']); diff --git a/src/batch-symfony-console/src/CommandRunner.php b/src/batch-symfony-console/src/CommandRunner.php index 9ea4ecd2..b23585f0 100644 --- a/src/batch-symfony-console/src/CommandRunner.php +++ b/src/batch-symfony-console/src/CommandRunner.php @@ -35,8 +35,7 @@ public function __construct(string $binDir, string $logDir, PhpExecutableFinder } /** - * @param string $commandName - * @param array $arguments + * @phpstan-param array $arguments */ public function run(string $commandName, array $arguments = []): void { @@ -44,9 +43,7 @@ public function run(string $commandName, array $arguments = []): void } /** - * @param string $commandName - * @param string $logFilename - * @param array $arguments + * @phpstan-param array $arguments */ public function runAsync(string $commandName, string $logFilename, array $arguments = []): void { @@ -64,6 +61,9 @@ protected function exec(string $command): void exec($command); } + /** + * @phpstan-param array $arguments + */ private function buildCommand(string $commandName, array $arguments): string { return sprintf( diff --git a/src/batch-symfony-console/src/RunJobCommand.php b/src/batch-symfony-console/src/RunJobCommand.php index ec00f226..cbd6f814 100644 --- a/src/batch-symfony-console/src/RunJobCommand.php +++ b/src/batch-symfony-console/src/RunJobCommand.php @@ -15,6 +15,8 @@ final class RunJobCommand extends Command { + protected static $defaultName = 'yokai:batch:run'; + public const EXIT_SUCCESS_CODE = 0; public const EXIT_ERROR_CODE = 1; public const EXIT_WARNING_CODE = 2; @@ -29,7 +31,7 @@ final class RunJobCommand extends Command */ public function __construct(JobLauncherInterface $jobLauncher) { - parent::__construct(null); + parent::__construct(); $this->jobLauncher = $jobLauncher; } @@ -38,10 +40,8 @@ public function __construct(JobLauncherInterface $jobLauncher) */ protected function configure(): void { - $this - ->setName('yokai:batch:run') - ->addArgument('job', InputArgument::REQUIRED) - ->addArgument('configuration', InputArgument::OPTIONAL); + $this->addArgument('job', InputArgument::REQUIRED); + $this->addArgument('configuration', InputArgument::OPTIONAL); } /** @@ -118,6 +118,8 @@ private function outputExecution(JobExecution $jobExecution, OutputInterface $ou * * @throws InvalidArgumentException * @return array + * + * @phpstan-return array */ private function decodeConfiguration(string $data): array { diff --git a/src/batch-symfony-framework/src/DependencyInjection/YokaiBatchExtension.php b/src/batch-symfony-framework/src/DependencyInjection/YokaiBatchExtension.php index 7a7c2f18..b3176d8c 100644 --- a/src/batch-symfony-framework/src/DependencyInjection/YokaiBatchExtension.php +++ b/src/batch-symfony-framework/src/DependencyInjection/YokaiBatchExtension.php @@ -24,6 +24,7 @@ final class YokaiBatchExtension extends Extension { /** * @inheritDoc + * @phpstan-param list> $configs */ public function load(array $configs, ContainerBuilder $container): void { @@ -79,6 +80,9 @@ private function getLoader(ContainerBuilder $container): ConfigLoader\LoaderInte return new ConfigLoader\DelegatingLoader($resolver); } + /** + * @param array $config + */ private function configureStorage(ContainerBuilder $container, array $config): void { if (isset($config['service'])) { diff --git a/src/batch-symfony-messenger/src/LaunchJobMessage.php b/src/batch-symfony-messenger/src/LaunchJobMessage.php index de2cf538..8c7c5e76 100644 --- a/src/batch-symfony-messenger/src/LaunchJobMessage.php +++ b/src/batch-symfony-messenger/src/LaunchJobMessage.php @@ -12,10 +12,13 @@ final class LaunchJobMessage private string $jobName; /** - * @var array + * @phpstan-var array */ private array $configuration; + /** + * @phpstan-param array $configuration + */ public function __construct(string $jobName, array $configuration = []) { $this->jobName = $jobName; @@ -27,6 +30,9 @@ public function getJobName(): string return $this->jobName; } + /** + * @phpstan-return array + */ public function getConfiguration(): array { return $this->configuration; diff --git a/src/batch-symfony-serializer/src/DenormalizeItemProcessor.php b/src/batch-symfony-serializer/src/DenormalizeItemProcessor.php index 48a29d4f..8305e9d5 100644 --- a/src/batch-symfony-serializer/src/DenormalizeItemProcessor.php +++ b/src/batch-symfony-serializer/src/DenormalizeItemProcessor.php @@ -27,10 +27,13 @@ final class DenormalizeItemProcessor implements ItemProcessorInterface private ?string $format; /** - * @var array + * @phpstan-var array */ private array $context; + /** + * @phpstan-param array $context + */ public function __construct( DenormalizerInterface $denormalizer, string $type, diff --git a/src/batch-symfony-serializer/src/NormalizeItemProcessor.php b/src/batch-symfony-serializer/src/NormalizeItemProcessor.php index 4336335d..7217203c 100644 --- a/src/batch-symfony-serializer/src/NormalizeItemProcessor.php +++ b/src/batch-symfony-serializer/src/NormalizeItemProcessor.php @@ -22,10 +22,13 @@ final class NormalizeItemProcessor implements ItemProcessorInterface private ?string $format; /** - * @var array + * @phpstan-var array */ private array $context; + /** + * @phpstan-param array $context + */ public function __construct(NormalizerInterface $normalizer, string $format = null, array $context = []) { $this->normalizer = $normalizer; diff --git a/src/batch-symfony-validator/src/SkipInvalidItemProcessor.php b/src/batch-symfony-validator/src/SkipInvalidItemProcessor.php index a0acb5c8..94b72604 100644 --- a/src/batch-symfony-validator/src/SkipInvalidItemProcessor.php +++ b/src/batch-symfony-validator/src/SkipInvalidItemProcessor.php @@ -18,10 +18,13 @@ final class SkipInvalidItemProcessor implements ItemProcessorInterface private ValidatorInterface $validator; /** - * @var array|null + * @var string[]|null */ private ?array $groups; + /** + * @param string[]|null $groups + */ public function __construct(ValidatorInterface $validator, array $groups = null) { $this->validator = $validator; diff --git a/src/batch/src/BatchStatus.php b/src/batch/src/BatchStatus.php index 56e7020a..f2524fc0 100644 --- a/src/batch/src/BatchStatus.php +++ b/src/batch/src/BatchStatus.php @@ -62,7 +62,7 @@ public function is(int $value): bool } /** - * @param array $values + * @param int[] $values * * @return bool */ @@ -84,7 +84,7 @@ public function isUnsuccessful(): bool */ public function isSuccessful(): bool { - return $this->isOneOf([self::COMPLETED]); + return $this->is(self::COMPLETED); } /** diff --git a/src/batch/src/Exception/UnexpectedValueException.php b/src/batch/src/Exception/UnexpectedValueException.php index 1052cf97..72c3e2fc 100644 --- a/src/batch/src/Exception/UnexpectedValueException.php +++ b/src/batch/src/Exception/UnexpectedValueException.php @@ -35,7 +35,7 @@ public static function type(string $expected, $argument, string $message = null) } /** - * @param array $expected + * @param mixed[] $expected * @param mixed $argument * @param string|null $message * diff --git a/src/batch/src/Factory/JobExecutionFactory.php b/src/batch/src/Factory/JobExecutionFactory.php index 1e7f7d8a..9ff32d9b 100644 --- a/src/batch/src/Factory/JobExecutionFactory.php +++ b/src/batch/src/Factory/JobExecutionFactory.php @@ -23,10 +23,7 @@ public function __construct(JobExecutionIdGeneratorInterface $idGenerator) } /** - * @param string $name - * @param array $configuration - * - * @return JobExecution + * @phpstan-param array $configuration */ public function create(string $name, array $configuration = []): JobExecution { diff --git a/src/batch/src/Failure.php b/src/batch/src/Failure.php index d030f40a..32009db3 100644 --- a/src/batch/src/Failure.php +++ b/src/batch/src/Failure.php @@ -24,7 +24,7 @@ final class Failure private int $code; /** - * @var array + * @phpstan-var array */ private array $parameters; @@ -34,11 +34,7 @@ final class Failure private ?string $trace; /** - * @param string $class - * @param string $message - * @param int $code - * @param array $parameters - * @param string|null $trace + * @phpstan-param array $parameters */ public function __construct( string $class, @@ -55,10 +51,7 @@ public function __construct( } /** - * @param Throwable $exception - * @param array $parameters - * - * @return Failure + * @phpstan-param array $parameters */ public static function fromException(Throwable $exception, array $parameters = []): self { @@ -104,7 +97,7 @@ public function getCode(): int } /** - * @return array + * @phpstan-return array */ public function getParameters(): array { diff --git a/src/batch/src/Job/Item/ExpandProcessedItem.php b/src/batch/src/Job/Item/ExpandProcessedItem.php index df6e9b16..2cc804a3 100644 --- a/src/batch/src/Job/Item/ExpandProcessedItem.php +++ b/src/batch/src/Job/Item/ExpandProcessedItem.php @@ -5,19 +5,41 @@ namespace Yokai\Batch\Job\Item; use ArrayIterator; +use Iterator; +use IteratorAggregate; use IteratorIterator; /** * A processor may return an element of this class * in order to write multiple items per item read. + * + * @template-implements IteratorAggregate */ -final class ExpandProcessedItem extends IteratorIterator +final class ExpandProcessedItem implements IteratorAggregate { - public function __construct(iterable $iterator, string $class = '') + /** + * @phpstan-var Iterator + */ + private Iterator $iterator; + + /** + * @phpstan-param iterable $iterator + */ + public function __construct(iterable $iterator) { if (\is_array($iterator)) { - $iterator = new ArrayIterator($iterator); + $this->iterator = new ArrayIterator($iterator); + } else { + $this->iterator = new IteratorIterator($iterator); } - parent::__construct($iterator, $class); + } + + /** + * @inheritdoc + * @phpstan-return Iterator + */ + public function getIterator(): Iterator + { + return $this->iterator; } } diff --git a/src/batch/src/Job/Item/InvalidItemException.php b/src/batch/src/Job/Item/InvalidItemException.php index 5e293882..59585f00 100644 --- a/src/batch/src/Job/Item/InvalidItemException.php +++ b/src/batch/src/Job/Item/InvalidItemException.php @@ -9,10 +9,13 @@ class InvalidItemException extends \RuntimeException { /** - * @var array + * @phpstan-var array */ private array $parameters; + /** + * @phpstan-param array $parameters + */ public function __construct(string $message, array $parameters = [], int $code = 0, Throwable $previous = null) { parent::__construct($message, $code, $previous); @@ -20,7 +23,7 @@ public function __construct(string $message, array $parameters = [], int $code = } /** - * @return array + * @phpstan-return array */ public function getParameters(): array { diff --git a/src/batch/src/Job/Item/ItemJob.php b/src/batch/src/Job/Item/ItemJob.php index 3dd314cd..57715dc9 100644 --- a/src/batch/src/Job/Item/ItemJob.php +++ b/src/batch/src/Job/Item/ItemJob.php @@ -34,7 +34,7 @@ class ItemJob extends AbstractJob private ItemWriterInterface $writer; /** - * @var array + * @phpstan-var list */ private array $elements; @@ -126,6 +126,8 @@ protected function doExecute(JobExecution $jobExecution): void * @param mixed $processedItem The processed item * * @return iterable A list of items to write + * + * @phpstan-return iterable */ protected function getItemsToWrite($processedItem): iterable { diff --git a/src/batch/src/Job/Item/ItemReaderInterface.php b/src/batch/src/Job/Item/ItemReaderInterface.php index 3e3b04cf..c0ed8399 100644 --- a/src/batch/src/Job/Item/ItemReaderInterface.php +++ b/src/batch/src/Job/Item/ItemReaderInterface.php @@ -8,6 +8,7 @@ interface ItemReaderInterface { /** * @return iterable + * @phpstan-return iterable */ public function read(): iterable; } diff --git a/src/batch/src/Job/Item/ItemWriterInterface.php b/src/batch/src/Job/Item/ItemWriterInterface.php index c8672ab8..a35e25c2 100644 --- a/src/batch/src/Job/Item/ItemWriterInterface.php +++ b/src/batch/src/Job/Item/ItemWriterInterface.php @@ -8,6 +8,7 @@ interface ItemWriterInterface { /** * @param iterable $items + * @phpstan-param iterable $items */ public function write(iterable $items): void; } diff --git a/src/batch/src/Job/Item/Processor/ArrayMapProcessor.php b/src/batch/src/Job/Item/Processor/ArrayMapProcessor.php index 82260390..1cb42915 100644 --- a/src/batch/src/Job/Item/Processor/ArrayMapProcessor.php +++ b/src/batch/src/Job/Item/Processor/ArrayMapProcessor.php @@ -19,6 +19,7 @@ public function __construct(Closure $callback) /** * @inheritdoc + * @phpstan-return array */ public function process($item): array { diff --git a/src/batch/src/Job/Item/Reader/StaticIterableReader.php b/src/batch/src/Job/Item/Reader/StaticIterableReader.php index 33290387..f906ea67 100644 --- a/src/batch/src/Job/Item/Reader/StaticIterableReader.php +++ b/src/batch/src/Job/Item/Reader/StaticIterableReader.php @@ -9,10 +9,13 @@ final class StaticIterableReader implements ItemReaderInterface { /** - * @var iterable + * @phpstan-var iterable */ private iterable $items; + /** + * @phpstan-param iterable $items + */ public function __construct(iterable $items) { $this->items = $items; diff --git a/src/batch/src/JobExecution.php b/src/batch/src/JobExecution.php index 76620f54..5ffaf3e3 100644 --- a/src/batch/src/JobExecution.php +++ b/src/batch/src/JobExecution.php @@ -321,9 +321,7 @@ public function addFailure(Failure $failure, bool $log = true): void } /** - * @param Throwable $exception - * @param array $parameters - * @param bool $log + * @phpstan-param array $parameters */ public function addFailureException(Throwable $exception, array $parameters = [], bool $log = true): void { diff --git a/src/batch/src/JobParameters.php b/src/batch/src/JobParameters.php index 19243e3c..831b8f37 100644 --- a/src/batch/src/JobParameters.php +++ b/src/batch/src/JobParameters.php @@ -6,21 +6,23 @@ use ArrayIterator; use Countable; -use Iterator; use IteratorAggregate; use Yokai\Batch\Exception\UndefinedJobParameterException; +/** + * @template-implements IteratorAggregate + */ final class JobParameters implements Countable, IteratorAggregate { /** - * @var array + * @phpstan-var array */ private array $parameters; /** - * @param array $parameters + * @phpstan-param array $parameters */ public function __construct(array $parameters = []) { @@ -61,8 +63,9 @@ public function count(): int /** * @inheritDoc + * @phpstan-return ArrayIterator */ - public function getIterator(): Iterator + public function getIterator(): ArrayIterator { return new ArrayIterator($this->parameters); } diff --git a/src/batch/src/Launcher/JobLauncherInterface.php b/src/batch/src/Launcher/JobLauncherInterface.php index b494c925..045b57e5 100644 --- a/src/batch/src/Launcher/JobLauncherInterface.php +++ b/src/batch/src/Launcher/JobLauncherInterface.php @@ -13,6 +13,8 @@ interface JobLauncherInterface * @param array $configuration * * @return JobExecution + * + * @phpstan-param array $configuration */ public function launch(string $name, array $configuration = []): JobExecution; } diff --git a/src/batch/src/Launcher/SimpleJobLauncher.php b/src/batch/src/Launcher/SimpleJobLauncher.php index e96ce641..c7fb242c 100644 --- a/src/batch/src/Launcher/SimpleJobLauncher.php +++ b/src/batch/src/Launcher/SimpleJobLauncher.php @@ -105,6 +105,9 @@ private function store(JobExecution $execution): void $this->jobExecutionStorage->store($execution); } + /** + * @phpstan-param array $configuration + */ private function getJobExecution(string $name, array $configuration): JobExecution { $id = $configuration['_id'] ?? null; diff --git a/src/batch/src/Serializer/JsonJobExecutionSerializer.php b/src/batch/src/Serializer/JsonJobExecutionSerializer.php index a747c05f..4d70028e 100644 --- a/src/batch/src/Serializer/JsonJobExecutionSerializer.php +++ b/src/batch/src/Serializer/JsonJobExecutionSerializer.php @@ -71,6 +71,9 @@ public function extension(): string return 'json'; } + /** + * @phpstan-return array + */ private function toArray(JobExecution $jobExecution): array { return [ @@ -88,6 +91,9 @@ private function toArray(JobExecution $jobExecution): array ]; } + /** + * @phpstan-param array $jobExecutionData + */ private function fromArray(array $jobExecutionData, JobExecution $parentExecution = null): JobExecution { $name = $jobExecutionData['jobName']; @@ -149,6 +155,9 @@ private function stringToDate(?string $date): ?DateTimeInterface return $dateObject; } + /** + * @phpstan-return array + */ private function failureToArray(Failure $failure): array { return [ @@ -160,6 +169,9 @@ private function failureToArray(Failure $failure): array ]; } + /** + * @phpstan-param array $array + */ private function failureFromArray(array $array): Failure { return new Failure( @@ -171,6 +183,9 @@ private function failureFromArray(array $array): Failure ); } + /** + * @phpstan-return array + */ private function warningToArray(Warning $warning): array { return [ @@ -180,6 +195,9 @@ private function warningToArray(Warning $warning): array ]; } + /** + * @phpstan-param array $array + */ private function warningFromArray(array $array): Warning { return new Warning($array['message'], $array['parameters'], $array['context']); diff --git a/src/batch/src/Storage/Query.php b/src/batch/src/Storage/Query.php index bfda649d..989a223b 100644 --- a/src/batch/src/Storage/Query.php +++ b/src/batch/src/Storage/Query.php @@ -42,6 +42,13 @@ final class Query private int $offset; /** + * @param string[] $jobNames + * @param string[] $ids + * @param int[] $statuses + * @param string|null $sortBy + * @param int $limit + * @param int $offset + * * @internal */ public function __construct( @@ -60,16 +67,25 @@ public function __construct( $this->offset = $offset; } + /** + * @return string[] + */ public function jobs(): array { return $this->jobNames; } + /** + * @return string[] + */ public function ids(): array { return $this->ids; } + /** + * @return int[] + */ public function statuses(): array { return $this->statuses; diff --git a/src/batch/src/Storage/QueryBuilder.php b/src/batch/src/Storage/QueryBuilder.php index 91f027cd..2ae110aa 100644 --- a/src/batch/src/Storage/QueryBuilder.php +++ b/src/batch/src/Storage/QueryBuilder.php @@ -55,6 +55,11 @@ final class QueryBuilder */ private int $offset = 0; + /** + * @param string[] $names + * + * @return $this + */ public function jobs(array $names): self { $names = array_unique($names); @@ -69,6 +74,11 @@ public function jobs(array $names): self return $this; } + /** + * @param string[] $ids + * + * @return $this + */ public function ids(array $ids): self { $ids = array_unique($ids); @@ -83,6 +93,11 @@ public function ids(array $ids): self return $this; } + /** + * @param int[] $statuses + * + * @return $this + */ public function statuses(array $statuses): self { $statuses = array_unique($statuses); diff --git a/src/batch/src/Summary.php b/src/batch/src/Summary.php index 2f77fc1c..98023104 100644 --- a/src/batch/src/Summary.php +++ b/src/batch/src/Summary.php @@ -8,17 +8,20 @@ use Countable; use IteratorAggregate; +/** + * @template-implements IteratorAggregate + */ final class Summary implements Countable, IteratorAggregate { /** - * @var array + * @phpstan-var array */ private array $values; /** - * @param array $values + * @phpstan-param array $values */ public function __construct(array $values = []) { @@ -54,15 +57,13 @@ public function get(string $key) } /** - * @return array + * @phpstan-return array */ public function all(): array { return $this->values; } - /** - */ public function clear(): void { $this->values = []; @@ -70,8 +71,9 @@ public function clear(): void /** * @inheritdoc + * @phpstan-return ArrayIterator */ - public function getIterator() + public function getIterator(): ArrayIterator { return new ArrayIterator($this->values); } @@ -79,7 +81,7 @@ public function getIterator() /** * @inheritdoc */ - public function count() + public function count(): int { return count($this->values); } diff --git a/src/batch/src/Test/Factory/SequenceJobExecutionIdGenerator.php b/src/batch/src/Test/Factory/SequenceJobExecutionIdGenerator.php index 2da4b535..04dd60a3 100644 --- a/src/batch/src/Test/Factory/SequenceJobExecutionIdGenerator.php +++ b/src/batch/src/Test/Factory/SequenceJobExecutionIdGenerator.php @@ -8,14 +8,23 @@ final class SequenceJobExecutionIdGenerator implements JobExecutionIdGeneratorInterface { + /** + * @phpstan-var list + */ private array $sequence; private int $current = 0; + /** + * @phpstan-param list $sequence + */ public function __construct(array $sequence) { $this->sequence = \array_values($sequence); } + /** + * @inheritdoc + */ public function generate(): string { $current = $this->sequence[$this->current] ?? ''; diff --git a/src/batch/src/Test/Job/Item/Reader/TestDebugReader.php b/src/batch/src/Test/Job/Item/Reader/TestDebugReader.php index a1f8638e..cd227d03 100644 --- a/src/batch/src/Test/Job/Item/Reader/TestDebugReader.php +++ b/src/batch/src/Test/Job/Item/Reader/TestDebugReader.php @@ -18,12 +18,18 @@ public function __construct(ItemReaderInterface $decorated) $this->decorated = $decorated; } + /** + * @inheritdoc + */ public function initialize(): void { $this->read = false; parent::initialize(); } + /** + * @inheritdoc + */ public function read(): iterable { $this->read = true; diff --git a/src/batch/src/Test/Job/Item/Writer/InMemoryWriter.php b/src/batch/src/Test/Job/Item/Writer/InMemoryWriter.php index 184660ae..7769999c 100644 --- a/src/batch/src/Test/Job/Item/Writer/InMemoryWriter.php +++ b/src/batch/src/Test/Job/Item/Writer/InMemoryWriter.php @@ -9,13 +9,22 @@ final class InMemoryWriter implements ItemWriterInterface, InitializableInterface { + /** + * @phpstan-var list + */ private array $items = []; + /** + * @inheritdoc + */ public function initialize(): void { $this->items = []; } + /** + * @inheritdoc + */ public function write(iterable $items): void { foreach ($items as $item) { @@ -23,6 +32,9 @@ public function write(iterable $items): void } } + /** + * @phpstan-return list + */ public function getItems(): array { return $this->items; diff --git a/src/batch/src/Test/Job/Item/Writer/TestDebugWriter.php b/src/batch/src/Test/Job/Item/Writer/TestDebugWriter.php index 8a57199f..44636a45 100644 --- a/src/batch/src/Test/Job/Item/Writer/TestDebugWriter.php +++ b/src/batch/src/Test/Job/Item/Writer/TestDebugWriter.php @@ -18,12 +18,18 @@ public function __construct(ItemWriterInterface $decorated) $this->decorated = $decorated; } + /** + * @inheritdoc + */ public function initialize(): void { $this->written = false; parent::initialize(); } + /** + * @inheritdoc + */ public function write(iterable $items): void { $this->written = true; diff --git a/src/batch/src/Test/Launcher/BufferingJobLauncher.php b/src/batch/src/Test/Launcher/BufferingJobLauncher.php index 2d7d93ef..8567b7d8 100644 --- a/src/batch/src/Test/Launcher/BufferingJobLauncher.php +++ b/src/batch/src/Test/Launcher/BufferingJobLauncher.php @@ -12,6 +12,10 @@ final class BufferingJobLauncher implements JobLauncherInterface { private JobExecutionIdGeneratorInterface $idGenerator; + + /** + * @var JobExecution[] + */ private array $executions = []; public function __construct(JobExecutionIdGeneratorInterface $idGenerator) @@ -19,6 +23,9 @@ public function __construct(JobExecutionIdGeneratorInterface $idGenerator) $this->idGenerator = $idGenerator; } + /** + * @inheritdoc + */ public function launch(string $name, array $configuration = []): JobExecution { $configuration['_id'] ??= $this->idGenerator->generate(); diff --git a/src/batch/src/Trigger/Scheduler/CallbackScheduler.php b/src/batch/src/Trigger/Scheduler/CallbackScheduler.php index c326c309..fc5ee5e5 100644 --- a/src/batch/src/Trigger/Scheduler/CallbackScheduler.php +++ b/src/batch/src/Trigger/Scheduler/CallbackScheduler.php @@ -43,6 +43,10 @@ public function __construct(array $config) } } + /** + * @inheritdoc + * @phpstan-return Generator + */ public function get(JobExecution $execution): Generator { /** @var callable $callback */ diff --git a/src/batch/src/Trigger/Scheduler/ScheduledJob.php b/src/batch/src/Trigger/Scheduler/ScheduledJob.php index fe46829a..245efd0e 100644 --- a/src/batch/src/Trigger/Scheduler/ScheduledJob.php +++ b/src/batch/src/Trigger/Scheduler/ScheduledJob.php @@ -10,9 +10,15 @@ final class ScheduledJob { private string $jobName; + /** + * @phpstan-var array + */ private array $parameters; private ?string $id; + /** + * @phpstan-param array $parameters + */ public function __construct(string $jobName, array $parameters = [], string $id = null) { $this->jobName = $jobName; @@ -34,6 +40,7 @@ public function getJobName(): string * The job parameters for the job to trigger. * * @return array + * @phpstan-return array */ public function getParameters(): array { diff --git a/src/batch/src/Warning.php b/src/batch/src/Warning.php index 284d0f40..3382ae78 100644 --- a/src/batch/src/Warning.php +++ b/src/batch/src/Warning.php @@ -13,14 +13,20 @@ final class Warning /** * @var array + * @phpstan-var array */ private array $parameters; /** * @var array + * @phpstan-var array */ private array $context; + /** + * @phpstan-param array $parameters + * @phpstan-param array $context + */ public function __construct(string $message, array $parameters = [], array $context = []) { $this->message = $message; @@ -46,13 +52,16 @@ public function getMessage(): string /** * @return array + * @phpstan-return array */ public function getParameters(): array { return $this->parameters; } + /** * @return array + * @phpstan-return array */ public function getContext(): array {