Skip to content
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
4 changes: 2 additions & 2 deletions src/Type/Generic/TemplateTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@
{
if (!$templateType->getVariance()->covariant()) {
$isArrayKey = $templateType->getBound()->describe(VerbosityLevel::precise()) === '(int|string)';
if ($type->isScalar()->yes() && $isArrayKey) {
if ($type->isInteger()->yes() && $isArrayKey) {

Check warning on line 145 in src/Type/Generic/TemplateTypeHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ { if (!$templateType->getVariance()->covariant()) { $isArrayKey = $templateType->getBound()->describe(VerbosityLevel::precise()) === '(int|string)'; - if ($type->isInteger()->yes() && $isArrayKey) { + if (!$type->isInteger()->no() && $isArrayKey) { $type = $type->generalize(GeneralizePrecision::templateArgument()); } elseif ($type->isConstantValue()->yes() && !$templateType->getBound()->isScalar()->yes()) { $type = $type->generalize(GeneralizePrecision::templateArgument());

Check warning on line 145 in src/Type/Generic/TemplateTypeHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ { if (!$templateType->getVariance()->covariant()) { $isArrayKey = $templateType->getBound()->describe(VerbosityLevel::precise()) === '(int|string)'; - if ($type->isInteger()->yes() && $isArrayKey) { + if (!$type->isInteger()->no() && $isArrayKey) { $type = $type->generalize(GeneralizePrecision::templateArgument()); } elseif ($type->isConstantValue()->yes() && !$templateType->getBound()->isScalar()->yes()) { $type = $type->generalize(GeneralizePrecision::templateArgument());
Copy link
Contributor

Choose a reason for hiding this comment

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

I dont see a reason to have a different behavior for int and string

$type = $type->generalize(GeneralizePrecision::templateArgument());
} elseif ($type->isConstantValue()->yes() && (!$templateType->getBound()->isScalar()->yes() || $isArrayKey)) {
} elseif ($type->isConstantValue()->yes() && !$templateType->getBound()->isScalar()->yes()) {

Check warning on line 147 in src/Type/Generic/TemplateTypeHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $isArrayKey = $templateType->getBound()->describe(VerbosityLevel::precise()) === '(int|string)'; if ($type->isInteger()->yes() && $isArrayKey) { $type = $type->generalize(GeneralizePrecision::templateArgument()); - } elseif ($type->isConstantValue()->yes() && !$templateType->getBound()->isScalar()->yes()) { + } elseif ($type->isConstantValue()->yes() && $templateType->getBound()->isScalar()->no()) { $type = $type->generalize(GeneralizePrecision::templateArgument()); } }

Check warning on line 147 in src/Type/Generic/TemplateTypeHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $isArrayKey = $templateType->getBound()->describe(VerbosityLevel::precise()) === '(int|string)'; if ($type->isInteger()->yes() && $isArrayKey) { $type = $type->generalize(GeneralizePrecision::templateArgument()); - } elseif ($type->isConstantValue()->yes() && !$templateType->getBound()->isScalar()->yes()) { + } elseif (!$type->isConstantValue()->no() && !$templateType->getBound()->isScalar()->yes()) { $type = $type->generalize(GeneralizePrecision::templateArgument()); } }

Check warning on line 147 in src/Type/Generic/TemplateTypeHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $isArrayKey = $templateType->getBound()->describe(VerbosityLevel::precise()) === '(int|string)'; if ($type->isInteger()->yes() && $isArrayKey) { $type = $type->generalize(GeneralizePrecision::templateArgument()); - } elseif ($type->isConstantValue()->yes() && !$templateType->getBound()->isScalar()->yes()) { + } elseif ($type->isConstantValue()->yes() && $templateType->getBound()->isScalar()->no()) { $type = $type->generalize(GeneralizePrecision::templateArgument()); } }

Check warning on line 147 in src/Type/Generic/TemplateTypeHelper.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $isArrayKey = $templateType->getBound()->describe(VerbosityLevel::precise()) === '(int|string)'; if ($type->isInteger()->yes() && $isArrayKey) { $type = $type->generalize(GeneralizePrecision::templateArgument()); - } elseif ($type->isConstantValue()->yes() && !$templateType->getBound()->isScalar()->yes()) { + } elseif (!$type->isConstantValue()->no() && !$templateType->getBound()->isScalar()->yes()) { $type = $type->generalize(GeneralizePrecision::templateArgument()); } }
$type = $type->generalize(GeneralizePrecision::templateArgument());
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-13144.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

$arr = new ArrayObject(['a' => 1, 'b' => 2]);

assertType('ArrayObject<string, int>', $arr); // correctly inferred as `ArrayObject<string, int>`
Copy link
Contributor

Choose a reason for hiding this comment

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

I dunno if this will create issues...

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure either.

related issue was comment to be blocked on bidrectional narrowing:
phpstan/phpstan#8031 (comment)

assertType("ArrayObject<'a'|'b', int>", $arr);

$a = $arr['a']; // ok
$b = $arr['b']; // ok
Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-8031.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug8031;

use function PHPStan\Testing\assertType;

/**
* @template TKey of array-key
* @template TValue of mixed
*/
class Collection
{
/**
* @param array<TKey, TValue> $val
*/
public function __construct(protected array $val) {}
}

/**
* @return Collection<'one'|'two', int>
*/
function test(): Collection
{
$c = new Collection([
'one' => 1,
'two' => 2,
]);
assertType("Bug8031\Collection<'one'|'two', int>", $c);
return $c;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
function () {
assertType('ArrayObject<*NEVER*, *NEVER*>', new \ArrayObject());
assertType('ArrayObject<*NEVER*, *NEVER*>', new \ArrayObject([]));
assertType('ArrayObject<string, int>', new \ArrayObject(['key' => 1]));
assertType("ArrayObject<'key', int>", new \ArrayObject(['key' => 1]));
};
Loading