Skip to content

Commit

Permalink
Merge branch '11.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jan 28, 2025
2 parents d7c0cc2 + a58e175 commit a46d634
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Runner/Baseline/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testTriggeredIssue(DeprecationTriggered|NoticeTriggered|PhpDepre
return;
}

if ($this->restrict($event) && !(new SourceFilter)->includes($this->source, $event->file())) {
if ($this->restrict($event) && !SourceFilter::instance()->includes($event->file())) {
return;
}

Expand Down
8 changes: 3 additions & 5 deletions src/Runner/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ final class ErrorHandler
private bool $enabled = false;
private ?int $originalErrorReportingLevel = null;
private readonly Source $source;
private readonly SourceFilter $sourceFilter;

/**
* @var ?array{functions: list<non-empty-string>, methods: list<array{className: class-string, methodName: non-empty-string}>}
Expand All @@ -72,8 +71,7 @@ public static function instance(): self

private function __construct(Source $source)
{
$this->source = $source;
$this->sourceFilter = new SourceFilter;
$this->source = $source;
}

/**
Expand Down Expand Up @@ -276,14 +274,14 @@ private function trigger(TestMethod $test, bool $filterTrigger): IssueTrigger
return IssueTrigger::test();
}

if ($this->sourceFilter->includes($this->source, $trace[0]['file'])) {
if (SourceFilter::instance()->includes($trace[0]['file'])) {
$triggeredInFirstPartyCode = true;
}
}

if (isset($trace[1]['file']) &&
($trace[1]['file'] === $test->file() ||
$this->sourceFilter->includes($this->source, $trace[1]['file']))) {
SourceFilter::instance()->includes($trace[1]['file']))) {
$triggerCalledFromFirstPartyCode = true;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Runner/IssueFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function shouldBeProcessed(DeprecationTriggered|ErrorTriggered|NoticeTrig
return false;
}

if ($this->source->restrictNotices() && !(new SourceFilter)->includes($this->source, $event->file())) {
if ($this->source->restrictNotices() && !SourceFilter::instance()->includes($event->file())) {
return false;
}
}
Expand All @@ -77,7 +77,7 @@ public function shouldBeProcessed(DeprecationTriggered|ErrorTriggered|NoticeTrig
return false;
}

if ($this->source->restrictNotices() && !(new SourceFilter)->includes($this->source, $event->file())) {
if ($this->source->restrictNotices() && !SourceFilter::instance()->includes($event->file())) {
return false;
}
}
Expand All @@ -87,7 +87,7 @@ public function shouldBeProcessed(DeprecationTriggered|ErrorTriggered|NoticeTrig
return false;
}

if ($this->source->restrictWarnings() && !(new SourceFilter)->includes($this->source, $event->file())) {
if ($this->source->restrictWarnings() && !SourceFilter::instance()->includes($event->file())) {
return false;
}
}
Expand All @@ -97,7 +97,7 @@ public function shouldBeProcessed(DeprecationTriggered|ErrorTriggered|NoticeTrig
return false;
}

if ($this->source->restrictWarnings() && !(new SourceFilter)->includes($this->source, $event->file())) {
if ($this->source->restrictWarnings() && !SourceFilter::instance()->includes($event->file())) {
return false;
}
}
Expand Down
34 changes: 30 additions & 4 deletions src/TextUI/Configuration/SourceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,38 @@
*
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final readonly class SourceFilter
final class SourceFilter
{
public function includes(Source $source, string $path): bool
private static ?self $instance = null;

/**
* @psalm-var array<non-empty-string, true>
*/
private readonly array $map;

public static function instance(): self
{
if (self::$instance === null) {
self::$instance = new self(
(new SourceMapper)->map(
Registry::get()->source(),
),
);
}

return self::$instance;
}

/**
* @psalm-param array<non-empty-string, true> $map
*/
public function __construct(array $map)
{
$files = (new SourceMapper)->map($source);
$this->map = $map;
}

return isset($files[$path]);
public function includes(string $path): bool
{
return isset($this->map[$path]);
}
}
8 changes: 4 additions & 4 deletions src/TextUI/Output/Default/ProgressPrinter/ProgressPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function testTriggeredNotice(NoticeTriggered $event): void
}

if ($this->source->restrictNotices() &&
!(new SourceFilter)->includes($this->source, $event->file())) {
!SourceFilter::instance()->includes($event->file())) {
return;
}

Expand All @@ -124,7 +124,7 @@ public function testTriggeredPhpNotice(PhpNoticeTriggered $event): void
}

if ($this->source->restrictNotices() &&
!(new SourceFilter)->includes($this->source, $event->file())) {
!SourceFilter::instance()->includes($event->file())) {
return;
}

Expand Down Expand Up @@ -204,7 +204,7 @@ public function testTriggeredWarning(WarningTriggered $event): void
}

if ($this->source->restrictWarnings() &&
!(new SourceFilter)->includes($this->source, $event->file())) {
!SourceFilter::instance()->includes($event->file())) {
return;
}

Expand All @@ -222,7 +222,7 @@ public function testTriggeredPhpWarning(PhpWarningTriggered $event): void
}

if ($this->source->restrictWarnings() &&
!(new SourceFilter)->includes($this->source, $event->file())) {
!SourceFilter::instance()->includes($event->file())) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/TextUI/SourceFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,6 @@ public static function provider(): array
#[DataProvider('provider')]
public function testDeterminesWhetherFileIsIncluded(bool $expected, string $file, Source $source): void
{
$this->assertSame($expected, (new SourceFilter)->includes($source, $file));
$this->assertSame($expected, (new SourceFilter((new SourceMapper)->map($source)))->includes($file));
}
}

0 comments on commit a46d634

Please sign in to comment.