Skip to content
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

Fix union of lowercase/uppercase string with empty string #3751

Open
wants to merge 1 commit into
base: 1.12.x
Choose a base branch
from
Open
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
38 changes: 32 additions & 6 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace PHPStan\Type;

use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Accessory\HasPropertyType;
Expand Down Expand Up @@ -452,7 +454,10 @@ private static function compareTypesInUnion(Type $a, Type $b): ?array
&& ($b->describe(VerbosityLevel::value()) === 'non-empty-string'
|| $b->describe(VerbosityLevel::value()) === 'non-falsy-string')
) {
return [null, new StringType()];
return [null, self::intersect(
new StringType(),
...self::getAccessoryCaseStringTypes($b),
)];
}

if (
Expand All @@ -461,34 +466,55 @@ private static function compareTypesInUnion(Type $a, Type $b): ?array
&& ($a->describe(VerbosityLevel::value()) === 'non-empty-string'
|| $a->describe(VerbosityLevel::value()) === 'non-falsy-string')
) {
return [new StringType(), null];
return [self::intersect(
new StringType(),
...self::getAccessoryCaseStringTypes($a),
), null];
}

if (
$a instanceof ConstantStringType
&& $a->getValue() === '0'
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like we need another test (the change seems untested)

&& $b->describe(VerbosityLevel::value()) === 'non-falsy-string'
) {
return [null, new IntersectionType([
return [null, self::intersect(
new StringType(),
new AccessoryNonEmptyStringType(),
])];
...self::getAccessoryCaseStringTypes($b),
)];
}

if (
$b instanceof ConstantStringType
&& $b->getValue() === '0'
&& $a->describe(VerbosityLevel::value()) === 'non-falsy-string'
) {
return [new IntersectionType([
return [self::intersect(
new StringType(),
new AccessoryNonEmptyStringType(),
]), null];
...self::getAccessoryCaseStringTypes($a),
), null];
}

return null;
}

/**
* @return array<Type>
*/
private static function getAccessoryCaseStringTypes(Type $type): array
{
$accessory = [];
if ($type->isLowercaseString()->yes()) {
$accessory[] = new AccessoryLowercaseStringType();
}
if ($type->isUppercaseString()->yes()) {
$accessory[] = new AccessoryUppercaseStringType();
}

return $accessory;
}

private static function unionWithSubtractedType(
Type $type,
?Type $subtractedType,
Expand Down
41 changes: 41 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12312.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace Bug12312;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param lowercase-string $s
*/
public function sayLowercase(string $s): void
{
if ($s != '') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need another test test for the condition with operands in reversed order

assertType('lowercase-string&non-empty-string', $s);
}
assertType('lowercase-string', $s);
}

/**
* @param uppercase-string $s
*/
public function sayUppercase(string $s): void
{
if ($s != '') {
assertType('non-empty-string&uppercase-string', $s);
}
assertType('uppercase-string', $s);
}

/**
* @param lowercase-string&uppercase-string $s
*/
public function sayBoth(string $s): void
{
if ($s != '') {
assertType('lowercase-string&non-empty-string&uppercase-string', $s);
}
assertType('lowercase-string&uppercase-string', $s);
}
}
Loading