diff --git a/.phan.php b/.phan.php index 64820b43..8780395e 100644 --- a/.phan.php +++ b/.phan.php @@ -16,8 +16,19 @@ $default = include __DIR__ . '/vendor/jbzoo/codestyle/src/phan.php'; +// Remove SimplifyExpressionPlugin from plugin list +$default['plugins'] = \array_diff($default['plugins'], ['SimplifyExpressionPlugin']); + return \array_merge($default, [ 'directory_list' => [ 'src', + + 'vendor/jbzoo/data/src', + 'vendor/jbzoo/cli/src', + 'vendor/jbzoo/utils/src', + 'vendor/jbzoo/ci-report-converter/src', + 'vendor/league/csv/src', + 'vendor/fakerphp/faker/src', + 'vendor/symfony/console', ], ]); diff --git a/composer.json b/composer.json index b78cd5e6..ef26dcbe 100644 --- a/composer.json +++ b/composer.json @@ -26,15 +26,15 @@ "prefer-stable" : true, "require" : { - "php" : "^8.1", - "ext-mbstring" : "*", - - "jbzoo/data" : "^7.1", - "jbzoo/cli" : "^7.1", - "jbzoo/utils" : "^7.1", - "league/csv" : "^9.15", - "fakerphp/faker" : "^1.23", - "jbzoo/ci-report-converter": "^7.2" + "php" : "^8.1", + "ext-mbstring" : "*", + + "jbzoo/data" : "^7.1", + "jbzoo/cli" : "^7.1", + "jbzoo/utils" : "^7.1", + "jbzoo/ci-report-converter" : "^7.2", + "league/csv" : "^9.15", + "fakerphp/faker" : "^1.23" }, "require-dev" : { diff --git a/composer.lock b/composer.lock index e87cb2a4..b3c61e26 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "661fc4e2593d0d9598e69e2de68aca8f", + "content-hash": "aa2d64e2d78d191e4bc83a4ad90051b6", "packages": [ { "name": "bluepsyduck/symfony-process-manager", diff --git a/src/Csv/Column.php b/src/Csv/Column.php index 9096305f..1cc88b66 100644 --- a/src/Csv/Column.php +++ b/src/Csv/Column.php @@ -118,10 +118,10 @@ private function prepareRuleSet(string $schemaKey): array { $rules = []; - $ruleSetConfig = $this->column->getSelf($schemaKey, []); + $ruleSetConfig = $this->column->getSelf($schemaKey, [])->getArrayCopy(); foreach ($ruleSetConfig as $ruleName => $ruleValue) { - if (\str_starts_with($ruleName, 'custom_')) { + if (\str_starts_with((string)$ruleName, 'custom_')) { $rules[$ruleName] = \array_merge(['class' => '', 'args' => []], $ruleValue); } else { $rules[$ruleName] = $ruleValue; diff --git a/src/Csv/CsvFile.php b/src/Csv/CsvFile.php index 0c03d038..d39e35ac 100644 --- a/src/Csv/CsvFile.php +++ b/src/Csv/CsvFile.php @@ -43,7 +43,7 @@ public function __construct(string $csvFilename, null|array|string $csvSchemaFil public function getCsvFilename(): string { - return \str_replace(PROJECT_ROOT, '.', (string)\realpath($this->csvFilename)); + return \pathinfo((string)\realpath($this->csvFilename), \PATHINFO_BASENAME); } public function getCsvStructure(): ParseConfig @@ -79,7 +79,7 @@ public function validate(bool $quickStop = false): ErrorSuite $errors->addErrorSuit($this->validateHeader()) ->addErrorSuit($this->validateEachCell($quickStop)) - ->addErrorSuit($this->validateAggregateRules($quickStop)); + ->addErrorSuit(self::validateAggregateRules($quickStop)); return $errors; } @@ -137,7 +137,7 @@ private function validateEachCell(bool $quickStop = false): ErrorSuite continue; } - $errors->addErrorSuit($column->validate($record[$column->getKey()], $line + 1)); + $errors->addErrorSuit($column->validate($record[$column->getKey()], (int)$line + 1)); if ($quickStop && $errors->count() > 0) { return $errors; } @@ -147,7 +147,7 @@ private function validateEachCell(bool $quickStop = false): ErrorSuite return $errors; } - private function validateAggregateRules(bool $quickStop = false): ErrorSuite + private static function validateAggregateRules(bool $quickStop = false): ErrorSuite { $errors = new ErrorSuite(); diff --git a/src/Schema.php b/src/Schema.php index d3f7e084..acbe0b84 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -84,14 +84,14 @@ public function getColumns(): array /** * @return Column[]|null[] + * @phan-suppress PhanPartialTypeMismatchReturn */ public function getColumnsMappedByHeader(array $header): array { $map = []; foreach ($header as $headerName) { - $column = $this->columns[$headerName] ?? null; - $map[$headerName] = $column; + $map[$headerName] = $this->columns[$headerName] ?? null; } return $map; @@ -141,7 +141,7 @@ private function prepareColumns(): array $result = []; foreach ($this->data->getArray('columns') as $columnId => $columnPreferences) { - $column = new Column($columnId, $columnPreferences); + $column = new Column((int)$columnId, $columnPreferences); $result[$column->getKey()] = $column; } diff --git a/src/Validators/Rules/MaxDate.php b/src/Validators/Rules/MaxDate.php index addb827b..434ceeb2 100644 --- a/src/Validators/Rules/MaxDate.php +++ b/src/Validators/Rules/MaxDate.php @@ -23,7 +23,7 @@ public function validateRule(?string $cellValue): ?string $minDate = $this->getOptionAsDate(); $cellDate = new \DateTimeImmutable((string)$cellValue); - if ($cellDate > $minDate) { + if ($cellDate->getTimestamp() > $minDate->getTimestamp()) { return "Value \"{$cellValue}\" is more than the maximum " . "date \"{$minDate->format(\DATE_RFC3339_EXTENDED)}\""; } diff --git a/src/Validators/Rules/MinDate.php b/src/Validators/Rules/MinDate.php index 73bb3b39..225a4c43 100644 --- a/src/Validators/Rules/MinDate.php +++ b/src/Validators/Rules/MinDate.php @@ -23,7 +23,7 @@ public function validateRule(?string $cellValue): ?string $minDate = $this->getOptionAsDate(); $cellDate = new \DateTimeImmutable((string)$cellValue); - if ($cellDate < $minDate) { + if ($cellDate->getTimestamp() < $minDate->getTimestamp()) { return "Value \"{$cellValue}\" is less than the minimum " . "date \"{$minDate->format(\DATE_RFC3339_EXTENDED)}\""; } diff --git a/src/Validators/Rules/Precision.php b/src/Validators/Rules/Precision.php index 6fd59e70..e1bfe56c 100644 --- a/src/Validators/Rules/Precision.php +++ b/src/Validators/Rules/Precision.php @@ -20,16 +20,17 @@ final class Precision extends AbstarctRule { public function validateRule(?string $cellValue): ?string { - if ($this->getOptionAsInt() !== $this->getFloatPrecision($cellValue)) { - return "Value \"{$cellValue}\" has a precision of " . $this->getFloatPrecision( - $cellValue, - ) . ' but should have a precision of ' . $this->getOptionAsInt(); + $valuePrecision = self::getFloatPrecision($cellValue); + + if ($this->getOptionAsInt() !== $valuePrecision) { + return "Value \"{$cellValue}\" has a precision of {$valuePrecision} " . + "but should have a precision of {$this->getOptionAsInt()}"; } return null; } - private function getFloatPrecision(?string $cellValue): int + private static function getFloatPrecision(?string $cellValue): int { $floatAsString = (string)$cellValue; $dotPosition = \strpos($floatAsString, '.'); diff --git a/src/Validators/Ruleset.php b/src/Validators/Ruleset.php index 4f1565ce..42efff96 100644 --- a/src/Validators/Ruleset.php +++ b/src/Validators/Ruleset.php @@ -31,7 +31,7 @@ public function __construct(array $rules, string $columnNameId) $this->rules = []; foreach ($rules as $ruleName => $options) { - $this->rules[] = $this->createRule($ruleName, $options); + $this->rules[] = $this->createRule((string)$ruleName, $options); } } diff --git a/tests/Blueprint/CsvReaderTest.php b/tests/Blueprint/CsvReaderTest.php index 6ec33902..4291b685 100644 --- a/tests/Blueprint/CsvReaderTest.php +++ b/tests/Blueprint/CsvReaderTest.php @@ -32,7 +32,7 @@ final class CsvReaderTest extends PHPUnit public function testReadCsvFileWithoutHeader(): void { $csv = new CsvFile(self::CSV_SIMPLE_NO_HEADER, self::SCHEMA_SIMPLE_NO_HEADER); - isSame('./tests/fixtures/simple_no_header.csv', $csv->getCsvFilename()); + isSame('simple_no_header.csv', $csv->getCsvFilename()); isSame([], $csv->getHeader()); @@ -50,7 +50,7 @@ public function testReadCsvFileWithoutHeader(): void public function testReadCsvFileWithHeader(): void { $csv = new CsvFile(self::CSV_SIMPLE_HEADER, self::SCHEMA_SIMPLE_HEADER); - isSame('./tests/fixtures/simple_header.csv', $csv->getCsvFilename()); + isSame('simple_header.csv', $csv->getCsvFilename()); isSame(['seq', 'bool', 'exact'], $csv->getHeader());