Skip to content
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
8 changes: 6 additions & 2 deletions src/Errbit/Errbit.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ protected function shouldNotify(\Throwable $exception, array $skippedExceptions)
}
}
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
/** @var array<int, string> $ignoreUserAgents */
$userAgent = is_string($userAgent) ? $userAgent : '';
/** @var list<string> $ignoreUserAgents */
$ignoreUserAgents = $this->config['ignore_user_agent'];
foreach ($ignoreUserAgents as $ua) {
if ($userAgent !== '' && str_contains($userAgent, $ua)) {
if ($userAgent === '' || !is_string($ua) || $ua === '') {
continue;
}
if (str_contains($userAgent, $ua)) {
return false;
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Errbit/Errors/BaseError.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
abstract class BaseError extends \Exception
{
protected string $errorFile = '';
/** @var list<array<string, mixed>> */
protected array $backtrace = [];

/**
* @param list<array<string, mixed>> $backtrace
*/
public function __construct(
string $message = "",
int $code = 0,
Expand All @@ -32,6 +36,9 @@ public function getErrorFile(): string
return $this->errorFile;
}

/**
* @return list<array<string, mixed>>
*/
public function getBacktrace(): array
{
return $this->backtrace;
Expand Down
3 changes: 3 additions & 0 deletions src/Errbit/Errors/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

class Error extends BaseError
{
/**
* @param list<array<string, mixed>> $backtrace
*/
public function __construct(
string $message,
?int $line = null,
Expand Down
3 changes: 3 additions & 0 deletions src/Errbit/Errors/Notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

class Notice extends BaseError
{
/**
* @param list<array<string, mixed>> $backtrace
*/
public function __construct(
string $message,
?int $line = null,
Expand Down
3 changes: 3 additions & 0 deletions src/Errbit/Errors/Warning.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

class Warning extends BaseError
{
/**
* @param list<array<string, mixed>> $backtrace
*/
public function __construct(
string $message,
?int $line = null,
Expand Down
7 changes: 5 additions & 2 deletions src/Errbit/Utils/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ public static function createDefault(): Converter
return new self();
}

/**
* @param list<array<string, mixed>> $backtrace
*/
public function convert(int $code, string $message, ?\Throwable $previous = null, string $file ='', ?int $line = null, array $backtrace = []): \Throwable
{
return match ($code) {
E_NOTICE, E_USER_NOTICE => new Notice($message, $line, $previous, $file, $backtrace),
E_WARNING, E_USER_WARNING => new Warning($message, $line, $previous, $file, $backtrace),
E_RECOVERABLE_ERROR, E_ERROR, E_CORE_ERROR => new Fatal($message, $line, $previous, $file),
default => new Error($message, $line, $previous, $file, $backtrace),
E_RECOVERABLE_ERROR, E_ERROR, E_CORE_ERROR => new Fatal($message, $line ?? 0, $previous, $file),
default => new Error($message, $line, $previous, $file, $backtrace),
};
}
}
62 changes: 19 additions & 43 deletions src/Errbit/Utils/XmlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class XmlBuilder
/**
* Instantiate a new XmlBuilder.
*
* @param SimpleXMLElement $xml the parent node (only used internally)
* @param SimpleXMLElement|null $xml the parent node (only used internally)
*/
public function __construct(?\SimpleXMLElement $xml = null)
{
Expand All @@ -41,8 +41,8 @@ public function __construct(?\SimpleXMLElement $xml = null)
/**
* Insert a tag into the XML.
*
* @param string $name the name of the tag, required.
* @param mixed $value the text value of the element, optional
* @param string $name the name of the tag, required.
* @param mixed $value the text value of the element, optional
* @param array<string, mixed> $attributes an array of attributes for the tag, optional
* @param callable|null $callback a callback to receive an XmlBuilder for the new tag, optional
* @param bool $getLastChild whether to get the last child element
Expand All @@ -53,10 +53,16 @@ public function tag(string $name, mixed $value = '', array $attributes = [], ?ca
{
$idx = is_countable($this->_xml->$name) ? count($this->_xml->$name) : 0;

$this->_xml->{$name}[$idx] = $this->normalizeValue($value);
if (is_object($value)) {
$value = "[" . $value::class . "]";
} else {
$value = (string) $value;
}

$this->_xml->{$name}[$idx] = $value;

foreach ($attributes as $attr => $v) {
$this->_xml->{$name}[$idx][$attr] = $this->normalizeValue($v);
$this->_xml->{$name}[$idx][(string) $attr] = (string) $v;
}
$node = new self($this->_xml->$name);
if ($getLastChild) {
Expand All @@ -79,50 +85,18 @@ public function tag(string $name, mixed $value = '', array $attributes = [], ?ca
/**
* Add an attribute to the current element.
*
* @param String $name the name of the attribute
* @param String $value the value of the attribute
* @param string $name the name of the attribute
* @param string $value the value of the attribute
*
* @return static the current builder
*/
public function attribute($name, $value): static
public function attribute(string $name, string $value): static
{
$this->_xml[$name] = $this->normalizeValue($value);
$this->_xml[$name] = $value;

return $this;
}

/**
* Cast any scalar or object value into a string for XML nodes.
*/
private function normalizeValue(mixed $value): string
{
if ($value instanceof \Stringable) {
return (string) $value;
}

if (is_object($value)) {
return sprintf('[%s]', $value::class);
}

if (is_bool($value)) {
return $value ? 'true' : 'false';
}

if (null === $value) {
return '';
}

if (is_resource($value)) {
return sprintf('[resource:%s]', get_resource_type($value));
}

if (is_array($value)) {
return '[array]';
}

return (string) $value;
}

/**
* Return this XmlBuilder as a string of XML.
*
Expand All @@ -141,8 +115,10 @@ public function asXml(): string
*
* @return string escaped string
*/
public static function utf8ForXML($string)
public static function utf8ForXML(string $string): string
{
return preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);
$filtered = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);

return is_string($filtered) ? $filtered : '';
}
}
Loading