Skip to content

Commit 0d89a3f

Browse files
authored
Improved codebase quality through static analysis (#1723)
* Improved codebase quality through static analysis * Increased types precision by narrowing types through types library * Cleanup docblocks
1 parent 4ba4df3 commit 0d89a3f

369 files changed

Lines changed: 4272 additions & 1165 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

documentation/components/libs/types.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ composer require flow-php/types:~--FLOW_PHP_VERSION--
1818

1919
### Usage
2020

21+
**Heads Up** - in order to use full potential of `type_structure()` with PHPStan you need
22+
to enable a dedicated extension in your `phpstan.neon` configuration file:
23+
24+
```neon
25+
services:
26+
-
27+
class: Flow\Types\PHPStan\StructureTypeReturnTypeExtension
28+
tags:
29+
- phpstan.broker.dynamicFunctionReturnTypeExtension
30+
```
31+
2132
#### Type Narrowing
2233

2334
To narrow a variable to a specific type, you can use one of two available methods:

phpstan.neon

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
parameters:
2-
level: 8
2+
level: 9
33
treatPhpDocTypesAsCertain: false
4-
ignoreErrors:
5-
- identifier: argument.type
6-
paths:
7-
- src/core/etl/tests/*
8-
- src/bridge/filesystem/azure/tests/*
9-
- src/bridge/filesystem/async-aws/tests/*
10-
- src/adapter/etl-adapter-xml/tests/*
11-
- src/adapter/etl-adapter-text/tests/*
12-
- src/adapter/etl-adapter-meilisearch/tests/*
13-
- src/adapter/etl-adapter-logger/tests/*
14-
- src/adapter/etl-adapter-json/tests/*
15-
- src/adapter/etl-adapter-http/tests/*
16-
- src/adapter/etl-adapter-csv/tests/*
17-
- identifier: missingType.iterableValue
184
bootstrapFiles:
195
- tools/phpunit/vendor/autoload.php
206
- tools/phpbench/vendor/autoload.php
@@ -98,3 +84,9 @@ parameters:
9884

9985
includes:
10086
- tools/phpstan/vendor/spaze/phpstan-disallowed-calls/extension.neon
87+
88+
services:
89+
-
90+
class: Flow\Types\PHPStan\StructureTypeReturnTypeExtension
91+
tags:
92+
- phpstan.broker.dynamicFunctionReturnTypeExtension

src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@ interface Chart
1111
{
1212
public function collect(Rows $rows) : void;
1313

14+
/**
15+
* @return array<array-key, mixed>
16+
*/
1417
public function data() : array;
1518

19+
/**
20+
* @param array<array-key, mixed> $options
21+
*/
1622
public function setDatasetOptions(EntryReference $dataset, array $options) : self;
1723

24+
/**
25+
* @param array<array-key, mixed> $options
26+
*/
1827
public function setOptions(array $options) : self;
1928
}

src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/BarChart.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ final class BarChart implements Chart
1313
/**
1414
* @var array{
1515
* labels: array<string>,
16-
* datasets: array<string, array{label: string, data: array}>
16+
* datasets: array<string, array{label: string, data: array<mixed>}>
1717
* }
1818
*/
1919
private array $data = [
2020
'labels' => [],
2121
'datasets' => [],
2222
];
2323

24+
/**
25+
* @var array<string, array<array-key, mixed>>
26+
*/
2427
private array $datasetOptions = [];
2528

29+
/**
30+
* @var array<array-key, mixed>
31+
*/
2632
private array $options = [];
2733

2834
public function __construct(
@@ -34,7 +40,8 @@ public function __construct(
3440
public function collect(Rows $rows) : void
3541
{
3642
foreach ($rows as $row) {
37-
$this->data['labels'][] = (string) $row->valueOf($this->label);
43+
$labelValue = $row->valueOf($this->label);
44+
$this->data['labels'][] = \is_scalar($labelValue) ? (string) $labelValue : '';
3845

3946
foreach ($this->datasets as $dataset) {
4047
if (!\array_key_exists($dataset->name(), $this->data['datasets'])) {
@@ -49,6 +56,9 @@ public function collect(Rows $rows) : void
4956
}
5057
}
5158

59+
/**
60+
* @return array<array-key, mixed>
61+
*/
5262
public function data() : array
5363
{
5464
$data = [
@@ -74,13 +84,19 @@ function (array $dataset) : array {
7484
return $data;
7585
}
7686

87+
/**
88+
* @param array<array-key, mixed> $options
89+
*/
7790
public function setDatasetOptions(Reference $dataset, array $options) : self
7891
{
7992
$this->datasetOptions[$dataset->name()] = $options;
8093

8194
return $this;
8295
}
8396

97+
/**
98+
* @param array<array-key, mixed> $options
99+
*/
84100
public function setOptions(array $options) : self
85101
{
86102
$this->options = $options;

src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/LineChart.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ final class LineChart implements Chart
1313
/**
1414
* @var array{
1515
* labels: array<string>,
16-
* datasets: array<string, array{label: string, data: array}>
16+
* datasets: array<string, array{label: string, data: array<mixed>}>
1717
* }
1818
*/
1919
private array $data = [
2020
'labels' => [],
2121
'datasets' => [],
2222
];
2323

24+
/**
25+
* @var array<string, array<array-key, mixed>>
26+
*/
2427
private array $datasetOptions = [];
2528

29+
/**
30+
* @var array<array-key, mixed>
31+
*/
2632
private array $options = [];
2733

2834
public function __construct(
@@ -34,7 +40,8 @@ public function __construct(
3440
public function collect(Rows $rows) : void
3541
{
3642
foreach ($rows as $row) {
37-
$this->data['labels'][] = (string) $row->valueOf($this->label);
43+
$labelValue = $row->valueOf($this->label);
44+
$this->data['labels'][] = \is_scalar($labelValue) ? (string) $labelValue : '';
3845

3946
foreach ($this->datasets as $dataset) {
4047
if (!\array_key_exists($dataset->name(), $this->data['datasets'])) {
@@ -49,6 +56,9 @@ public function collect(Rows $rows) : void
4956
}
5057
}
5158

59+
/**
60+
* @return array<array-key, mixed>
61+
*/
5262
public function data() : array
5363
{
5464
$data = [
@@ -74,13 +84,19 @@ function (array $dataset) : array {
7484
return $data;
7585
}
7686

87+
/**
88+
* @param array<array-key, mixed> $options
89+
*/
7790
public function setDatasetOptions(Reference $dataset, array $options) : self
7891
{
7992
$this->datasetOptions[$dataset->name()] = $options;
8093

8194
return $this;
8295
}
8396

97+
/**
98+
* @param array<array-key, mixed> $options
99+
*/
84100
public function setOptions(array $options) : self
85101
{
86102
$this->options = $options;

src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/Chart/PieChart.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class PieChart implements Chart
1212
{
1313
/**
1414
* @var array{
15-
* datasets: array<string, array{data: array, label: ?string}>
15+
* datasets: array<string, array{data: array<mixed>, label: ?string}>
1616
* }
1717
*/
1818
private array $data = [
@@ -24,6 +24,9 @@ final class PieChart implements Chart
2424
*/
2525
private array $datasetOptions = [];
2626

27+
/**
28+
* @var array<array-key, mixed>
29+
*/
2730
private array $options = [];
2831

2932
public function __construct(
@@ -39,16 +42,20 @@ public function collect(Rows $rows) : void
3942
if (!\array_key_exists('pie', $this->data['datasets'])) {
4043
$this->data['datasets']['pie'] = [
4144
'data' => [$row->valueOf($dataset)],
42-
'label' => (string) $row->valueOf($this->label),
45+
'label' => \is_scalar($row->valueOf($this->label)) ? (string) $row->valueOf($this->label) : '',
4346
];
4447
} else {
4548
$this->data['datasets']['pie']['data'][] = $row->valueOf($dataset);
46-
$this->data['datasets']['pie']['label'] = (string) $row->valueOf($this->label);
49+
$labelValue = $row->valueOf($this->label);
50+
$this->data['datasets']['pie']['label'] = \is_scalar($labelValue) ? (string) $labelValue : '';
4751
}
4852
}
4953
}
5054
}
5155

56+
/**
57+
* @return array<array-key, mixed>
58+
*/
5259
public function data() : array
5360
{
5461
$labels = [];
@@ -78,13 +85,19 @@ public function data() : array
7885
return $data;
7986
}
8087

88+
/**
89+
* @param array<array-key, mixed> $options
90+
*/
8191
public function setDatasetOptions(Reference $dataset, array $options) : self
8292
{
8393
$this->datasetOptions[$dataset->name()] = $options;
8494

8595
return $this;
8696
}
8797

98+
/**
99+
* @param array<array-key, mixed> $options
100+
*/
88101
public function setOptions(array $options) : self
89102
{
90103
$this->options = $options;

src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/ChartJSLoader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ final class ChartJSLoader implements Closure, Loader
1212
{
1313
private ?Path $output = null;
1414

15+
/**
16+
* @var null|array<array-key, mixed>
17+
*/
1518
private ?array $outputVar = null;
1619

1720
private Path $template;
@@ -71,6 +74,9 @@ public function withOutputPath(Path $output) : self
7174
return $this;
7275
}
7376

77+
/**
78+
* @param array<array-key, mixed> $outputVar
79+
*/
7480
public function withOutputVar(array &$outputVar) : self
7581
{
7682
$this->outputVar = &$outputVar;

src/adapter/etl-adapter-chartjs/src/Flow/ETL/Adapter/ChartJS/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function to_chartjs_file(Chart $type, Path|string|null $output = null, Path|stri
6464

6565
/**
6666
* @param Chart $type
67-
* @param array $output - @deprecated use $loader->withOutputVar() instead
67+
* @param array<array-key, mixed> $output - @deprecated use $loader->withOutputVar() instead
6868
*/
6969
#[DocumentationDSL(module: Module::CHART_JS, type: Type::LOADER)]
7070
function to_chartjs_var(Chart $type, array &$output) : ChartJSLoader

src/adapter/etl-adapter-csv/src/Flow/ETL/Adapter/CSV/CSVExtractor.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ public function withHeader(bool $withHeader) : self
168168
return $this;
169169
}
170170

171+
/**
172+
* @param Schema $schema
173+
*/
171174
public function withSchema(Schema $schema) : self
172175
{
173176
$this->schema = $schema;
@@ -182,9 +185,24 @@ public function withSeparator(string $separator) : self
182185
return $this;
183186
}
184187

188+
/**
189+
* @param array<array-key, mixed> $headers
190+
*
191+
* @return array<int, string>
192+
*/
185193
private function mapHeaders(array $headers) : array
186194
{
187-
$headers = \array_map(fn (string $header) : string => \trim($header), $headers);
195+
$headers = \array_map(
196+
static fn (mixed $header) : string => \trim(
197+
match (true) {
198+
\is_string($header) => $header,
199+
\is_numeric($header) => (string) $header,
200+
$header === null => '',
201+
default => \is_scalar($header) ? (string) $header : '',
202+
}
203+
),
204+
$headers
205+
);
188206

189207
return \array_map(
190208
fn (string $header, int $index) : string => $header !== '' ? $header : 'e' . \str_pad(

src/adapter/etl-adapter-csv/src/Flow/ETL/Adapter/CSV/CSVLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public function withSeparator(string $separator) : self
9999
}
100100

101101
/**
102+
* @param array<string> $headers
102103
* @param array<Partition> $partitions
103104
*/
104105
public function write(Rows $nextRows, array $headers, FlowContext $context, array $partitions, RowsNormalizer $normalizer) : void
@@ -115,6 +116,9 @@ public function write(Rows $nextRows, array $headers, FlowContext $context, arra
115116
}
116117
}
117118

119+
/**
120+
* @param array<array-key, null|bool|float|int|string> $row
121+
*/
118122
private function writeCSV(array $row, DestinationStream $stream) : void
119123
{
120124
$tmpHandle = fopen('php://temp/maxmemory:' . (5 * 1024 * 1024), 'rb+');

0 commit comments

Comments
 (0)