Skip to content

Commit

Permalink
small refactor to reduce complexity #hacktoberfest
Browse files Browse the repository at this point in the history
  • Loading branch information
codisart authored and sebastianbergmann committed Oct 28, 2019
1 parent 6194d0d commit 5f61126
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/Util/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ final class Filter
*/
public static function getFilteredStacktrace(\Throwable $t): string
{
$prefix = false;
$script = \realpath($GLOBALS['_SERVER']['SCRIPT_NAME']);

if (\defined('__PHPUNIT_PHAR_ROOT__')) {
$prefix = __PHPUNIT_PHAR_ROOT__;
}

$filteredStacktrace = '';

if ($t instanceof SyntheticError) {
Expand Down Expand Up @@ -56,14 +49,11 @@ public static function getFilteredStacktrace(\Throwable $t): string
);
}

$prefix = \defined('__PHPUNIT_PHAR_ROOT__') ? __PHPUNIT_PHAR_ROOT__ : false;
$blacklist = new Blacklist;

foreach ($eTrace as $frame) {
if (isset($frame['file']) && \is_file($frame['file']) &&
(empty($GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST']) || !\in_array($frame['file'], $GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST'])) &&
!$blacklist->isBlacklisted($frame['file']) &&
($prefix === false || \strpos($frame['file'], $prefix) !== 0) &&
$frame['file'] !== $script) {
if (self::shouldPrintFrame($frame, $prefix, $blacklist)) {
$filteredStacktrace .= \sprintf(
"%s:%s\n",
$frame['file'],
Expand All @@ -75,6 +65,31 @@ public static function getFilteredStacktrace(\Throwable $t): string
return $filteredStacktrace;
}

private static function shouldPrintFrame($frame, $prefix, Blacklist $blacklist): bool
{
if (!isset($frame['file'])) {
return false;
}

$file = $frame['file'];
$fileIsNotPrefixed = $prefix === false || \strpos($file, $prefix) !== 0;
$script = \realpath($GLOBALS['_SERVER']['SCRIPT_NAME']);

return \is_file($file)
&& self::fileIsBlacklisted($file, $blacklist)
&& $fileIsNotPrefixed
&& $file !== $script;
}

private static function fileIsBlacklisted($file, Blacklist $blacklist)
{
return (
empty($GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST'])
|| !\in_array($file, $GLOBALS['__PHPUNIT_ISOLATION_BLACKLIST'])
)
&& !$blacklist->isBlacklisted($file);
}

private static function frameExists(array $trace, string $file, int $line): bool
{
foreach ($trace as $frame) {
Expand Down

0 comments on commit 5f61126

Please sign in to comment.