From 859231488a7c2dbe0fc1a4ce447bdbdf2d125898 Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Mon, 11 Mar 2024 01:59:56 +0400 Subject: [PATCH] Phan fixed! --- src/Commands/CreateCsv.php | 3 +++ src/Commands/CreateSchema.php | 3 +++ src/Commands/ValidateCsv.php | 3 +++ src/Commands/ValidateDir.php | 3 +++ src/Csv/CsvFile.php | 5 +---- src/Schema.php | 5 +++++ src/Validators/ErrorSuite.php | 15 ++++++++------- src/Validators/Rules/Regex.php | 6 +++++- src/Validators/Rules/UsaMarketName.php | 2 +- src/Validators/Ruleset.php | 5 +++++ 10 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/Commands/CreateCsv.php b/src/Commands/CreateCsv.php index 7eac8231..0128051f 100644 --- a/src/Commands/CreateCsv.php +++ b/src/Commands/CreateCsv.php @@ -20,6 +20,9 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; +/** + * @psalm-suppress PropertyNotSetInConstructor + */ final class CreateCsv extends CliCommand { protected function configure(): void diff --git a/src/Commands/CreateSchema.php b/src/Commands/CreateSchema.php index 4b886d28..feadeafa 100644 --- a/src/Commands/CreateSchema.php +++ b/src/Commands/CreateSchema.php @@ -20,6 +20,9 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; +/** + * @psalm-suppress PropertyNotSetInConstructor + */ final class CreateSchema extends CliCommand { protected function configure(): void diff --git a/src/Commands/ValidateCsv.php b/src/Commands/ValidateCsv.php index d0b6c3e7..091396c3 100644 --- a/src/Commands/ValidateCsv.php +++ b/src/Commands/ValidateCsv.php @@ -23,6 +23,9 @@ use JBZoo\CsvBlueprint\Validators\ErrorSuite; use Symfony\Component\Console\Input\InputOption; +/** + * @psalm-suppress PropertyNotSetInConstructor + */ final class ValidateCsv extends CliCommand { protected function configure(): void diff --git a/src/Commands/ValidateDir.php b/src/Commands/ValidateDir.php index 4d4d6a7a..c3785aba 100644 --- a/src/Commands/ValidateDir.php +++ b/src/Commands/ValidateDir.php @@ -19,6 +19,9 @@ use JBZoo\Cli\CliCommand; use Symfony\Component\Console\Input\InputArgument; +/** + * @psalm-suppress PropertyNotSetInConstructor + */ final class ValidateDir extends CliCommand { protected function configure(): void diff --git a/src/Csv/CsvFile.php b/src/Csv/CsvFile.php index 473a485e..0c03d038 100644 --- a/src/Csv/CsvFile.php +++ b/src/Csv/CsvFile.php @@ -63,10 +63,7 @@ public function getHeader(): array return []; } - /** - * @return iterable|\League\Csv\MapIterator - */ - public function getRecords(): iterable + public function getRecords(): \Iterator { return $this->reader->getRecords($this->getHeader()); } diff --git a/src/Schema.php b/src/Schema.php index d4e12c9d..d3f7e084 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -44,7 +44,9 @@ public function __construct(null|array|string $csvSchemaFilenameOrArray = null) && \file_exists($csvSchemaFilenameOrArray) ) { $this->filename = $csvSchemaFilenameOrArray; + $this->data = new Data(); $fileExtension = \pathinfo($csvSchemaFilenameOrArray, \PATHINFO_EXTENSION); + if ($fileExtension === 'yml' || $fileExtension === 'yaml') { $this->data = yml($csvSchemaFilenameOrArray); } elseif ($fileExtension === 'json') { @@ -54,6 +56,9 @@ public function __construct(null|array|string $csvSchemaFilenameOrArray = null) } else { throw new \InvalidArgumentException("Unsupported file extension: {$fileExtension}"); } + } else { + $this->filename = null; + $this->data = new Data(); } $this->columns = $this->prepareColumns(); diff --git a/src/Validators/ErrorSuite.php b/src/Validators/ErrorSuite.php index f97c6119..35055d6b 100644 --- a/src/Validators/ErrorSuite.php +++ b/src/Validators/ErrorSuite.php @@ -51,13 +51,14 @@ public function render(string $mode = self::RENDER_TEXT): string return ''; } - $map = [ - self::RENDER_TEXT => fn () => $this->renderPlainText(), - self::RENDER_TABLE => fn () => $this->renderTable(), - self::RENDER_GITHUB => fn () => (new GithubCliConverter())->fromInternal($this->prepareSourceSuite()), - self::RENDER_GITLAB => fn () => (new GitLabJsonConverter())->fromInternal($this->prepareSourceSuite()), - self::RENDER_TEAMCITY => fn () => (new TeamCityTestsConverter())->fromInternal($this->prepareSourceSuite()), - self::RENDER_JUNIT => fn () => (new JUnitConverter())->fromInternal($this->prepareSourceSuite()), + $sourceSuite = $this->prepareSourceSuite(); + $map = [ + self::RENDER_TEXT => fn (): string => $this->renderPlainText(), + self::RENDER_TABLE => fn (): string => $this->renderTable(), + self::RENDER_GITHUB => static fn (): string => (new GithubCliConverter())->fromInternal($sourceSuite), + self::RENDER_GITLAB => static fn (): string => (new GitLabJsonConverter())->fromInternal($sourceSuite), + self::RENDER_TEAMCITY => static fn (): string => (new TeamCityTestsConverter())->fromInternal($sourceSuite), + self::RENDER_JUNIT => static fn (): string => (new JUnitConverter())->fromInternal($sourceSuite), ]; if (isset($map[$mode])) { diff --git a/src/Validators/Rules/Regex.php b/src/Validators/Rules/Regex.php index 057abde1..27b707ea 100644 --- a/src/Validators/Rules/Regex.php +++ b/src/Validators/Rules/Regex.php @@ -24,7 +24,11 @@ public function validateRule(?string $cellValue): ?string { $regex = Utils::prepareRegex($this->getOptionAsString()); - if (\preg_match((string)$regex, (string)$cellValue) === 0) { + if ($regex === null || $regex === '') { + return 'Regex pattern is not defined'; + } + + if (\preg_match($regex, (string)$cellValue) === 0) { return "Value \"{$cellValue}\" does not match the pattern \"{$regex}\""; } diff --git a/src/Validators/Rules/UsaMarketName.php b/src/Validators/Rules/UsaMarketName.php index d570cfcc..365bf4c5 100644 --- a/src/Validators/Rules/UsaMarketName.php +++ b/src/Validators/Rules/UsaMarketName.php @@ -25,7 +25,7 @@ public function validateRule(?string $cellValue): ?string } if (\preg_match('/^[A-Za-z0-9\s-]+, [A-Z]{2}$/u', (string)$cellValue) === 0) { - return 'Invalid market name format for value "' . $cellValue . '". ' . + return "Invalid market name format for value \"{$cellValue}\". " . 'Market name must have format "New York, NY"'; } diff --git a/src/Validators/Ruleset.php b/src/Validators/Ruleset.php index 5d3a6b7e..4f1565ce 100644 --- a/src/Validators/Ruleset.php +++ b/src/Validators/Ruleset.php @@ -28,12 +28,17 @@ final class Ruleset public function __construct(array $rules, string $columnNameId) { $this->columnNameId = $columnNameId; + $this->rules = []; foreach ($rules as $ruleName => $options) { $this->rules[] = $this->createRule($ruleName, $options); } } + /** + * @psalm-suppress MoreSpecificReturnType + * @psalm-suppress LessSpecificReturnStatement + */ public function createRule(string $ruleName, null|array|bool|float|int|string $options = null): AbstarctRule { $classname = __NAMESPACE__ . '\\Rules\\' . Utils::kebabToCamelCase($ruleName);