Skip to content

Fix lowercase string usage in template #4122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
if (
($type instanceof AccessoryLowercaseStringType || $type instanceof AccessoryUppercaseStringType)
&& !$level->isPrecise()
&& !$level->isCache()
) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function describe(VerbosityLevel $level): string
}
}

if ($level->isPrecise()) {
if ($level->isPrecise() || $level->isCache()) {
$duplicates = array_diff_assoc($typeNames, array_unique($typeNames));
if (count($duplicates) > 0) {
$indexByDuplicate = array_fill_keys($duplicates, 0);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/VerbosityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public function isPrecise(): bool
return $this->value === self::PRECISE;
}

public function isCache(): bool
{
return $this->value === self::CACHE;
}

/** @api */
public static function getRecommendedLevelByType(Type $acceptingType, ?Type $acceptedType = null): self
{
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,11 @@ public function testBug13043(): void
$this->analyse([__DIR__ . '/data/bug-13043.php'], []);
}

public function testBug12739(): void
{
$this->analyse([__DIR__ . '/data/bug-12739.php'], []);
}

public function testBug12928(): void
{
$this->analyse([__DIR__ . '/data/bug-12928.php'], [
Expand Down
58 changes: 58 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-12739.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types = 1);

/**
* @template TScalar of scalar
*/
abstract class ScalarType {
/**
* @return class-string<ScalarType<TScalar>>
*/
abstract public static function getClassIdentify() : string;
}

/**
* @extends ScalarType<int>
*/
class MyInt extends ScalarType {
public static function getClassIdentify() : string {
return MyInt::class;
}
}

/**
* @extends ScalarType<string>
*/
class MyString extends ScalarType {
public static function getClassIdentify() : string {
return MyString::class;
}
}

/**
* @extends ScalarType<lowercase-string>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the fix also affect other string types, which should be tested here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, uppercaseString.
I added a test.

*/
class MyLowerString extends ScalarType {
public static function getClassIdentify() : string {
return MyLowerString::class;
}
}

/**
* @extends ScalarType<uppercase-string>
*/
class MyUpperString extends ScalarType {
public static function getClassIdentify() : string {
return MyUpperString::class;
}
}

/**
* @extends ScalarType<class-string>
*/
class MyClassString extends ScalarType {
public static function getClassIdentify() : string {
return MyClassString::class;
}
}
17 changes: 17 additions & 0 deletions tests/PHPStan/Type/UnionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,12 +708,14 @@ public static function dataDescribe(): array
'int|string',
'int|string',
'int|string',
'int|string',
],
[
new UnionType([new IntegerType(), new StringType(), new NullType()]),
'int|string|null',
'int|string|null',
'int|string|null',
'int|string|null',
],
[
new UnionType([
Expand All @@ -733,6 +735,7 @@ public static function dataDescribe(): array
new ConstantStringType('2'),
new ConstantStringType('1'),
]),
"1|2|2.2|10|'1'|'10'|'10aaa'|'11aaa'|'1aaa'|'2'|'2aaa'|'foo'|stdClass-PHPStan\Type\ObjectType-|true|null",
"1|2|2.2|10|'1'|'10'|'10aaa'|'11aaa'|'1aaa'|'2'|'2aaa'|'foo'|stdClass|true|null",
"1|2|2.2|10|'1'|'10'|'10aaa'|'11aaa'|'1aaa'|'2'|'2aaa'|'foo'|stdClass|true|null",
'float|int|stdClass|string|true|null',
Expand All @@ -757,6 +760,7 @@ public static function dataDescribe(): array
),
'\'aaa\'|array{a: int, b: float}|array{a: string, b: bool}',
'\'aaa\'|array{a: int, b: float}|array{a: string, b: bool}',
'\'aaa\'|array{a: int, b: float}|array{a: string, b: bool}',
'array<string, bool|float|int|string>|string',
],
[
Expand All @@ -779,6 +783,7 @@ public static function dataDescribe(): array
),
'\'aaa\'|array{a: string, b: bool}|array{b: int, c: float}',
'\'aaa\'|array{a: string, b: bool}|array{b: int, c: float}',
'\'aaa\'|array{a: string, b: bool}|array{b: int, c: float}',
'array<string, bool|float|int|string>|string',
],
[
Expand All @@ -801,6 +806,7 @@ public static function dataDescribe(): array
),
'\'aaa\'|array{a: string, b: bool}|array{c: int, d: float}',
'\'aaa\'|array{a: string, b: bool}|array{c: int, d: float}',
'\'aaa\'|array{a: string, b: bool}|array{c: int, d: float}',
'array<string, bool|float|int|string>|string',
],
[
Expand All @@ -822,6 +828,7 @@ public static function dataDescribe(): array
),
'array{int, bool, float}|array{string}',
'array{int, bool, float}|array{string}',
'array{int, bool, float}|array{string}',
'array<int, bool|float|int|string>',
],
[
Expand All @@ -835,6 +842,7 @@ public static function dataDescribe(): array
),
'array{}|array{foooo: \'barrr\'}',
'array{}|array{foooo: \'barrr\'}',
'array{}|array{foooo: \'barrr\'}',
'array<string, string>',
],
[
Expand All @@ -847,6 +855,7 @@ public static function dataDescribe(): array
),
'int|numeric-string',
'int|numeric-string',
'int|numeric-string',
'int|string',
],
[
Expand All @@ -857,6 +866,7 @@ public static function dataDescribe(): array
'int<0, 4>|int<6, 10>',
'int<0, 4>|int<6, 10>',
'int<0, 4>|int<6, 10>',
'int<0, 4>|int<6, 10>',
],
[
TypeCombinator::union(
Expand All @@ -869,6 +879,7 @@ public static function dataDescribe(): array
new NullType(),
),
'TFoo of int (class foo, parameter)|null',
'TFoo of int (class foo, parameter)|null',
'(TFoo of int)|null',
'(TFoo of int)|null',
],
Expand All @@ -882,6 +893,7 @@ public static function dataDescribe(): array
),
new GenericClassStringType(new ObjectType('Abc')),
),
'class-string<Abc-PHPStan\Type\ObjectType->|TFoo of int (class foo, parameter)',
'class-string<Abc>|TFoo of int (class foo, parameter)',
'class-string<Abc>|TFoo of int',
'class-string<Abc>|TFoo of int',
Expand All @@ -897,6 +909,7 @@ public static function dataDescribe(): array
new NullType(),
),
'TFoo (class foo, parameter)|null',
'TFoo (class foo, parameter)|null',
'TFoo|null',
'TFoo|null',
],
Expand All @@ -916,11 +929,13 @@ public static function dataDescribe(): array
new NullType(),
),
'TFoo of TBar (class foo, parameter) (class foo, parameter)|null',
'TFoo of TBar (class foo, parameter) (class foo, parameter)|null',
'(TFoo of TBar)|null',
'(TFoo of TBar)|null',
],
[
new UnionType([new ObjectType('Foo'), new ObjectType('Foo')]),
'Foo-PHPStan\Type\ObjectType-#1|Foo-PHPStan\Type\ObjectType-#2',
'Foo#1|Foo#2',
'Foo',
'Foo',
Expand All @@ -931,11 +946,13 @@ public static function dataDescribe(): array
#[DataProvider('dataDescribe')]
public function testDescribe(
Type $type,
string $expectedCacheDescription,
string $expectedPreciseDescription,
string $expectedValueDescription,
string $expectedTypeOnlyDescription,
): void
{
$this->assertSame($expectedCacheDescription, $type->describe(VerbosityLevel::cache()));
$this->assertSame($expectedPreciseDescription, $type->describe(VerbosityLevel::precise()));
$this->assertSame($expectedValueDescription, $type->describe(VerbosityLevel::value()));
$this->assertSame($expectedTypeOnlyDescription, $type->describe(VerbosityLevel::typeOnly()));
Expand Down
Loading