Skip to content

Commit 03341cc

Browse files
committed
Nicer type descriptions usable in PHPDocs
1 parent f7d232a commit 03341cc

Some content is hidden

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

52 files changed

+333
-284
lines changed

build/baseline-8.0.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ parameters:
1111
path: ../src/Type/Php/MbFunctionsReturnTypeExtension.php
1212

1313
-
14-
message: "#^Strict comparison using \\=\\=\\= between array<int, string>&nonEmpty and false will always evaluate to false\\.$#"
14+
message: "#^Strict comparison using \\=\\=\\= between non-empty-array<int, string> and false will always evaluate to false\\.$#"
1515
count: 1
1616
path: ../src/Type/Php/StrSplitFunctionReturnTypeExtension.php
1717

phpstan-baseline.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ parameters:
259259
count: 1
260260
path: src/Testing/PHPStanTestCase.php
261261

262+
-
263+
message: "#^Call to function in_array\\(\\) with arguments 'array', array\\<int, 'array'\\|'string'\\> and true will always evaluate to true\\.$#"
264+
count: 1
265+
path: src/Type/IntersectionType.php
266+
262267
-
263268
message:
264269
"""

src/Type/Accessory/AccessoryNumericStringType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function equals(Type $type): bool
8484

8585
public function describe(\PHPStan\Type\VerbosityLevel $level): string
8686
{
87-
return 'numeric';
87+
return 'numeric-string';
8888
}
8989

9090
public function isOffsetAccessible(): TrinaryLogic

src/Type/Accessory/NonEmptyArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function equals(Type $type): bool
8383

8484
public function describe(\PHPStan\Type\VerbosityLevel $level): string
8585
{
86-
return 'nonEmpty';
86+
return 'non-empty-array';
8787
}
8888

8989
public function isOffsetAccessible(): TrinaryLogic

src/Type/IntersectionType.php

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -149,45 +149,75 @@ function () use ($level): string {
149149
return implode('&', $typeNames);
150150
},
151151
function () use ($level): string {
152-
$typeNames = [];
153-
$accessoryTypes = [];
154-
foreach ($this->types as $type) {
155-
if ($type instanceof AccessoryNonEmptyStringType || $type instanceof AccessoryLiteralStringType) {
156-
$accessoryTypes[] = $type;
157-
}
158-
if ($type instanceof AccessoryType && !$type instanceof AccessoryNumericStringType && !$type instanceof NonEmptyArrayType && !$type instanceof AccessoryNonEmptyStringType) {
159-
continue;
160-
}
161-
$typeNames[] = $type->describe($level);
162-
}
163-
164-
if (count($accessoryTypes) > 0) {
165-
return implode('&', array_map(static function (Type $type) use ($level): string {
166-
return $type->describe($level);
167-
}, $accessoryTypes));
168-
}
169-
170-
return implode('&', $typeNames);
152+
return $this->describeItself($level, true);
171153
},
172154
function () use ($level): string {
173-
$typeNames = [];
174-
$accessoryTypes = [];
175-
foreach ($this->types as $type) {
176-
if ($type instanceof AccessoryNonEmptyStringType || $type instanceof AccessoryLiteralStringType) {
177-
$accessoryTypes[] = $type;
155+
return $this->describeItself($level, false);
156+
}
157+
);
158+
}
159+
160+
private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes): string
161+
{
162+
$typesToDescribe = [];
163+
$skipTypeNames = [];
164+
foreach ($this->types as $type) {
165+
if ($type instanceof AccessoryNonEmptyStringType || $type instanceof AccessoryLiteralStringType || $type instanceof AccessoryNumericStringType) {
166+
$typesToDescribe[] = $type;
167+
$skipTypeNames[] = 'string';
168+
continue;
169+
}
170+
if ($type instanceof NonEmptyArrayType) {
171+
$typesToDescribe[] = $type;
172+
$skipTypeNames[] = 'array';
173+
continue;
174+
}
175+
176+
if ($skipAccessoryTypes) {
177+
continue;
178+
}
179+
180+
if (!$type instanceof AccessoryType) {
181+
continue;
182+
}
183+
184+
$typesToDescribe[] = $type;
185+
}
186+
187+
$describedTypes = [];
188+
foreach ($this->types as $type) {
189+
if ($type instanceof AccessoryType) {
190+
continue;
191+
}
192+
$typeDescription = $type->describe($level);
193+
if (
194+
substr($typeDescription, 0, strlen('array<')) === 'array<'
195+
&& in_array('array', $skipTypeNames, true)
196+
) {
197+
foreach ($typesToDescribe as $j => $typeToDescribe) {
198+
if (!$typeToDescribe instanceof NonEmptyArrayType) {
199+
continue;
178200
}
179-
$typeNames[] = $type->describe($level);
180-
}
181201

182-
if (count($accessoryTypes) > 0) {
183-
return implode('&', array_map(static function (Type $type) use ($level): string {
184-
return $type->describe($level);
185-
}, $accessoryTypes));
202+
unset($typesToDescribe[$j]);
186203
}
187204

188-
return implode('&', $typeNames);
205+
$describedTypes[] = 'non-empty-array<' . substr($typeDescription, strlen('array<'));
206+
continue;
189207
}
190-
);
208+
209+
if (in_array($typeDescription, $skipTypeNames, true)) {
210+
continue;
211+
}
212+
213+
$describedTypes[] = $type->describe($level);
214+
}
215+
216+
foreach ($typesToDescribe as $typeToDescribe) {
217+
$describedTypes[] = $typeToDescribe->describe($level);
218+
}
219+
220+
return implode('&', $describedTypes);
191221
}
192222

193223
public function canAccessProperties(): TrinaryLogic

src/Type/VerbosityLevel.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc
6969
$moreVerbose = true;
7070
return $type;
7171
}
72-
if ($type instanceof AccessoryNumericStringType || $type instanceof AccessoryNonEmptyStringType || $type instanceof AccessoryLiteralStringType) {
73-
$moreVerbose = true;
74-
return $type;
75-
}
76-
if ($type instanceof NonEmptyArrayType) {
72+
if (
73+
// synced with IntersectionType::describe()
74+
$type instanceof AccessoryNonEmptyStringType
75+
|| $type instanceof AccessoryLiteralStringType
76+
|| $type instanceof AccessoryNumericStringType
77+
|| $type instanceof NonEmptyArrayType
78+
) {
7779
$moreVerbose = true;
7880
return $type;
7981
}

0 commit comments

Comments
 (0)