Skip to content

Commit d43ca1b

Browse files
committed
fix
1 parent 47b54f9 commit d43ca1b

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

Diff for: src/Type/Regex/RegexGroupParser.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,17 @@ public function parseGroups(string $regex): ?RegexAstWalkResult
110110
RegexGroupWalkResult::createEmpty(),
111111
);
112112

113-
// we could handle numeric-string, in case we know the regex is delimited by ^ and $
114-
if ($subjectAsGroupResult->isNonFalsy()->yes()) {
115-
$astWalkResult = $astWalkResult->withSubjectBaseType(
116-
TypeCombinator::intersect(new StringType(), new AccessoryNonFalsyStringType()),
117-
);
118-
} elseif ($subjectAsGroupResult->isNonEmpty()->yes()) {
119-
$astWalkResult = $astWalkResult->withSubjectBaseType(
120-
TypeCombinator::intersect(new StringType(), new AccessoryNonEmptyStringType()),
121-
);
113+
if (!$subjectAsGroupResult->containsEmptyStringLiteral()) {
114+
// we could handle numeric-string, in case we know the regex is delimited by ^ and $
115+
if ($subjectAsGroupResult->isNonFalsy()->yes()) {
116+
$astWalkResult = $astWalkResult->withSubjectBaseType(
117+
TypeCombinator::intersect(new StringType(), new AccessoryNonFalsyStringType()),
118+
);
119+
} elseif ($subjectAsGroupResult->isNonEmpty()->yes()) {
120+
$astWalkResult = $astWalkResult->withSubjectBaseType(
121+
TypeCombinator::intersect(new StringType(), new AccessoryNonEmptyStringType()),
122+
);
123+
}
122124
}
123125

124126
return $astWalkResult;

Diff for: src/Type/Regex/RegexGroupWalkResult.php

+14
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ public function getOnlyLiterals(): ?array
103103
return $this->onlyLiterals;
104104
}
105105

106+
public function containsEmptyStringLiteral(): bool
107+
{
108+
if ($this->onlyLiterals === null) {
109+
return false;
110+
}
111+
foreach ($this->onlyLiterals as $onlyLiteral) {
112+
if ($onlyLiteral === '') {
113+
return true;
114+
}
115+
}
116+
117+
return false;
118+
}
119+
106120
public function isNonEmpty(): TrinaryLogic
107121
{
108122
return $this->isNonEmpty;

Diff for: tests/PHPStan/Analyser/nsrt/preg_match_shapes.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -915,31 +915,31 @@ function bugEmptySubexpression (string $string): void {
915915
}
916916

917917
if (preg_match('~|(a)~', $string, $matches)) {
918-
assertType("array{0: non-empty-string, 1?: 'a'}", $matches);
918+
assertType("array{0: string, 1?: 'a'}", $matches);
919919
}
920920

921921
if (preg_match('~(a)|~', $string, $matches)) {
922-
assertType("array{0: non-empty-string, 1?: 'a'}", $matches);
922+
assertType("array{0: string, 1?: 'a'}", $matches);
923923
}
924924

925925
if (preg_match('~(a)||(b)~', $string, $matches)) {
926-
assertType("array{0: non-empty-string, 1?: 'a'}|array{non-empty-string, '', 'b'}", $matches);
926+
assertType("array{0: string, 1?: 'a'}|array{string, '', 'b'}", $matches);
927927
}
928928

929929
if (preg_match('~(|(a))~', $string, $matches)) {
930-
assertType("array{0: non-empty-string, 1: ''|'a', 2?: 'a'}", $matches);
930+
assertType("array{0: string, 1: ''|'a', 2?: 'a'}", $matches);
931931
}
932932

933933
if (preg_match('~((a)|)~', $string, $matches)) {
934-
assertType("array{0: non-empty-string, 1: ''|'a', 2?: 'a'}", $matches);
934+
assertType("array{0: string, 1: ''|'a', 2?: 'a'}", $matches);
935935
}
936936

937937
if (preg_match('~((a)||(b))~', $string, $matches)) {
938-
assertType("array{0: non-empty-string, 1: ''|'a'|'b', 2?: ''|'a', 3?: 'b'}", $matches);
938+
assertType("array{0: string, 1: ''|'a'|'b', 2?: ''|'a', 3?: 'b'}", $matches);
939939
}
940940

941941
if (preg_match('~((a)|()|(b))~', $string, $matches)) {
942-
assertType("array{0: non-empty-string, 1: ''|'a'|'b', 2?: ''|'a', 3?: '', 4?: 'b'}", $matches);
942+
assertType("array{0: string, 1: ''|'a'|'b', 2?: ''|'a', 3?: '', 4?: 'b'}", $matches);
943943
}
944944
}
945945

0 commit comments

Comments
 (0)