Repository navigation
Expand file tree
/
Copy pathStackTraceFilter.php
More file actions
113 lines (90 loc) · 2.92 KB
/
Copy pathStackTraceFilter.php
File metadata and controls
113 lines (90 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace Codeception\Util;
use Throwable;
class StackTraceFilter
{
protected static array $filteredClassesPattern = [
'Symfony\Component\Console',
'Codeception\Command\\',
'Codeception\TestCase\\',
];
public static function getFilteredStackTrace(Throwable $e, bool $asString = true, bool $filter = true): array|string
{
$stackTrace = $asString ? '' : [];
$trace = $e->getPrevious() ? $e->getPrevious()->getTrace() : $e->getTrace();
$eFile = $e->getFile();
$eLine = $e->getLine();
if (!self::frameExists($trace, $eFile, $eLine)) {
array_unshift(
$trace,
['file' => $eFile, 'line' => $eLine]
);
}
foreach ($trace as $step) {
if (self::classIsFiltered($step) and $filter) {
continue;
}
if (self::fileIsFiltered($step) and $filter) {
continue;
}
if (!$asString) {
$stackTrace[] = $step;
continue;
}
if (!isset($step['file'])) {
continue;
}
$stackTrace .= $step['file'] . ':' . $step['line'] . "\n";
}
return $stackTrace;
}
protected static function classIsFiltered(array $step): bool
{
if (!isset($step['class'])) {
return false;
}
$className = $step['class'];
foreach (self::$filteredClassesPattern as $filteredClassName) {
if (str_starts_with($className, $filteredClassName)) {
return true;
}
}
return false;
}
protected static function fileIsFiltered(array $step): bool
{
if (!isset($step['file'])) {
return false;
}
if (str_contains($step['file'], 'codecept.phar/')) {
return true;
}
if (str_contains($step['file'], 'vendor' . DIRECTORY_SEPARATOR . 'phpunit')) {
return true;
}
if (str_contains($step['file'], 'vendor' . DIRECTORY_SEPARATOR . 'codeception')) {
return true;
}
$modulePath = 'src' . DIRECTORY_SEPARATOR . 'Codeception' . DIRECTORY_SEPARATOR . 'Module';
if (str_contains($step['file'], $modulePath)) {
return false; // don`t filter modules
}
if (str_contains($step['file'], 'src' . DIRECTORY_SEPARATOR . 'Codeception' . DIRECTORY_SEPARATOR)) {
return true;
}
return false;
}
private static function frameExists(array $trace, string $file, int $line): bool
{
foreach ($trace as $frame) {
if (
isset($frame['file']) && $frame['file'] == $file &&
isset($frame['line']) && $frame['line'] == $line
) {
return true;
}
}
return false;
}
}