Skip to content

Commit

Permalink
Refactor BacktraceIgnoreMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jan 19, 2025
1 parent b326385 commit f8571a9
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 112 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"yiisoft/files": "^2.0",
"yiisoft/profiler": "^3.0",
"yiisoft/proxy": "^1.0.1",
"yiisoft/strings": "^2.2",
"yiisoft/strings": "^2.5",
"yiisoft/var-dumper": "^1.7"
},
"require-dev": {
Expand Down
8 changes: 4 additions & 4 deletions src/Collector/Stream/FilesystemStreamProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Yiisoft\Yii\Debug\Collector\Stream;

use Yiisoft\Strings\CombinedRegexp;
use Yiisoft\Yii\Debug\Helper\BacktraceIgnoreMatcher;
use Yiisoft\Yii\Debug\Helper\BacktraceMatcher;
use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapper;
use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapperInterface;

Expand Down Expand Up @@ -85,7 +85,7 @@ public static function register(): void
/**
* It's important to trigger autoloader before unregistering the file stream handler
*/
class_exists(BacktraceIgnoreMatcher::class);
class_exists(BacktraceMatcher::class);
class_exists(StreamWrapper::class);
class_exists(CombinedRegexp::class);
stream_wrapper_unregister('file');
Expand All @@ -105,8 +105,8 @@ public static function unregister(): void
private function isIgnored(): bool
{
$backtrace = debug_backtrace();
return BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, self::$ignoredClasses)
|| BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, self::$ignoredPathPatterns);
return BacktraceMatcher::byClass($backtrace[3], self::$ignoredClasses)
|| BacktraceMatcher::byFile($backtrace[3], self::$ignoredPathPatterns);
}

public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
Expand Down
11 changes: 6 additions & 5 deletions src/Collector/Stream/HttpStreamProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Yiisoft\Yii\Debug\Collector\Stream;

use Yiisoft\Strings\CombinedRegexp;
use Yiisoft\Yii\Debug\Helper\BacktraceIgnoreMatcher;
use Yiisoft\Strings\StringHelper;
use Yiisoft\Yii\Debug\Helper\BacktraceMatcher;
use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapper;
use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapperInterface;

Expand Down Expand Up @@ -94,7 +95,7 @@ public static function register(): void
/**
* It's important to trigger autoloader before unregistering the file stream handler
*/
class_exists(BacktraceIgnoreMatcher::class);
class_exists(BacktraceMatcher::class);
class_exists(StreamWrapper::class);
class_exists(CombinedRegexp::class);
stream_wrapper_unregister('http');
Expand Down Expand Up @@ -303,12 +304,12 @@ public function url_stat(string $path, int $flags): array|false

private function isIgnored(string $url): bool
{
if (BacktraceIgnoreMatcher::doesStringMatchPattern($url, self::$ignoredUrls)) {
if (StringHelper::matchAnyRegex($url, self::$ignoredUrls)) {
return true;
}

$backtrace = debug_backtrace();
return BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, self::$ignoredClasses)
|| BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, self::$ignoredPathPatterns);
return BacktraceMatcher::byClass($backtrace[3], self::$ignoredClasses)
|| BacktraceMatcher::byFile($backtrace[3], self::$ignoredPathPatterns);
}
}
3 changes: 0 additions & 3 deletions src/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
use Yiisoft\Yii\Debug\StartupPolicy\Debugger\DebuggerStartupPolicyInterface;
use Yiisoft\Yii\Debug\Storage\StorageInterface;

/**
* @psalm-type BacktraceType = list<array{file?:string,line?:int,function?:string,class?:class-string,object?:object,type?:string,args?:array}>
*/
final class Debugger
{
/**
Expand Down
53 changes: 0 additions & 53 deletions src/Helper/BacktraceIgnoreMatcher.php

This file was deleted.

47 changes: 47 additions & 0 deletions src/Helper/BacktraceMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Helper;

use Yiisoft\Strings\StringHelper;

use function in_array;

/**
* `BacktraceMatcher` provides methods to match backtrace items returned by the PHP function `debug_backtrace()`.
*
* @see https://www.php.net/manual/function.debug-backtrace.php
*
* @psalm-type TBacktraceItem = array{
* file?: string,
* line?: int,
* function?: string,
* class?: class-string,
* object?: object,
* type?: string,
* args?:array,
* }
*/
final class BacktraceMatcher
{
/**
* @param string[] $patterns
* @psalm-param TBacktraceItem $backtraceItem
*/
public static function byFile(array $backtraceItem, array $patterns): bool
{
$path = $backtraceItem['file'] ?? null;
return $path !== null && StringHelper::matchAnyRegex($path, $patterns);
}

/**
* @param string[] $classes
* @psalm-param TBacktraceItem $backtraceItem
*/
public static function byClass(array $backtraceItem, array $classes): bool
{
$class = $backtraceItem['class'] ?? null;
return $class !== null && in_array($class, $classes, true);
}
}
59 changes: 13 additions & 46 deletions tests/Unit/Helper/BacktraceIgnoreMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
use Yiisoft\Yii\Debug\Helper\BacktraceIgnoreMatcher;
use Yiisoft\Yii\Debug\Helper\BacktraceMatcher;

final class BacktraceIgnoreMatcherTest extends TestCase
{
public function testClassIgnorance(): void
{
$backtrace = debug_backtrace();

$this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, [self::class]));
$this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, [stdClass::class]));
$this->assertFalse(BacktraceMatcher::byClass($backtrace[3], [self::class]));
$this->assertFalse(BacktraceMatcher::byClass($backtrace[3], [stdClass::class]));

$backtrace[3] = $backtrace[0];

$this->assertTrue(BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, [self::class]));
$this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, [stdClass::class]));
$this->assertTrue(BacktraceMatcher::byClass($backtrace[0], [self::class]));
$this->assertFalse(BacktraceMatcher::byClass($backtrace[0], [stdClass::class]));
}

public function testFileIgnorance(): void
Expand All @@ -30,53 +28,22 @@ public function testFileIgnorance(): void
$reflection = new ReflectionClass(TestCase::class);
$file = $reflection->getFileName();

$this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [preg_quote($file)]));
$this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [preg_quote(__FILE__)]));

$backtrace[2] = $backtrace[0];
$this->assertFalse(BacktraceMatcher::byFile($backtrace[2], [preg_quote($file)]));
$this->assertFalse(BacktraceMatcher::byFile($backtrace[2], [preg_quote(__FILE__)]));

$this->assertTrue(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [preg_quote($file)]));
$this->assertTrue(BacktraceMatcher::byFile($backtrace[0], [preg_quote($file)]));
$this->assertTrue(
BacktraceIgnoreMatcher::isIgnoredByFile(
$backtrace,
BacktraceMatcher::byFile(
$backtrace[0],
[preg_quote(dirname($file) . DIRECTORY_SEPARATOR) . '*']
)
);
$this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [preg_quote(__FILE__)]));
}

public function testStringMatches(): void
{
$this->assertTrue(
BacktraceIgnoreMatcher::doesStringMatchPattern(
'dev/123/456',
['dev/123/456']
)
);
$this->assertTrue(
BacktraceIgnoreMatcher::doesStringMatchPattern(
'dev/123/456',
['456']
)
);
$this->assertTrue(
BacktraceIgnoreMatcher::doesStringMatchPattern(
'dev/123/456',
['dev/.*/456']
)
);
$this->assertTrue(
BacktraceIgnoreMatcher::doesStringMatchPattern(
'dev/123/456',
['dev*/456', 'dev/123/*']
)
);
$this->assertFalse(BacktraceMatcher::byFile($backtrace[0], [preg_quote(__FILE__)]));
}

public function testEmptyBacktrace(): void
{
$this->assertFalse(BacktraceIgnoreMatcher::doesStringMatchPattern('dev/123/456', []));
$this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile([], ['dev/123/456']));
$this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByClass([], ['dev/123/456']));
$this->assertFalse(BacktraceMatcher::byFile([], ['dev/123/456']));
$this->assertFalse(BacktraceMatcher::byClass([], ['dev/123/456']));
}
}

0 comments on commit f8571a9

Please sign in to comment.