From 7557311a95fead8d1544f21a0b326f4b32939f9d Mon Sep 17 00:00:00 2001 From: Dmitriy Derepko Date: Sun, 8 Oct 2023 12:49:57 +0300 Subject: [PATCH] Update strings package (#226) * Update strings package * Fix check * Fix tests * Fix tests * Add example doc --- composer.json | 2 +- config/di.php | 8 +++++++- src/Collector/Stream/FilesystemStreamProxy.php | 2 ++ src/Collector/Stream/HttpStreamProxy.php | 2 ++ src/Helper/BacktraceIgnoreMatcher.php | 10 +++++----- tests/Unit/Collector/FilesystemStreamCollectorTest.php | 10 +++++----- tests/Unit/Collector/HttpStreamCollectorTest.php | 4 ++-- 7 files changed, 24 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 103a792a..e500baf8 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,7 @@ "yiisoft/json": "^1.0", "yiisoft/profiler": "^3.0", "yiisoft/proxy": "^1.0.1", - "yiisoft/strings": "^2.0", + "yiisoft/strings": "^2.2", "yiisoft/var-dumper": "^1.0" }, "require-dev": { diff --git a/config/di.php b/config/di.php index 67eee2a3..e8f4ff37 100644 --- a/config/di.php +++ b/config/di.php @@ -58,7 +58,13 @@ }, FilesystemStreamCollector::class => [ '__construct()' => [ - 'ignoredPathPatterns' => [], + 'ignoredPathPatterns' => [ + /** + * Examples: + * - templates/ + * - src/Directory/To/Ignore + */ + ], 'ignoredClasses' => [ ClosureExporter::class, UseStatementParser::class, diff --git a/src/Collector/Stream/FilesystemStreamProxy.php b/src/Collector/Stream/FilesystemStreamProxy.php index 46030354..f419f72a 100644 --- a/src/Collector/Stream/FilesystemStreamProxy.php +++ b/src/Collector/Stream/FilesystemStreamProxy.php @@ -4,6 +4,7 @@ namespace Yiisoft\Yii\Debug\Collector\Stream; +use Yiisoft\Strings\CombinedRegexp; use Yiisoft\Yii\Debug\Helper\BacktraceIgnoreMatcher; use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapper; use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapperInterface; @@ -67,6 +68,7 @@ public static function register(): void */ class_exists(BacktraceIgnoreMatcher::class); class_exists(StreamWrapper::class); + class_exists(CombinedRegexp::class); stream_wrapper_unregister('file'); stream_wrapper_register('file', self::class, STREAM_IS_URL); self::$registered = true; diff --git a/src/Collector/Stream/HttpStreamProxy.php b/src/Collector/Stream/HttpStreamProxy.php index 4a6050cc..39212a80 100644 --- a/src/Collector/Stream/HttpStreamProxy.php +++ b/src/Collector/Stream/HttpStreamProxy.php @@ -4,6 +4,7 @@ namespace Yiisoft\Yii\Debug\Collector\Stream; +use Yiisoft\Strings\CombinedRegexp; use Yiisoft\Yii\Debug\Helper\BacktraceIgnoreMatcher; use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapper; use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapperInterface; @@ -68,6 +69,7 @@ public static function register(): void */ class_exists(BacktraceIgnoreMatcher::class); class_exists(StreamWrapper::class); + class_exists(CombinedRegexp::class); stream_wrapper_unregister('http'); stream_wrapper_register('http', self::class, STREAM_IS_URL); stream_wrapper_unregister('https'); diff --git a/src/Helper/BacktraceIgnoreMatcher.php b/src/Helper/BacktraceIgnoreMatcher.php index 0130ae71..11f0f8c4 100644 --- a/src/Helper/BacktraceIgnoreMatcher.php +++ b/src/Helper/BacktraceIgnoreMatcher.php @@ -4,6 +4,8 @@ namespace Yiisoft\Yii\Debug\Helper; +use Yiisoft\Strings\CombinedRegexp; + /** * All backtrace parameters should contain at least 4 elements in the following order: * 0 – Called method @@ -30,11 +32,9 @@ public static function isIgnoredByClass(array $backtrace, array $classes): bool public static function doesStringMatchPattern(string $string, array $patterns): bool { - foreach ($patterns as $ignoredPathPattern) { - if (preg_match($ignoredPathPattern, $string) > 0) { - return true; - } + if (empty($patterns)) { + return false; } - return false; + return (new CombinedRegexp($patterns))->matches($string); } } diff --git a/tests/Unit/Collector/FilesystemStreamCollectorTest.php b/tests/Unit/Collector/FilesystemStreamCollectorTest.php index 94166a74..31079772 100644 --- a/tests/Unit/Collector/FilesystemStreamCollectorTest.php +++ b/tests/Unit/Collector/FilesystemStreamCollectorTest.php @@ -92,7 +92,7 @@ public function dataSkipCollectOnMatchIgnoreReferences(): iterable yield 'mkdir ignored by path' => [ $path, $mkdirBefore, - ['/' . basename(__FILE__, '.php') . '/'], + [basename(__FILE__, '.php')], [], $mkdirOperation, $mkdirAfter, @@ -139,7 +139,7 @@ public function dataSkipCollectOnMatchIgnoreReferences(): iterable yield 'rename ignored by path' => [ $path, $renameBefore, - ['/' . basename(__FILE__, '.php') . '/'], + [ basename(__FILE__, '.php') ], [], $renameOperation, $renameAfter, @@ -185,7 +185,7 @@ public function dataSkipCollectOnMatchIgnoreReferences(): iterable yield 'rmdir ignored by path' => [ $path, $rmdirBefore, - ['/' . basename(__FILE__, '.php') . '/'], + [basename(__FILE__, '.php')], [], $rmdirOperation, $rmdirAfter, @@ -232,7 +232,7 @@ public function dataSkipCollectOnMatchIgnoreReferences(): iterable yield 'unlink ignored by path' => [ $path, $unlinkBefore, - ['/' . basename(__FILE__, '.php') . '/'], + [basename(__FILE__, '.php')], [], $unlinkOperation, $unlinkAfter, @@ -291,7 +291,7 @@ public function dataSkipCollectOnMatchIgnoreReferences(): iterable yield 'file stream ignored by path' => [ $path, $fileStreamBefore, - ['/' . basename(__FILE__, '.php') . '/'], + [basename(__FILE__, '.php')], [], $fileStreamOperation, $fileStreamAfter, diff --git a/tests/Unit/Collector/HttpStreamCollectorTest.php b/tests/Unit/Collector/HttpStreamCollectorTest.php index 01700f82..e29323a8 100644 --- a/tests/Unit/Collector/HttpStreamCollectorTest.php +++ b/tests/Unit/Collector/HttpStreamCollectorTest.php @@ -108,7 +108,7 @@ function (string $url, array $collected) { yield 'file stream ignored by path' => [ $url, $httpStreamBefore, - ['/' . basename(__FILE__, '.php') . '/'], + [basename(__FILE__, '.php')], [], [], $httpStreamOperation, @@ -130,7 +130,7 @@ function (string $url, array $collected) { $httpStreamBefore, [], [], - ['/example/'], + ['example'], $httpStreamOperation, $httpStreamAfter, [],