Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc9ae89c

Browse files
committed
Handle ConstantArrayType == ConstantArrayType
1 parent6e02453 commitc9ae89c

File tree

3 files changed

+81
-57
lines changed

3 files changed

+81
-57
lines changed

‎src/Analyser/MutatingScope.php‎

Lines changed: 73 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -628,18 +628,7 @@ private function resolveType(Expr $node): Type
628628
$leftType =$this->getType($node->left);
629629
$rightType =$this->getType($node->right);
630630

631-
$stringType =newStringType();
632-
$integerType =newIntegerType();
633-
$floatType =newFloatType();
634-
if (
635-
($stringType->isSuperTypeOf($leftType)->yes() &&$stringType->isSuperTypeOf($rightType)->yes())
636-
|| ($integerType->isSuperTypeOf($leftType)->yes() &&$integerType->isSuperTypeOf($rightType)->yes())
637-
|| ($floatType->isSuperTypeOf($leftType)->yes() &&$floatType->isSuperTypeOf($rightType)->yes())
638-
) {
639-
return$this->getType(newExpr\BinaryOp\Identical($node->left,$node->right));
640-
}
641-
642-
returnnewBooleanType();
631+
return$this->resolveEqualType($leftType,$rightType);
643632
}
644633

645634
if ($nodeinstanceofExpr\BinaryOp\NotEqual) {
@@ -2335,7 +2324,7 @@ private function resolveType(Expr $node): Type
23352324
returnnewMixedType();
23362325
}
23372326

2338-
privatefunctionresolveIdenticalType(Type$leftType,Type$rightType):Type
2327+
privatefunctionresolveIdenticalType(Type$leftType,Type$rightType):BooleanType
23392328
{
23402329
$isSuperset =$leftType->isSuperTypeOf($rightType);
23412330
if ($isSuperset->no()) {
@@ -2350,65 +2339,93 @@ private function resolveIdenticalType(Type $leftType, Type $rightType): Type
23502339
}
23512340

23522341
if ($leftTypeinstanceof ConstantArrayType &&$rightTypeinstanceof ConstantArrayType) {
2353-
$leftValueTypes =$leftType->getValueTypes();
2354-
$rightKeyTypes =$rightType->getKeyTypes();
2355-
$rightValueTypes =$rightType->getValueTypes();
2356-
$hasOptional =false;
2357-
foreach ($leftType->getKeyTypes()as$i =>$keyType) {
2358-
if (!array_key_exists($i,$rightKeyTypes)) {
2359-
if ($leftType->isOptionalKey($i)) {
2360-
$hasOptional =true;
2361-
continue;
2362-
}
2363-
returnnewConstantBooleanType(false);
2364-
}
2342+
return$this->resolveConstantArrayTypeComparison($leftType,$rightType,fn ($leftValueType,$rightValueType):BooleanType =>$this->resolveIdenticalType($leftValueType,$rightValueType));
2343+
}
23652344

2366-
$rightKeyType =$rightKeyTypes[$i];
2367-
if (!$keyType->equals($rightKeyType)) {
2368-
returnnewConstantBooleanType(false);
2369-
}
2345+
returnnewBooleanType();
2346+
}
23702347

2371-
$leftValueType =$leftValueTypes[$i];
2372-
$rightValueType =$rightValueTypes[$i];
2373-
$leftIdenticalToRight =$this->resolveIdenticalType($leftValueType,$rightValueType);
2374-
if ($leftIdenticalToRightinstanceof ConstantBooleanType) {
2375-
if (!$leftIdenticalToRight->getValue()) {
2376-
returnnewConstantBooleanType(false);
2377-
}
2378-
$isLeftOptional =$leftType->isOptionalKey($i);
2379-
if ($isLeftOptional ||$rightType->isOptionalKey($i)) {
2380-
$hasOptional =true;
2381-
}
2382-
continue;
2383-
}
2348+
privatefunctionresolveEqualType(Type$leftType,Type$rightType):BooleanType
2349+
{
2350+
$stringType =newStringType();
2351+
$integerType =newIntegerType();
2352+
$floatType =newFloatType();
2353+
if (
2354+
($stringType->isSuperTypeOf($leftType)->yes() &&$stringType->isSuperTypeOf($rightType)->yes())
2355+
|| ($integerType->isSuperTypeOf($leftType)->yes() &&$integerType->isSuperTypeOf($rightType)->yes())
2356+
|| ($floatType->isSuperTypeOf($leftType)->yes() &&$floatType->isSuperTypeOf($rightType)->yes())
2357+
) {
2358+
return$this->resolveIdenticalType($leftType,$rightType);
2359+
}
23842360

2385-
$hasOptional =true;
2386-
}
2361+
if ($leftTypeinstanceof ConstantArrayType &&$rightTypeinstanceof ConstantArrayType) {
2362+
return$this->resolveConstantArrayTypeComparison($leftType,$rightType,fn ($leftValueType,$rightValueType):BooleanType =>$this->resolveEqualType($leftValueType,$rightValueType));
2363+
}
23872364

2388-
if (!isset($i)) {
2389-
$i =0;
2390-
}else {
2391-
$i++;
2392-
}
2365+
returnnewBooleanType();
2366+
}
23932367

2394-
$rightKeyTypesCount =count($rightKeyTypes);
2395-
for (;$i <$rightKeyTypesCount;$i++) {
2396-
if ($rightType->isOptionalKey($i)) {
2368+
/**
2369+
* @param callable(Type, Type): BooleanType $valueComparisonCallback
2370+
*/
2371+
privatefunctionresolveConstantArrayTypeComparison(ConstantArrayType$leftType,ConstantArrayType$rightType,callable$valueComparisonCallback):BooleanType
2372+
{
2373+
$leftValueTypes =$leftType->getValueTypes();
2374+
$rightKeyTypes =$rightType->getKeyTypes();
2375+
$rightValueTypes =$rightType->getValueTypes();
2376+
$hasOptional =false;
2377+
foreach ($leftType->getKeyTypes()as$i =>$keyType) {
2378+
if (!array_key_exists($i,$rightKeyTypes)) {
2379+
if ($leftType->isOptionalKey($i)) {
23972380
$hasOptional =true;
23982381
continue;
23992382
}
2383+
returnnewConstantBooleanType(false);
2384+
}
24002385

2386+
$rightKeyType =$rightKeyTypes[$i];
2387+
if (!$keyType->equals($rightKeyType)) {
24012388
returnnewConstantBooleanType(false);
24022389
}
24032390

2404-
if ($hasOptional) {
2405-
returnnewBooleanType();
2391+
$leftValueType =$leftValueTypes[$i];
2392+
$rightValueType =$rightValueTypes[$i];
2393+
$leftIdenticalToRight =$valueComparisonCallback($leftValueType,$rightValueType);
2394+
if ($leftIdenticalToRightinstanceof ConstantBooleanType) {
2395+
if (!$leftIdenticalToRight->getValue()) {
2396+
returnnewConstantBooleanType(false);
2397+
}
2398+
$isLeftOptional =$leftType->isOptionalKey($i);
2399+
if ($isLeftOptional ||$rightType->isOptionalKey($i)) {
2400+
$hasOptional =true;
2401+
}
2402+
continue;
24062403
}
24072404

2408-
returnnewConstantBooleanType(true);
2405+
$hasOptional =true;
24092406
}
24102407

2411-
returnnewBooleanType();
2408+
if (!isset($i)) {
2409+
$i =0;
2410+
}else {
2411+
$i++;
2412+
}
2413+
2414+
$rightKeyTypesCount =count($rightKeyTypes);
2415+
for (;$i <$rightKeyTypesCount;$i++) {
2416+
if ($rightType->isOptionalKey($i)) {
2417+
$hasOptional =true;
2418+
continue;
2419+
}
2420+
2421+
returnnewConstantBooleanType(false);
2422+
}
2423+
2424+
if ($hasOptional) {
2425+
returnnewBooleanType();
2426+
}
2427+
2428+
returnnewConstantBooleanType(true);
24122429
}
24132430

24142431
privatefunctionresolveConcatType(Expr\BinaryOp\Concat|Expr\AssignOp\Concat$node):Type

‎tests/PHPStan/Analyser/data/bug-6940.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Bug6940
1010
publicfunctionfoo():void
1111
{
1212
$b = [] == [];
13-
assertType('bool',$b);
13+
assertType('true',$b);
1414
}
1515

1616
}

‎tests/PHPStan/Analyser/data/constant-array-type-identical.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ class Foo
1111
publicfunctiondoFoo(string$s):void
1212
{
1313
assertType('true', [1] === [1]);
14+
assertType('true', [1] == [1]);
15+
assertType('false', [1] != [1]);
16+
assertType('false', [1] == [2]);
17+
assertType('true', [1] != [2]);
18+
assertType('bool', [1] == ["1"]);
19+
assertType('bool', [1] != ["1"]);
20+
1421
assertType('false', [1] === [2]);
1522
assertType('false', [1] !== [1]);
1623
assertType('true', [1] !== [2]);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp