Skip to content
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

Refactor BacktraceIgnoreMatcher #304

Merged
merged 5 commits into from
Jan 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
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
10 changes: 6 additions & 4 deletions src/Collector/Stream/FilesystemStreamProxy.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 @@ -85,9 +86,10 @@ 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);
class_exists(StringHelper::class);
stream_wrapper_unregister('file');
stream_wrapper_register('file', self::class, STREAM_IS_URL);
self::$registered = true;
Expand All @@ -105,8 +107,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::matchesClass($backtrace[3], self::$ignoredClasses)
|| BacktraceMatcher::matchesFile($backtrace[3], self::$ignoredPathPatterns);
}

public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
Expand Down
12 changes: 7 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,9 +95,10 @@ 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);
class_exists(StringHelper::class);
stream_wrapper_unregister('http');
stream_wrapper_register('http', self::class, STREAM_IS_URL);

Expand Down Expand Up @@ -303,12 +305,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::matchesClass($backtrace[3], self::$ignoredClasses)
|| BacktraceMatcher::matchesFile($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 matchesFile(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 matchesClass(array $backtraceItem, array $classes): bool
{
$class = $backtraceItem['class'] ?? null;
return $class !== null && in_array($class, $classes, true);
}
}
82 changes: 0 additions & 82 deletions tests/Unit/Helper/BacktraceIgnoreMatcherTest.php

This file was deleted.

49 changes: 49 additions & 0 deletions tests/Unit/Helper/BacktraceMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Tests\Unit\Helper;

use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
use Yiisoft\Yii\Debug\Helper\BacktraceMatcher;

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

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

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

public function testFileIgnorance(): void
{
$backtrace = debug_backtrace();
$reflection = new ReflectionClass(TestCase::class);
$file = $reflection->getFileName();

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

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

public function testEmptyBacktrace(): void
{
$this->assertFalse(BacktraceMatcher::matchesFile([], ['dev/123/456']));
$this->assertFalse(BacktraceMatcher::matchesClass([], ['dev/123/456']));
}
}
Loading