Skip to content

Introduce uppercase-string #3613

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 24 commits into from
Nov 20, 2024
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
10 changes: 10 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,11 @@ services:
tags:
- phpstan.dynamicFunctionThrowTypeExtension

-
class: PHPStan\Type\Php\ParseStrParameterOutTypeExtension
tags:
- phpstan.functionParameterOutTypeExtension

-
class: PHPStan\Type\Php\PregMatchTypeSpecifyingExtension
tags:
Expand Down Expand Up @@ -1774,6 +1779,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\TrimFunctionDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\VersionCompareFunctionDynamicReturnTypeExtension
tags:
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,11 @@ parameters:
count: 1
path: src/Type/Accessory/AccessoryNumericStringType.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\IntersectionType is error\\-prone and deprecated\\.$#"
count: 1
path: src/Type/Accessory/AccessoryUppercaseStringType.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\IntersectionType is error\\-prone and deprecated\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6362,7 +6362,7 @@
'mb_strrpos' => ['0|positive-int|false', 'haystack'=>'string', 'needle'=>'string', 'offset='=>'int', 'encoding='=>'string'],
'mb_strstr' => ['string|false', 'haystack'=>'string', 'needle'=>'string', 'part='=>'bool', 'encoding='=>'string'],
'mb_strtolower' => ['lowercase-string', 'str'=>'string', 'encoding='=>'string'],
'mb_strtoupper' => ['string', 'str'=>'string', 'encoding='=>'string'],
'mb_strtoupper' => ['uppercase-string', 'str'=>'string', 'encoding='=>'string'],
'mb_strwidth' => ['0|positive-int', 'str'=>'string', 'encoding='=>'string'],
'mb_substitute_character' => ['mixed', 'substchar='=>'mixed'],
'mb_substr' => ['string', 'str'=>'string', 'start'=>'int', 'length='=>'?int', 'encoding='=>'string'],
Expand Down Expand Up @@ -12087,7 +12087,7 @@
'strtok\'1' => ['non-empty-string|false', 'token'=>'string'],
'strtolower' => ['lowercase-string', 'str'=>'string'],
'strtotime' => ['int|false', 'time'=>'string', 'now='=>'int'],
'strtoupper' => ['string', 'str'=>'string'],
'strtoupper' => ['uppercase-string', 'str'=>'string'],
'strtr' => ['string', 'str'=>'string', 'from'=>'string', 'to'=>'string'],
'strtr\'1' => ['string', 'str'=>'string', 'replace_pairs'=>'array'],
'strval' => ['string', 'var'=>'__stringAndStringable|int|float|bool|resource|null'],
Expand Down
11 changes: 11 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
Expand Down Expand Up @@ -223,6 +224,9 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
case 'lowercase-string':
return new IntersectionType([new StringType(), new AccessoryLowercaseStringType()]);

case 'uppercase-string':
return new IntersectionType([new StringType(), new AccessoryUppercaseStringType()]);

case 'literal-string':
return new IntersectionType([new StringType(), new AccessoryLiteralStringType()]);

Expand Down Expand Up @@ -303,6 +307,13 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
new AccessoryLowercaseStringType(),
]);

case 'non-empty-uppercase-string':
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
new AccessoryUppercaseStringType(),
]);

case 'truthy-string':
case 'non-falsy-string':
return new IntersectionType([
Expand Down
6 changes: 6 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
Expand Down Expand Up @@ -480,10 +481,15 @@ public function resolveConcatType(Type $left, Type $right): Type
if ($leftStringType->isLiteralString()->and($rightStringType->isLiteralString())->yes()) {
$accessoryTypes[] = new AccessoryLiteralStringType();
}

if ($leftStringType->isLowercaseString()->and($rightStringType->isLowercaseString())->yes()) {
$accessoryTypes[] = new AccessoryLowercaseStringType();
}

if ($leftStringType->isUppercaseString()->and($rightStringType->isUppercaseString())->yes()) {
$accessoryTypes[] = new AccessoryUppercaseStringType();
}

$leftNumericStringNonEmpty = TypeCombinator::remove($leftStringType, new ConstantStringType(''));
if ($leftNumericStringNonEmpty->isNumericString()->yes()) {
$allRightConstantsZeroOrMore = false;
Expand Down
2 changes: 2 additions & 0 deletions src/Rules/Api/ApiInstanceofTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\HasMethodType;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasPropertyType;
Expand Down Expand Up @@ -86,6 +87,7 @@ final class ApiInstanceofTypeRule implements Rule
AccessoryNumericStringType::class => 'Type::isNumericString()',
AccessoryLiteralStringType::class => 'Type::isLiteralString()',
AccessoryLowercaseStringType::class => 'Type::isLowercaseString()',
AccessoryUppercaseStringType::class => 'Type::isUppercaseString()',
AccessoryNonEmptyStringType::class => 'Type::isNonEmptyString()',
AccessoryNonFalsyStringType::class => 'Type::isNonFalsyString()',
HasMethodType::class => 'Type::hasMethod()',
Expand Down
11 changes: 9 additions & 2 deletions src/Rules/Comparison/StrictComparisonOfDifferentTypesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,26 @@ public function processNode(Node $node, Scope $scope): array
};

$verbosity = VerbosityLevel::value();

if (
(
$leftType->isConstantScalarValue()->yes()
&& !$leftType->isString()->no()
&& !$rightType->isConstantScalarValue()->yes()
&& !$rightType->isString()->no()
&& TrinaryLogic::extremeIdentity($leftType->isLowercaseString(), $rightType->isLowercaseString())->maybe()
&& (
TrinaryLogic::extremeIdentity($leftType->isLowercaseString(), $rightType->isLowercaseString())->maybe()
|| TrinaryLogic::extremeIdentity($leftType->isUppercaseString(), $rightType->isUppercaseString())->maybe()
)
) || (
$rightType->isConstantScalarValue()->yes()
&& !$rightType->isString()->no()
&& !$leftType->isConstantScalarValue()->yes()
&& !$leftType->isString()->no()
&& TrinaryLogic::extremeIdentity($leftType->isLowercaseString(), $rightType->isLowercaseString())->maybe()
&& (
TrinaryLogic::extremeIdentity($leftType->isLowercaseString(), $rightType->isLowercaseString())->maybe()
|| TrinaryLogic::extremeIdentity($leftType->isUppercaseString(), $rightType->isUppercaseString())->maybe()
)
)
) {
$verbosity = VerbosityLevel::precise();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ public function isLowercaseString(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function isUppercaseString(): TrinaryLogic
{
return TrinaryLogic::createNo();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ public function isLowercaseString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isUppercaseString(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryLowercaseStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ public function isLowercaseString(): TrinaryLogic
return TrinaryLogic::createYes();
}

public function isUppercaseString(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ public function isLowercaseString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isUppercaseString(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ public function isLowercaseString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isUppercaseString(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ public function isLowercaseString(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function isUppercaseString(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
}

public function isClassStringType(): TrinaryLogic
{
return TrinaryLogic::createNo();
Expand Down
Loading
Loading