Skip to content

Commit

Permalink
Fix internal error
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 8, 2024
1 parent 5814c7e commit 052f6b1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Type/Php/RegexArrayShapeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
if (!$this->containsUnmatchedAsNull($flags, $matchesAll)) {
// positive match has a subject but not any capturing group
$combiType = TypeCombinator::union(
new ConstantArrayType([new ConstantIntegerType(0)], [$this->createSubjectValueType($flags, $matchesAll)], [0], [], true),
new ConstantArrayType([new ConstantIntegerType(0)], [$this->createSubjectValueType($flags, $matchesAll)], [1], [], true),
$combiType,
);
}
Expand Down Expand Up @@ -222,7 +222,7 @@ private function matchRegex(string $regex, ?int $flags, TrinaryLogic $wasMatched
)
) {
// positive match has a subject but not any capturing group
$combiTypes[] = new ConstantArrayType([new ConstantIntegerType(0)], [$this->createSubjectValueType($flags, $matchesAll)], [0], [], true);
$combiTypes[] = new ConstantArrayType([new ConstantIntegerType(0)], [$this->createSubjectValueType($flags, $matchesAll)], [1], [], true);
}

return TypeCombinator::union(...$combiTypes);
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ public function testBug3309(): void
$this->assertNoErrors($errors);
}

public function testBug11649(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-11649.php');
$this->assertNoErrors($errors);
}

public function testBug6872(): void
{
if (PHP_VERSION_ID < 80000) {
Expand Down
38 changes: 38 additions & 0 deletions tests/PHPStan/Analyser/data/bug-11649.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Bug11649;

use DateTime;

class Date
{
/**
* Characters used in formats accepted by date()
*/
protected const DATE_FORMAT_CHARACTERS = 'AaBcDdeFgGHhIijlLMmnNoOpPrsSTtUuvWwyYzZ';

/**
* Regex used to parse formats accepted by date()
*/
protected const DATE_FORMAT_REGEX = '/(?P<escaped>(?:\\\[A-Za-z])+)|[' . self::DATE_FORMAT_CHARACTERS . ']|(?P<invalid>[A-Za-z])/';

/**
* Formats a DateTime object using the current translation for weekdays and months
* @param mixed $translation
*/
public static function formatDateTime(DateTime $dateTime, string $format, ?string $language , $translation): ?string
{
return preg_replace_callback(
self::DATE_FORMAT_REGEX,
fn(array $matches): string => match ($matches[0]) {
'M' => $translation->getStrings('date.months.short')[$dateTime->format('n') - 1],
'F' => $translation->getStrings('date.months.long')[$dateTime->format('n') - 1],
'D' => $translation->getStrings('date.weekdays.short')[(int) $dateTime->format('w')],
'l' => $translation->getStrings('date.weekdays.long')[(int) $dateTime->format('w')],
'r' => static::formatDateTime($dateTime, DateTime::RFC2822, null, $translation),
default => $dateTime->format($matches[1] ?? $matches[0])
},
$format
);
}
}

0 comments on commit 052f6b1

Please sign in to comment.