Skip to content

Commit 528bbf3

Browse files
committed
PHPstan level 6
1 parent f315a2a commit 528bbf3

13 files changed

+127
-157
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"require-dev": {
3131
"ext-curl": "*",
3232
"ext-json": "*",
33+
"ext-mongodb": "*",
3334
"ext-pdo_sqlite": "*",
3435
"ext-zip": "*",
3536
"doctrine/common": "^2.6|^3.3",

phpstan.neon

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
parameters:
2-
level: 5
2+
level: 6
33
paths:
4-
- src
4+
- src
55
excludePaths:
6-
- tests/Fixtures
6+
# We're not really testing these 2 barely supported adapters
7+
- src/Adapter/Elasticsearch
8+
- src/Adapter/MongoDB
79

8-
checkGenericClassInNonGenericObjectType: false
10+
# Fixture is messy
11+
- tests/Fixtures
12+
13+
checkGenericClassInNonGenericObjectType: false

src/Adapter/AbstractAdapter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,8 @@ abstract protected function prepareQuery(AdapterQuery $query): void;
7878

7979
abstract protected function mapPropertyPath(AdapterQuery $query, AbstractColumn $column): ?string;
8080

81+
/**
82+
* @return \Traversable<mixed[]>
83+
*/
8184
abstract protected function getResults(AdapterQuery $query): \Traversable;
8285
}

src/Adapter/AdapterQuery.php

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,100 +22,69 @@
2222
*/
2323
class AdapterQuery
2424
{
25-
/** @var DataTableState */
26-
private $state;
27-
28-
/** @var int|null */
29-
private $totalRows;
30-
31-
/** @var int|null */
32-
private $filteredRows;
33-
34-
/** @var string|null */
35-
private $identifierPropertyPath;
25+
private ?int $totalRows;
26+
private ?int $filteredRows;
27+
private ?string $identifierPropertyPath = null;
3628

3729
/** @var array<string, mixed> */
38-
private $data;
30+
private array $data;
3931

40-
/**
41-
* AdapterQuery constructor.
42-
*/
43-
public function __construct(DataTableState $state)
32+
public function __construct(private readonly DataTableState $state)
4433
{
45-
$this->state = $state;
4634
}
4735

4836
public function getState(): DataTableState
4937
{
5038
return $this->state;
5139
}
5240

53-
/**
54-
* @return int|null
55-
*/
56-
public function getTotalRows()
41+
public function getTotalRows(): ?int
5742
{
5843
return $this->totalRows;
5944
}
6045

61-
/**
62-
* @param int|null $totalRows
63-
* @return $this
64-
*/
65-
public function setTotalRows($totalRows): static
46+
public function setTotalRows(?int $totalRows): static
6647
{
6748
$this->totalRows = $totalRows;
6849

6950
return $this;
7051
}
7152

72-
/**
73-
* @return int|null
74-
*/
75-
public function getFilteredRows()
53+
public function getFilteredRows(): ?int
7654
{
7755
return $this->filteredRows;
7856
}
7957

80-
/**
81-
* @param int|null $filteredRows
82-
* @return $this
83-
*/
84-
public function setFilteredRows($filteredRows): static
58+
public function setFilteredRows(?int $filteredRows): static
8559
{
8660
$this->filteredRows = $filteredRows;
8761

8862
return $this;
8963
}
9064

91-
/**
92-
* @return string|null
93-
*/
94-
public function getIdentifierPropertyPath()
65+
public function getIdentifierPropertyPath(): ?string
9566
{
9667
return $this->identifierPropertyPath;
9768
}
9869

99-
/**
100-
* @param string|null $identifierPropertyPath
101-
* @return $this
102-
*/
103-
public function setIdentifierPropertyPath($identifierPropertyPath): static
70+
public function setIdentifierPropertyPath(?string $identifierPropertyPath): static
10471
{
10572
$this->identifierPropertyPath = $identifierPropertyPath;
10673

10774
return $this;
10875
}
10976

11077
/**
111-
* @return mixed|null
78+
* @template T
79+
* @param T $default
80+
* @return T|mixed
11281
*/
113-
public function get(string $key, $default = null)
82+
public function get(string $key, mixed $default = null): mixed
11483
{
11584
return $this->data[$key] ?? $default;
11685
}
11786

118-
public function set(string $key, $value)
87+
public function set(string $key, mixed $value): void
11988
{
12089
$this->data[$key] = $value;
12190
}

src/Adapter/ArrayAdapter.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ public function getData(DataTableState $state): ResultSetInterface
7171
}
7272

7373
/**
74-
* @return \Generator
74+
* @param mixed[][] $data
75+
* @param array<string, string> $map
76+
* @return \Generator<mixed[]>
7577
*/
76-
protected function processData(DataTableState $state, array $data, array $map)
78+
protected function processData(DataTableState $state, array $data, array $map): \Generator
7779
{
7880
$transformer = $state->getDataTable()->getTransformer();
7981
$search = $state->getGlobalSearch() ?: '';
@@ -88,9 +90,11 @@ protected function processData(DataTableState $state, array $data, array $map)
8890
}
8991

9092
/**
91-
* @return array|null
93+
* @param mixed[] $result
94+
* @param array<string, string> $map
95+
* @return mixed[]|null
9296
*/
93-
protected function processRow(DataTableState $state, array $result, array $map, string $search)
97+
protected function processRow(DataTableState $state, array $result, array $map, string $search): ?array
9498
{
9599
$row = [];
96100
$match = empty($search);

src/Adapter/ArrayResultSet.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@
1616
* ArrayResultSet.
1717
*
1818
* @author Niels Keurentjes <[email protected]>
19+
*
20+
* @phpstan-type Row array<string, mixed>
1921
*/
2022
class ArrayResultSet implements ResultSetInterface
2123
{
22-
/** @var array */
23-
private $data;
24-
25-
/** @var int */
26-
private $totalRows;
27-
28-
/** @var int */
29-
private $totalFilteredRows;
24+
/** @var Row[] */
25+
private array $data;
26+
private int $totalRows;
27+
private int $totalFilteredRows;
3028

3129
/**
32-
* ArrayResultSet constructor.
30+
* @param Row[] $data
3331
*/
3432
public function __construct(array $data, int $totalRows = null, int $totalFilteredRows = null)
3533
{

src/Adapter/Doctrine/FetchJoinORMAdapter.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
* the Doctrine Paginator.
2727
*
2828
* @author Jan Böhmer
29+
*
30+
* @phpstan-import-type ORMOptions from ORMAdapter
31+
* @phpstan-type FetchJoinORMOptions array{simple_total_query: bool}
2932
*/
3033
class FetchJoinORMAdapter extends ORMAdapter
3134
{
32-
protected $use_simple_total;
35+
protected bool $useSimpleTotal;
3336

3437
protected function configureOptions(OptionsResolver $resolver): void
3538
{
@@ -45,11 +48,14 @@ protected function configureOptions(OptionsResolver $resolver): void
4548
$resolver->setDefault('simple_total_query', false);
4649
}
4750

51+
/**
52+
* @param FetchJoinORMOptions|ORMOptions $options
53+
*/
4854
protected function afterConfiguration(array $options): void
4955
{
5056
parent::afterConfiguration($options);
5157

52-
$this->use_simple_total = $options['simple_total_query'];
58+
$this->useSimpleTotal = $options['simple_total_query'];
5359
}
5460

5561
protected function prepareQuery(AdapterQuery $query): void
@@ -70,7 +76,7 @@ protected function prepareQuery(AdapterQuery $query): void
7076
$identifier = "{$fromClause->getAlias()}.{$this->metadata->getSingleIdentifierFieldName()}";
7177

7278
// Use simpler (faster) total count query if the user wanted so...
73-
if ($this->use_simple_total) {
79+
if ($this->useSimpleTotal) {
7480
$query->setTotalRows($this->getSimpleTotalCount($builder));
7581
} else {
7682
$query->setTotalRows($this->getCount($builder, $identifier));
@@ -117,21 +123,23 @@ public function getResults(AdapterQuery $query): \Traversable
117123
}
118124
}
119125

120-
public function getCount(QueryBuilder $queryBuilder, $identifier): int
126+
public function getCount(QueryBuilder $queryBuilder, mixed $identifier): int
121127
{
122128
$paginator = new Paginator($queryBuilder);
123129

124130
return $paginator->count();
125131
}
126132

127-
protected function getSimpleTotalCount(QueryBuilder $queryBuilder)
133+
/**
134+
* The paginator count queries can be rather slow, so when query for total count (100ms or longer),
135+
* just return the entity count.
136+
*/
137+
protected function getSimpleTotalCount(QueryBuilder $queryBuilder): int
128138
{
129-
/** The paginator count queries can be rather slow, so when query for total count (100ms or longer),
130-
* just return the entity count.
131-
*/
132139
/** @var Query\Expr\From $from_expr */
133140
$from_expr = $queryBuilder->getDQLPart('from')[0];
134141

142+
/* @phpstan-ignore-next-line Unable to determine repository type */
135143
return $this->manager->getRepository($from_expr->getFrom())->count([]);
136144
}
137145
}

src/Adapter/Doctrine/ORM/AutomaticQueryBuilder.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,19 @@
2525
*/
2626
class AutomaticQueryBuilder implements QueryBuilderProcessorInterface
2727
{
28-
/** @var EntityManagerInterface */
29-
private $em;
28+
private EntityManagerInterface $em;
29+
private ClassMetadata $metadata;
30+
private string $entityShortName;
3031

31-
/** @var ClassMetadata */
32-
private $metadata;
32+
/** @var class-string */
33+
private string $entityName;
3334

34-
/** @var string */
35-
private $entityName;
35+
/** @var array<string, string[]> */
36+
private array $selectColumns = [];
3637

37-
/** @var string */
38-
private $entityShortName;
38+
/** @var array<string, string[]> */
39+
private array $joins = [];
3940

40-
/** @var array */
41-
private $selectColumns = [];
42-
43-
/** @var array */
44-
private $joins = [];
45-
46-
/**
47-
* AutomaticQueryBuilder constructor.
48-
*/
4941
public function __construct(EntityManagerInterface $em, ClassMetadata $metadata)
5042
{
5143
$this->em = $em;
@@ -55,7 +47,7 @@ public function __construct(EntityManagerInterface $em, ClassMetadata $metadata)
5547
$this->entityShortName = mb_strtolower($this->metadata->getReflectionClass()->getShortName());
5648
}
5749

58-
public function process(QueryBuilder $builder, DataTableState $state)
50+
public function process(QueryBuilder $builder, DataTableState $state): void
5951
{
6052
if (empty($this->selectColumns) && empty($this->joins)) {
6153
foreach ($state->getDataTable()->getColumns() as $column) {
@@ -67,7 +59,7 @@ public function process(QueryBuilder $builder, DataTableState $state)
6759
$this->setJoins($builder);
6860
}
6961

70-
protected function processColumn(AbstractColumn $column)
62+
protected function processColumn(AbstractColumn $column): void
7163
{
7264
$field = $column->getField();
7365

@@ -80,7 +72,7 @@ protected function processColumn(AbstractColumn $column)
8072
}
8173
}
8274

83-
private function addSelectColumns(AbstractColumn $column, string $field)
75+
private function addSelectColumns(AbstractColumn $column, string $field): void
8476
{
8577
$currentPart = $this->entityShortName;
8678
$currentAlias = $currentPart;
@@ -111,7 +103,7 @@ private function addSelectColumns(AbstractColumn $column, string $field)
111103
}
112104
}
113105

114-
private function addSelectColumn($columnTableName, $data)
106+
private function addSelectColumn(string $columnTableName, string $data): void
115107
{
116108
if (isset($this->selectColumns[$columnTableName])) {
117109
if (!in_array($data, $this->selectColumns[$columnTableName], true)) {
@@ -120,18 +112,16 @@ private function addSelectColumn($columnTableName, $data)
120112
} else {
121113
$this->selectColumns[$columnTableName][] = $data;
122114
}
123-
124-
return $this;
125115
}
126116

127-
private function getIdentifier(ClassMetadata $metadata)
117+
private function getIdentifier(ClassMetadata $metadata): string
128118
{
129119
$identifiers = $metadata->getIdentifierFieldNames();
130120

131121
return array_shift($identifiers);
132122
}
133123

134-
private function setIdentifierFromAssociation(string $association, string $key, ClassMetadata $metadata)
124+
private function setIdentifierFromAssociation(string $association, string $key, ClassMetadata $metadata): ClassMetadata
135125
{
136126
$targetEntityClass = $metadata->getAssociationTargetClass($key);
137127

@@ -142,7 +132,7 @@ private function setIdentifierFromAssociation(string $association, string $key,
142132
return $targetMetadata;
143133
}
144134

145-
private function setSelectFrom(QueryBuilder $qb)
135+
private function setSelectFrom(QueryBuilder $qb): void
146136
{
147137
foreach ($this->selectColumns as $key => $value) {
148138
if (false === empty($key)) {
@@ -151,16 +141,12 @@ private function setSelectFrom(QueryBuilder $qb)
151141
$qb->addSelect($value);
152142
}
153143
}
154-
155-
return $this;
156144
}
157145

158-
private function setJoins(QueryBuilder $qb)
146+
private function setJoins(QueryBuilder $qb): void
159147
{
160148
foreach ($this->joins as $key => $value) {
161149
$qb->{$value['type']}($key, $value['alias']);
162150
}
163-
164-
return $this;
165151
}
166152
}

0 commit comments

Comments
 (0)