Skip to content
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
34 changes: 0 additions & 34 deletions src/Type/FiniteTypeSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ final class FiniteTypeSet

private const ENUM_CASE_KEY_PREFIX = 'enum:';

private ?bool $hasClassStringMember = null;

/**
* @param array<string, Type> $members
* @param array<string, Type> $membersByKind
Expand Down Expand Up @@ -256,36 +254,4 @@ public function containedInKey(string $key): TrinaryLogic
return TrinaryLogic::createMaybe();
}

/**
* Whether a constant string member might also be a class-string.
*
* The class-string flag is part of a constant string's representation but not of its
* value, so operations that pick a member to hand back - as opposed to merely comparing
* values - cannot treat two same-valued constant strings as interchangeable. Answering
* this costs a reflection lookup per string member, so it is computed on demand: only
* combining operations ask.
*
* Every member is asked, no matter its kind: a keyed member is an instance of one of the
* five classes key() accepts, and every one of them but ConstantStringType answers
* isClassString() no outright - which is also the only one whose answer costs anything.
*/
public function hasClassStringMember(): bool
{
if ($this->hasClassStringMember !== null) {
return $this->hasClassStringMember;
}

$this->hasClassStringMember = false;
foreach ($this->members as $member) {
if ($member->isClassString()->no()) {
continue;
}

$this->hasClassStringMember = true;
break;
}

return $this->hasClassStringMember;
}

}
4 changes: 0 additions & 4 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1595,10 +1595,6 @@ private static function finiteUnionMembers(UnionType $union): ?array
return null;
}

if ($finiteTypeSet->hasClassStringMember()) {
return null;
}

return $finiteTypeSet->getMembers();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@copilot how does a nsrt test look like which is affected by the mentioned regression?

}

Expand Down
35 changes: 0 additions & 35 deletions tests/PHPStan/Type/FiniteTypeSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,41 +666,6 @@ public function testUnionComparisonsRequireBothSetsToBeComplete(): void
$this->assertSame('Maybe', $union->isAcceptedBy($otherUnion, true)->result->describe());
}

/**
* @return Iterator<string, array{list<Type>, bool}>
*/
public static function dataHasClassStringMember(): Iterator
{
yield 'plain strings' => [[new ConstantStringType('a'), new ConstantStringType('b')], false];
yield 'a value that names a class' => [[new ConstantStringType('a'), new ConstantStringType('DateTimeImmutable')], true];
yield 'the class-string flag' => [[new ConstantStringType('a'), new ConstantStringType('Zzz', true)], true];
// every member is asked, but only a string one can answer anything but no - not even
// an enum case, whose class name does name a class
yield 'no strings at all' => [
[
new ConstantIntegerType(1),
new ConstantBooleanType(true),
new NullType(),
new EnumCaseObjectType('PHPStan\Fixture\ManyCasesTestEnum', 'A'),
],
false,
];
}

/**
* @param list<Type> $types
*/
#[DataProvider('dataHasClassStringMember')]
public function testHasClassStringMember(array $types, bool $expected): void
{
$set = FiniteTypeSet::create($types);
$this->assertNotNull($set);

$this->assertSame($expected, $set->hasClassStringMember());
// answered from the cache the second time round, with the same answer
$this->assertSame($expected, $set->hasClassStringMember());
}

public function testUnionWithoutFiniteMembersHasNoSet(): void
{
$this->assertNull((new UnionType([new StringType(), new IntegerType()]))->getFiniteTypeSet());
Expand Down
Loading