Skip to content

RegexArrayShapeMatcher - turn more details immutable #3892

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 6 commits into from
Mar 21, 2025
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
102 changes: 16 additions & 86 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NullType;
use PHPStan\Type\Regex\RegexAlternation;
use PHPStan\Type\Regex\RegexCapturingGroup;
use PHPStan\Type\Regex\RegexExpressionHelper;
use PHPStan\Type\Regex\RegexGroupList;
use PHPStan\Type\Regex\RegexGroupParser;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function array_reverse;
use function count;
use function in_array;
use function is_string;
Expand Down Expand Up @@ -115,16 +114,10 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
}
[$groupList, $markVerbs] = $parseResult;

$trailingOptionals = 0;
foreach (array_reverse($groupList) as $captureGroup) {
if (!$captureGroup->isOptional()) {
break;
}
$trailingOptionals++;
}

$onlyOptionalTopLevelGroup = $this->getOnlyOptionalTopLevelGroup($groupList);
$onlyTopLevelAlternation = $this->getOnlyTopLevelAlternation($groupList);
Comment on lines -118 to -127
Copy link
Contributor Author

Choose a reason for hiding this comment

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

move operations previously did on array<int, RegexCapturingGroup> $captureGroups into a dedicated class

$regexGroupList = new RegexGroupList($groupList);
$trailingOptionals = $regexGroupList->countTrailingOptionals();
$onlyOptionalTopLevelGroup = $regexGroupList->getOnlyOptionalTopLevelGroup();
$onlyTopLevelAlternation = $regexGroupList->getOnlyTopLevelAlternation();
$flags ??= 0;

if (
Expand All @@ -134,11 +127,10 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
) {
// if only one top level capturing optional group exists
// we build a more precise tagged union of a empty-match and a match with the group

$onlyOptionalTopLevelGroup->forceNonOptional();
$regexGroupList = $regexGroupList->forceGroupNonOptional($onlyOptionalTopLevelGroup);

$combiType = $this->buildArrayType(
$groupList,
$regexGroupList,
$wasMatched,
$trailingOptionals,
$flags,
Expand All @@ -154,8 +146,6 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
);
}

$onlyOptionalTopLevelGroup->clearOverrides();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no more state clearing/handling


return $combiType;
} elseif (
!$matchesAll
Expand All @@ -168,24 +158,24 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
$combiTypes = [];
$isOptionalAlternation = false;
foreach ($onlyTopLevelAlternation->getGroupCombinations() as $groupCombo) {
$comboList = $groupList;
$comboList = new RegexGroupList($groupList);

$beforeCurrentCombo = true;
foreach ($comboList as $groupId => $group) {
if (in_array($groupId, $groupCombo, true)) {
foreach ($comboList as $group) {
if (in_array($group->getId(), $groupCombo, true)) {
$isOptionalAlternation = $group->inOptionalAlternation();
$group->forceNonOptional();
$comboList = $comboList->forceGroupNonOptional($group);
$beforeCurrentCombo = false;
} elseif ($beforeCurrentCombo && !$group->resetsGroupCounter()) {
$group->forceNonOptional();
$group->forceType(
$comboList = $comboList->forceGroupTypeAndNonOptional(
$group,
$this->containsUnmatchedAsNull($flags, $matchesAll) ? new NullType() : new ConstantStringType(''),
);
} elseif (
$group->getAlternationId() === $onlyTopLevelAlternation->getId()
&& !$this->containsUnmatchedAsNull($flags, $matchesAll)
) {
unset($comboList[$groupId]);
$comboList = $comboList->removeGroup($group);
}
}

Expand All @@ -199,11 +189,6 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
);

$combiTypes[] = $combiType;

foreach ($groupCombo as $groupId) {
$group = $comboList[$groupId];
$group->clearOverrides();
}
Comment on lines -203 to -206
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no more state clearing/handling

}

if (
Expand All @@ -223,7 +208,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
// the general case, which should work in all cases but does not yield the most
// precise result possible in some cases
return $this->buildArrayType(
$groupList,
$regexGroupList,
$wasMatched,
$trailingOptionals,
$flags,
Expand All @@ -233,65 +218,10 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
}

/**
* @param array<int, RegexCapturingGroup> $captureGroups
*/
private function getOnlyOptionalTopLevelGroup(array $captureGroups): ?RegexCapturingGroup
{
$group = null;
foreach ($captureGroups as $captureGroup) {
if (!$captureGroup->isTopLevel()) {
continue;
}

if (!$captureGroup->isOptional()) {
return null;
}

if ($group !== null) {
return null;
}

$group = $captureGroup;
}

return $group;
}

/**
* @param array<int, RegexCapturingGroup> $captureGroups
*/
private function getOnlyTopLevelAlternation(array $captureGroups): ?RegexAlternation
{
$alternation = null;
foreach ($captureGroups as $captureGroup) {
if (!$captureGroup->isTopLevel()) {
continue;
}

if (!$captureGroup->inAlternation()) {
return null;
}

if ($captureGroup->inOptionalQuantification()) {
return null;
}

if ($alternation === null) {
$alternation = $captureGroup->getAlternation();
} elseif ($alternation->getId() !== $captureGroup->getAlternation()->getId()) {
return null;
}
}

return $alternation;
}

/**
* @param array<RegexCapturingGroup> $captureGroups
* @param list<string> $markVerbs
*/
private function buildArrayType(
array $captureGroups,
RegexGroupList $captureGroups,
TrinaryLogic $wasMatched,
int $trailingOptionals,
int $flags,
Expand Down
51 changes: 40 additions & 11 deletions src/Type/Regex/RegexCapturingGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
final class RegexCapturingGroup
{

private bool $forceNonOptional = false;

private ?Type $forceType = null;

public function __construct(
private readonly int $id,
private readonly ?string $name,
private readonly ?RegexAlternation $alternation,
private readonly bool $inOptionalQuantification,
private readonly RegexCapturingGroup|RegexNonCapturingGroup|null $parent,
private readonly Type $type,
private readonly bool $forceNonOptional = false,
private readonly ?Type $forceType = null,
)
{
}
Expand All @@ -27,20 +25,46 @@ public function getId(): int
return $this->id;
}

public function forceNonOptional(): void
public function forceNonOptional(): self
{
$this->forceNonOptional = true;
return new self(
$this->id,
$this->name,
$this->alternation,
$this->inOptionalQuantification,
$this->parent,
$this->type,
true,
$this->forceType,
);
}

public function forceType(Type $type): void
public function forceType(Type $type): self
{
$this->forceType = $type;
return new self(
$this->id,
$this->name,
$this->alternation,
$this->inOptionalQuantification,
$this->parent,
$type,
$this->forceNonOptional,
$this->forceType,
);
}

public function clearOverrides(): void
public function withParent(RegexCapturingGroup|RegexNonCapturingGroup $parent): self
{
$this->forceNonOptional = false;
$this->forceType = null;
return new self(
$this->id,
$this->name,
$this->alternation,
$this->inOptionalQuantification,
$parent,
$this->type,
$this->forceNonOptional,
$this->forceType,
);
}

public function resetsGroupCounter(): bool
Expand Down Expand Up @@ -128,4 +152,9 @@ public function getType(): Type
return $this->type;
}

public function getParent(): RegexCapturingGroup|RegexNonCapturingGroup|null
{
return $this->parent;
}

}
Loading
Loading