Skip to content

Commit 3128f26

Browse files
Improve lowercase string verbosity level
1 parent 107a7e3 commit 3128f26

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/Type/VerbosityLevel.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc
131131
}
132132

133133
if ($moreVerbose) {
134-
return self::value();
134+
$verbosity = self::value();
135135
}
136136

137137
if ($acceptedType === null) {
138-
return self::typeOnly();
138+
return $verbosity ?? self::typeOnly();
139139
}
140140

141141
$containsInvariantTemplateType = false;
@@ -163,7 +163,7 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc
163163
});
164164

165165
if (!$containsInvariantTemplateType) {
166-
return self::typeOnly();
166+
return $verbosity ?? self::typeOnly();
167167
}
168168

169169
/** @var bool $moreVerbose */
@@ -176,7 +176,7 @@ public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acc
176176
return self::precise();
177177
}
178178

179-
return $moreVerbose ? self::value() : self::typeOnly();
179+
return $moreVerbose ? self::value() : $verbosity ?? self::typeOnly();
180180
}
181181

182182
/**
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type;
4+
5+
use PHPStan\Testing\PHPStanTestCase;
6+
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
7+
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
8+
use PHPStan\Type\Generic\GenericObjectType;
9+
use PHPStan\Type\Generic\TemplateTypeVariance;
10+
11+
class VerbosityLevelTest extends PHPStanTestCase
12+
{
13+
14+
public function dataGetRecommendedLevelByType(): iterable
15+
{
16+
yield [
17+
new BooleanType(),
18+
new IntersectionType([new StringType(), new AccessoryLowercaseStringType()]),
19+
VerbosityLevel::typeOnly(),
20+
];
21+
yield [
22+
new IntersectionType([new StringType(), new AccessoryNonFalsyStringType()]),
23+
new IntersectionType([new StringType(), new AccessoryLowercaseStringType()]),
24+
VerbosityLevel::value(),
25+
];
26+
yield [
27+
new GenericObjectType(
28+
'ArrayAccess',
29+
[
30+
new IntegerType(),
31+
new IntersectionType([new StringType(), new AccessoryNonFalsyStringType()]),
32+
],
33+
null,
34+
null,
35+
[TemplateTypeVariance::createInvariant(), TemplateTypeVariance::createInvariant()],
36+
),
37+
new GenericObjectType(
38+
'ArrayAccess',
39+
[
40+
new IntegerType(),
41+
new IntersectionType([new StringType(), new AccessoryLowercaseStringType()]),
42+
],
43+
null,
44+
null,
45+
[TemplateTypeVariance::createInvariant(), TemplateTypeVariance::createInvariant()],
46+
),
47+
VerbosityLevel::precise(),
48+
];
49+
}
50+
51+
/**
52+
* @dataProvider dataGetRecommendedLevelByType
53+
*/
54+
public function testGetRecommendedLevelByType(Type $acceptingType, ?Type $acceptedType, VerbosityLevel $expected): void
55+
{
56+
$level = VerbosityLevel::getRecommendedLevelByType($acceptingType, $acceptedType);
57+
58+
$this->assertSame($expected->getLevelValue(), $level->getLevelValue());
59+
}
60+
61+
}

0 commit comments

Comments
 (0)