-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a90fee4
commit 0ec7667
Showing
2 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace yii1tech\error\handler; | ||
|
||
use CComponent; | ||
|
||
/** | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
class ErrorTraceFilter extends CComponent | ||
{ | ||
/** | ||
* @var int maximum number of trace source code lines to be displayed. Defaults to 10. | ||
*/ | ||
public $maxTraceSourceLines = 10; | ||
|
||
public function filter(array $trace): array | ||
{ | ||
$trace = array_slice($trace, 0, $this->maxTraceSourceLines); | ||
|
||
$result = []; | ||
foreach ($trace as $entry) { | ||
if (array_key_exists('args', $entry)) { | ||
$entry['args'] = $this->simplifyArguments($entry['args']); | ||
} | ||
|
||
$result[] = $entry; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Converts arguments array to their simplified representation. | ||
* | ||
* @param array $args arguments array to be converted. | ||
* @return string string representation of the arguments array. | ||
*/ | ||
private function simplifyArguments(array $args) : string | ||
{ | ||
$count = 0; | ||
|
||
$isAssoc = $args !== array_values($args); | ||
|
||
foreach ($args as $key => $value) { | ||
$count++; | ||
|
||
if ($count >= 5) { | ||
if ($count > 5) { | ||
unset($args[$key]); | ||
} else { | ||
$args[$key] = '...'; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
$args[$key] = $this->simplifyArgument($value); | ||
|
||
if (is_string($key)) { | ||
$args[$key] = "'" . $key . "' => " . $args[$key]; | ||
} elseif ($isAssoc) { | ||
$args[$key] = $key.' => '.$args[$key]; | ||
} | ||
} | ||
|
||
return implode(', ', $args); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
private function simplifyArgument($value) | ||
{ | ||
if (is_object($value)) { | ||
return get_class($value); | ||
} elseif (is_bool($value)) { | ||
return $value ? 'true' : 'false'; | ||
} elseif (is_string($value)) { | ||
if (strlen($value) > 64) { | ||
return "'" . substr($value, 0, 64) . "...'"; | ||
} else { | ||
return "'" . $value . "'"; | ||
} | ||
} elseif (is_array($value)) { | ||
return '[' . $this->simplifyArguments($value) . ']'; | ||
} elseif ($value === null) { | ||
return 'null'; | ||
} elseif (is_resource($value)) { | ||
return 'resource'; | ||
} | ||
|
||
return $value; | ||
} | ||
} |