Skip to content

Commit

Permalink
Fix global phpstan errors (#1350)
Browse files Browse the repository at this point in the history
* Added missing path to libs to rector configuration

* Fixed phpstan globally ignored errors

* Updated DSL

* More CS Fixes

* Build docs
  • Loading branch information
norberttech authored Jan 10, 2025
1 parent 056ebe0 commit ca840c8
Show file tree
Hide file tree
Showing 183 changed files with 1,385 additions and 1,115 deletions.
5 changes: 5 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
__DIR__ . '/web/**/tests',
__DIR__ . '/examples',
__DIR__ . '/tools/rector/src',
])
->exclude([
__DIR__ . '/src/lib/parquet/src/Flow/Parquet/Thrift'
]);

if (!\file_exists(__DIR__ . '/var')) {
Expand Down Expand Up @@ -146,6 +149,8 @@
'ternary_to_null_coalescing' => true,
'yoda_style' => false,
'void_return' => true,
'fully_qualified_strict_types' => false,
'phpdoc_array_type' => true,
'php_unit_method_casing' => ['case' => 'snake_case'],
'php_unit_test_case_static_method_calls' => ['call_type' => 'static'],
])
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@
"tools/cs-fixer/vendor/bin/php-cs-fixer fix --dry-run"
],
"cs:php:fix": [
"tools/cs-fixer/vendor/bin/php-cs-fixer fix"
"tools/cs-fixer/vendor/bin/php-cs-fixer fix",
"./tools/rector/vendor/bin/rector -c ./rector.src.php",
"./tools/rector/vendor/bin/rector -c ./rector.tests.php"
],
"build:phar": [
"composer update --working-dir=./src/cli",
Expand Down
12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ parameters:
treatPhpDocTypesAsCertain: false
ignoreErrors:
- identifier: argument.type
paths:
- src/core/etl/tests/*
- src/bridge/filesystem/azure/tests/*
- src/bridge/filesystem/async-aws/tests/*
- src/adapter/etl-adapter-xml/tests/*
- src/adapter/etl-adapter-text/tests/*
- src/adapter/etl-adapter-parquet/tests/*
- src/adapter/etl-adapter-meilisearch/tests/*
- src/adapter/etl-adapter-logger/tests/*
- src/adapter/etl-adapter-json/tests/*
- src/adapter/etl-adapter-http/tests/*
- src/adapter/etl-adapter-csv/tests/*
- identifier: missingType.iterableValue
bootstrapFiles:
- tools/phpunit/vendor/autoload.php
Expand Down
2 changes: 2 additions & 0 deletions rector.src.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
__DIR__ . '/examples',
__DIR__ . '/src/core/etl/src',
__DIR__ . '/src/cli/src',
__DIR__ . '/src/lib/*/src',
__DIR__ . '/src/adapter/*/src',
__DIR__ . '/src/bridge/*/*/src',
__DIR__ . '/src/tools/*/*/src',
])
->withSkipPath(__DIR__ . '/src/lib/parquet/src/Flow/Parquet/Thrift')
->withCache(__DIR__ . '/var/rector/src')
->withSets([
LevelSetList::UP_TO_PHP_82
Expand Down
2 changes: 2 additions & 0 deletions rector.tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
->withPaths([
__DIR__ . '/src/core/etl/tests',
__DIR__ . '/src/cli/tests',
__DIR__ . '/src/lib/*/tests',
__DIR__ . '/src/adapter/*/tests',
__DIR__ . '/src/bridge/*/*/tests',
__DIR__ . '/src/tools/*/*/tests',
Expand Down Expand Up @@ -158,6 +159,7 @@
RemoveParentCallWithoutParentRector::class
])
->withCache(__DIR__ . '/var/rector/tests')
->withSkipPath(__DIR__ . '/src/lib/parquet/src/Flow/Parquet/Thrift')
->withImportNames(
importNames: true,
importDocBlockNames: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
final class HashIdFactory implements IdFactory
{
/**
* @var string[]
* @var array<string>
*/
private readonly array $entryNames;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public function extract(FlowContext $context) : \Generator
);

/**
* @var array[] $values
* @var array<array> $values
*/
$values = $response->getValues() ?? [];

if ($this->withHeader && [] !== $values) {
/** @var string[] $headers */
/** @var array<string> $headers */
$headers = $values[0];
unset($values[0]);
$totalRows = 1;
Expand Down Expand Up @@ -108,7 +108,7 @@ function (array $rowData) use ($headers, $shouldPutInputIntoRows) {
/** @var Sheets\ValueRange $response */
$response = $this->service->spreadsheets_values->get($this->spreadsheetId, $cellsRange->toString(), $this->options);
/**
* @var array[] $values
* @var array<array> $values
*/
$values = $response->getValues() ?? [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
public function normalize(string $name, Type $type, mixed $value) : XMLNode|XMLAttribute
{
if (\str_starts_with($name, $this->attributePrefix)) {
return new XMLAttribute(\substr($name, \strlen($this->attributePrefix)), $this->caster->to(type_string())->value($value));
return new XMLAttribute(\substr($name, \strlen($this->attributePrefix)), (string) $this->caster->to(type_string())->value($value));
}

if ($value === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private function createDOMElement(\DOMDocument $dom, XMLNode $node) : \DOMElemen
$element = $dom->createElement($node->name);

if ($node->hasValue()) {
$element->appendChild($dom->createTextNode($node->value));
$element->appendChild($dom->createTextNode((string) $node->value));
}

foreach ($node->attributes as $attribute) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function from_xml(
* @param string $row_element_name - @deprecated use `withRowElementName()` method instead
* @param string $attribute_prefix - @deprecated use `withAttributePrefix()` method instead
* @param string $date_time_format - @deprecated use `withDateTimeFormat()` method instead
* @param DOMDocumentWriter $xml_writer
* @param XMLWriter $xml_writer
*/
#[DocumentationDSL(module: Module::XML, type: DSLType::LOADER)]
function to_xml(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static function openAppend(
]);

$blockList = new BlockList();
$blockList->add($partCopyResponse->getCopyPartResult()?->getEtag());
$blockList->add((string) $partCopyResponse->getCopyPartResult()?->getEtag());

return new self(
$s3Client,
Expand Down Expand Up @@ -154,6 +154,10 @@ public function close() : void

$handle = \fopen($this->blocks->block()->path()->path(), 'rb');

if ($handle === false) {
throw new InvalidArgumentException('Cannot open file: ' . $this->blocks->block()->path()->path());
}

$partResponse = $this->s3Client->uploadPart([
'Bucket' => $this->bucket,
'Key' => ltrim($this->path->path(), '/'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Flow\Filesystem\Bridge\AsyncAWS\{AsyncAWSS3Filesystem, Options};

/**
* @param array<string, mixed> $configuration - for details please see https://async-aws.com/clients/s3.html
* @param array<mixed> $configuration - for details please see https://async-aws.com/clients/s3.html
*/
#[DocumentationDSL(module: Module::S3_FILESYSTEM, type: Type::HELPER)]
function aws_s3_client(array $configuration) : S3Client
Expand Down
2 changes: 1 addition & 1 deletion src/cli/src/Flow/CLI/Options/TypedOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function asListOfStringsNullable(InputInterface $input) : ?array
}

/**
* @var string[] $options
* @var array<string> $options
*/
$options = [];

Expand Down
8 changes: 7 additions & 1 deletion src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ function filesystem_cache(Path|string|null $cache_dir = null, Filesystem $filesy
return new FilesystemCache($filesystem, $serializer, \is_string($cache_dir) ? Path::realpath($cache_dir) : $cache_dir);
}

/**
* @param int<1, max> $chunk_size
*/
#[DocumentationDSL(module: Module::CORE, type: DSLType::EXTRACTOR)]
function chunks_from(Extractor $extractor, int $chunk_size) : Extractor\ChunkExtractor
{
Expand Down Expand Up @@ -486,6 +489,9 @@ function type_structure(array $elements, bool $nullable = false) : StructureType
return new StructureType($elements, $nullable);
}

/**
* @param list<mixed> $value
*/
#[DocumentationDSL(module: Module::CORE, type: DSLType::ENTRY)]
function list_entry(string $name, ?array $value, ListType $type) : Entry\ListEntry
{
Expand All @@ -505,7 +511,7 @@ function type_list(Type $element, bool $nullable = false) : ListType
* @param Type<mixed> $value_type
*/
#[DocumentationDSL(module: Module::CORE, type: DSLType::TYPE)]
function type_map(IntegerType|StringType $key_type, Type $value_type, bool $nullable = false) : MapType
function type_map(StringType|IntegerType $key_type, Type $value_type, bool $nullable = false) : MapType
{
return new MapType($key_type, $value_type, $nullable);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/DataFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public function filterPartitions(Filter|ScalarFunction $filter) : self
/**
* @lazy
*
* @param ScalarFunction[] $functions
* @param array<ScalarFunction> $functions
*/
public function filters(array $functions) : self
{
Expand Down
3 changes: 3 additions & 0 deletions src/core/etl/src/Flow/ETL/Extractor/ChunkExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

final readonly class ChunkExtractor implements Extractor, OverridingExtractor
{
/**
* @param int<1, max> $chunkSize
*/
public function __construct(
private Extractor $extractor,
private int $chunkSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
final readonly class DatePeriodSequenceGenerator implements SequenceGenerator
{
/**
* @param \DatePeriod<\DateTimeImmutable, \DateTimeImmutable, null> $period
* @param \DatePeriod<\DateTimeInterface, \DateTimeInterface, null>|\DatePeriod<\DateTimeInterface, null, int> $period
*/
public function __construct(private \DatePeriod $period)
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Formatter/ASCII/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function maximumLength(string $entry, int|bool $truncate = 20) : int
}

/**
* @return Partition[]
* @return array<Partition>
*/
public function partitions() : array
{
Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/Greatest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function eval(Row $row) : mixed
$extractedValues[] = (new Parameter($value))->eval($row);
}

if (!\count($extractedValues)) {
return null;
}

$this->assertAllComparable($extractedValues, '>');

return max($extractedValues);
Expand Down
4 changes: 4 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/Least.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function eval(Row $row) : mixed
$extractedValues[] = (new Parameter($value))->eval($row);
}

if (!\count($extractedValues)) {
return null;
}

$this->assertAllComparable($extractedValues, '<');

return \min($extractedValues);
Expand Down
23 changes: 16 additions & 7 deletions src/core/etl/src/Flow/ETL/Function/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,14 @@ public function asInstanceOf(Row $row, string $class) : ?object
return \is_a($result, $class) ? $result : null;
}

public function asInt(Row $row) : ?int
/**
* @phpstan-return ($default is null ? int|null : int)
*/
public function asInt(Row $row, ?int $default = null) : ?int
{
$result = $this->function->eval($row);

return \is_int($result) ? $result : null;
return \is_int($result) ? $result : $default;
}

public function asListOfObjects(Row $row, string $class) : ?array
Expand All @@ -107,15 +110,18 @@ public function asListOfObjects(Row $row, string $class) : ?array
return $result;
}

public function asNumber(Row $row) : int|float|null
/**
* @phpstan-return ($default is null ? int|float|null : int|float)
*/
public function asNumber(Row $row, int|float|null $default = null) : int|float|null
{
$result = $this->function->eval($row);

if (\is_string($result)) {
return null;
return $default;
}

return \is_numeric($result) ? $result : null;
return \is_numeric($result) ? $result : $default;
}

public function asObject(Row $row) : ?object
Expand All @@ -125,11 +131,14 @@ public function asObject(Row $row) : ?object
return \is_object($result) ? $result : null;
}

public function asString(Row $row) : ?string
/**
* @phpstan-return ($default is null ? string|null : string)
*/
public function asString(Row $row, ?string $default = null) : ?string
{
$result = $this->function->eval($row);

return \is_string($result) ? $result : null;
return \is_string($result) ? $result : $default;
}

public function eval(Row $row) : mixed
Expand Down
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ public function strPadRight(int $length, string $pad_string = ' ') : self
}

/**
* @param ScalarFunction|string|string[] $search
* @param ScalarFunction|string|string[] $replace
* @param array<string>|ScalarFunction|string $search
* @param array<string>|ScalarFunction|string $replace
*/
public function strReplace(ScalarFunction|string|array $search, ScalarFunction|string|array $replace) : self
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/Function/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
public function eval(Row $row) : mixed
{
$string = (new Parameter($this->string))->asString($row);
$separator = (new Parameter($this->separator))->asString($row);
$separator = (new Parameter($this->separator))->asString($row, '-');
$locale = (new Parameter($this->locale))->asString($row);
$symbolsMap = (new Parameter($this->symbolsMap))->asArray($row);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum StringStyles : string
case UPPER = 'upper';

/**
* @return string[]
* @return array<string>
*/
public static function all() : array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
/**
* @return ?T
*/
public function value(mixed $value) : mixed
public function value(mixed $value)
{
if ($value === null && $this->type->nullable()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
final class MapCastingHandler implements CastingHandler
{
/**
* @param Type<array> $type
* @param Type<mixed> $type
*/
public function supports(Type $type) : bool
{
Expand Down Expand Up @@ -39,6 +39,10 @@ public function value(mixed $value, Type $type, Caster $caster, Options $options
foreach ($value as $key => $item) {
$castedKey = $caster->to($type->key())->value($key);

if ($castedKey === null) {
continue;
}

if (\array_key_exists($castedKey, $castedMap)) {
throw new CastingException($value, $type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function value(mixed $value, Type $type, Caster $caster, Options $options

$doc = new \DOMDocument();

if (!@$doc->loadXML($stringValue)) {
if (!@$doc->loadXML((string) $stringValue)) {
throw new CastingException($stringValue, type_xml());
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/etl/src/Flow/ETL/PHP/Type/Logical/ListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Flow\ETL\PHP\Type\{Type, TypeFactory};

/**
* @implements Type<array<int, mixed>>
* @implements Type<list<mixed>>
*/
final readonly class ListType implements Type
{
Expand Down
Loading

0 comments on commit ca840c8

Please sign in to comment.