Skip to content

Commit 8e8eca7

Browse files
Fix lowercase string usage in template
1 parent d066898 commit 8e8eca7

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
lines changed

src/Type/IntersectionType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
368368
if (
369369
($type instanceof AccessoryLowercaseStringType || $type instanceof AccessoryUppercaseStringType)
370370
&& !$level->isPrecise()
371+
&& !$level->isCache()
371372
) {
372373
continue;
373374
}

src/Type/UnionType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function describe(VerbosityLevel $level): string
337337
}
338338
}
339339

340-
if ($level->isPrecise()) {
340+
if ($level->isPrecise() || $level->isCache()) {
341341
$duplicates = array_diff_assoc($typeNames, array_unique($typeNames));
342342
if (count($duplicates) > 0) {
343343
$indexByDuplicate = array_fill_keys($duplicates, 0);

src/Type/VerbosityLevel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public function isPrecise(): bool
8686
return $this->value === self::PRECISE;
8787
}
8888

89+
public function isCache(): bool
90+
{
91+
return $this->value === self::CACHE;
92+
}
93+
8994
/** @api */
9095
public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acceptedType = null): self
9196
{

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,11 @@ public function testBug13043(): void
12441244
$this->analyse([__DIR__ . '/data/bug-13043.php'], []);
12451245
}
12461246

1247+
public function testBug12739(): void
1248+
{
1249+
$this->analyse([__DIR__ . '/data/bug-12739.php'], []);
1250+
}
1251+
12471252
public function testBug12928(): void
12481253
{
12491254
$this->analyse([__DIR__ . '/data/bug-12928.php'], [
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
/**
6+
* @template TScalar of scalar
7+
*/
8+
abstract class ScalarType {
9+
/**
10+
* @return class-string<ScalarType<TScalar>>
11+
*/
12+
abstract public static function getClassIdentify() : string;
13+
}
14+
15+
/**
16+
* @extends ScalarType<int>
17+
*/
18+
class MyInt extends ScalarType {
19+
public static function getClassIdentify() : string {
20+
return MyInt::class;
21+
}
22+
}
23+
24+
/**
25+
* @extends ScalarType<string>
26+
*/
27+
class MyString extends ScalarType {
28+
public static function getClassIdentify() : string {
29+
return MyString::class;
30+
}
31+
}
32+
33+
/**
34+
* @extends ScalarType<lowercase-string>
35+
*/
36+
class MyLowerString extends ScalarType {
37+
public static function getClassIdentify() : string {
38+
return MyLowerString::class;
39+
}
40+
}
41+
42+
/**
43+
* @extends ScalarType<uppercase-string>
44+
*/
45+
class MyUpperString extends ScalarType {
46+
public static function getClassIdentify() : string {
47+
return MyUpperString::class;
48+
}
49+
}
50+
51+
/**
52+
* @extends ScalarType<class-string>
53+
*/
54+
class MyClassString extends ScalarType {
55+
public static function getClassIdentify() : string {
56+
return MyClassString::class;
57+
}
58+
}

tests/PHPStan/Type/UnionTypeTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,12 +708,14 @@ public static function dataDescribe(): array
708708
'int|string',
709709
'int|string',
710710
'int|string',
711+
'int|string',
711712
],
712713
[
713714
new UnionType([new IntegerType(), new StringType(), new NullType()]),
714715
'int|string|null',
715716
'int|string|null',
716717
'int|string|null',
718+
'int|string|null',
717719
],
718720
[
719721
new UnionType([
@@ -733,6 +735,7 @@ public static function dataDescribe(): array
733735
new ConstantStringType('2'),
734736
new ConstantStringType('1'),
735737
]),
738+
"1|2|2.2|10|'1'|'10'|'10aaa'|'11aaa'|'1aaa'|'2'|'2aaa'|'foo'|stdClass-PHPStan\Type\ObjectType-|true|null",
736739
"1|2|2.2|10|'1'|'10'|'10aaa'|'11aaa'|'1aaa'|'2'|'2aaa'|'foo'|stdClass|true|null",
737740
"1|2|2.2|10|'1'|'10'|'10aaa'|'11aaa'|'1aaa'|'2'|'2aaa'|'foo'|stdClass|true|null",
738741
'float|int|stdClass|string|true|null',
@@ -757,6 +760,7 @@ public static function dataDescribe(): array
757760
),
758761
'\'aaa\'|array{a: int, b: float}|array{a: string, b: bool}',
759762
'\'aaa\'|array{a: int, b: float}|array{a: string, b: bool}',
763+
'\'aaa\'|array{a: int, b: float}|array{a: string, b: bool}',
760764
'array<string, bool|float|int|string>|string',
761765
],
762766
[
@@ -779,6 +783,7 @@ public static function dataDescribe(): array
779783
),
780784
'\'aaa\'|array{a: string, b: bool}|array{b: int, c: float}',
781785
'\'aaa\'|array{a: string, b: bool}|array{b: int, c: float}',
786+
'\'aaa\'|array{a: string, b: bool}|array{b: int, c: float}',
782787
'array<string, bool|float|int|string>|string',
783788
],
784789
[
@@ -801,6 +806,7 @@ public static function dataDescribe(): array
801806
),
802807
'\'aaa\'|array{a: string, b: bool}|array{c: int, d: float}',
803808
'\'aaa\'|array{a: string, b: bool}|array{c: int, d: float}',
809+
'\'aaa\'|array{a: string, b: bool}|array{c: int, d: float}',
804810
'array<string, bool|float|int|string>|string',
805811
],
806812
[
@@ -822,6 +828,7 @@ public static function dataDescribe(): array
822828
),
823829
'array{int, bool, float}|array{string}',
824830
'array{int, bool, float}|array{string}',
831+
'array{int, bool, float}|array{string}',
825832
'array<int, bool|float|int|string>',
826833
],
827834
[
@@ -835,6 +842,7 @@ public static function dataDescribe(): array
835842
),
836843
'array{}|array{foooo: \'barrr\'}',
837844
'array{}|array{foooo: \'barrr\'}',
845+
'array{}|array{foooo: \'barrr\'}',
838846
'array<string, string>',
839847
],
840848
[
@@ -847,6 +855,7 @@ public static function dataDescribe(): array
847855
),
848856
'int|numeric-string',
849857
'int|numeric-string',
858+
'int|numeric-string',
850859
'int|string',
851860
],
852861
[
@@ -857,6 +866,7 @@ public static function dataDescribe(): array
857866
'int<0, 4>|int<6, 10>',
858867
'int<0, 4>|int<6, 10>',
859868
'int<0, 4>|int<6, 10>',
869+
'int<0, 4>|int<6, 10>',
860870
],
861871
[
862872
TypeCombinator::union(
@@ -869,6 +879,7 @@ public static function dataDescribe(): array
869879
new NullType(),
870880
),
871881
'TFoo of int (class foo, parameter)|null',
882+
'TFoo of int (class foo, parameter)|null',
872883
'(TFoo of int)|null',
873884
'(TFoo of int)|null',
874885
],
@@ -882,6 +893,7 @@ public static function dataDescribe(): array
882893
),
883894
new GenericClassStringType(new ObjectType('Abc')),
884895
),
896+
'class-string<Abc-PHPStan\Type\ObjectType->|TFoo of int (class foo, parameter)',
885897
'class-string<Abc>|TFoo of int (class foo, parameter)',
886898
'class-string<Abc>|TFoo of int',
887899
'class-string<Abc>|TFoo of int',
@@ -897,6 +909,7 @@ public static function dataDescribe(): array
897909
new NullType(),
898910
),
899911
'TFoo (class foo, parameter)|null',
912+
'TFoo (class foo, parameter)|null',
900913
'TFoo|null',
901914
'TFoo|null',
902915
],
@@ -916,11 +929,13 @@ public static function dataDescribe(): array
916929
new NullType(),
917930
),
918931
'TFoo of TBar (class foo, parameter) (class foo, parameter)|null',
932+
'TFoo of TBar (class foo, parameter) (class foo, parameter)|null',
919933
'(TFoo of TBar)|null',
920934
'(TFoo of TBar)|null',
921935
],
922936
[
923937
new UnionType([new ObjectType('Foo'), new ObjectType('Foo')]),
938+
'Foo-PHPStan\Type\ObjectType-#1|Foo-PHPStan\Type\ObjectType-#2',
924939
'Foo#1|Foo#2',
925940
'Foo',
926941
'Foo',
@@ -931,11 +946,13 @@ public static function dataDescribe(): array
931946
#[DataProvider('dataDescribe')]
932947
public function testDescribe(
933948
Type $type,
949+
string $expectedCacheDescription,
934950
string $expectedPreciseDescription,
935951
string $expectedValueDescription,
936952
string $expectedTypeOnlyDescription,
937953
): void
938954
{
955+
$this->assertSame($expectedCacheDescription, $type->describe(VerbosityLevel::cache()));
939956
$this->assertSame($expectedPreciseDescription, $type->describe(VerbosityLevel::precise()));
940957
$this->assertSame($expectedValueDescription, $type->describe(VerbosityLevel::value()));
941958
$this->assertSame($expectedTypeOnlyDescription, $type->describe(VerbosityLevel::typeOnly()));

0 commit comments

Comments
 (0)