diff --git a/src/Errbit/Errbit.php b/src/Errbit/Errbit.php index 954078c..88804b5 100644 --- a/src/Errbit/Errbit.php +++ b/src/Errbit/Errbit.php @@ -166,10 +166,14 @@ protected function shouldNotify(\Throwable $exception, array $skippedExceptions) } } $userAgent = $_SERVER['HTTP_USER_AGENT'] ?? ''; - /** @var array $ignoreUserAgents */ + $userAgent = is_string($userAgent) ? $userAgent : ''; + /** @var list $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; } } diff --git a/src/Errbit/Errors/BaseError.php b/src/Errbit/Errors/BaseError.php index f120d5d..da91a5c 100644 --- a/src/Errbit/Errors/BaseError.php +++ b/src/Errbit/Errors/BaseError.php @@ -10,8 +10,12 @@ abstract class BaseError extends \Exception { protected string $errorFile = ''; + /** @var list> */ protected array $backtrace = []; + /** + * @param list> $backtrace + */ public function __construct( string $message = "", int $code = 0, @@ -32,6 +36,9 @@ public function getErrorFile(): string return $this->errorFile; } + /** + * @return list> + */ public function getBacktrace(): array { return $this->backtrace; diff --git a/src/Errbit/Errors/Error.php b/src/Errbit/Errors/Error.php index ccc5087..8920efe 100644 --- a/src/Errbit/Errors/Error.php +++ b/src/Errbit/Errors/Error.php @@ -4,6 +4,9 @@ class Error extends BaseError { + /** + * @param list> $backtrace + */ public function __construct( string $message, ?int $line = null, diff --git a/src/Errbit/Errors/Notice.php b/src/Errbit/Errors/Notice.php index cf31cff..9521f4c 100644 --- a/src/Errbit/Errors/Notice.php +++ b/src/Errbit/Errors/Notice.php @@ -4,6 +4,9 @@ class Notice extends BaseError { + /** + * @param list> $backtrace + */ public function __construct( string $message, ?int $line = null, diff --git a/src/Errbit/Errors/Warning.php b/src/Errbit/Errors/Warning.php index 32e5da1..dc4b22b 100644 --- a/src/Errbit/Errors/Warning.php +++ b/src/Errbit/Errors/Warning.php @@ -4,6 +4,9 @@ class Warning extends BaseError { + /** + * @param list> $backtrace + */ public function __construct( string $message, ?int $line = null, diff --git a/src/Errbit/Utils/Converter.php b/src/Errbit/Utils/Converter.php index 993ac9c..f3311b9 100644 --- a/src/Errbit/Utils/Converter.php +++ b/src/Errbit/Utils/Converter.php @@ -20,13 +20,16 @@ public static function createDefault(): Converter return new self(); } + /** + * @param list> $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), }; } } diff --git a/src/Errbit/Utils/XmlBuilder.php b/src/Errbit/Utils/XmlBuilder.php index c418ffc..a520fcf 100644 --- a/src/Errbit/Utils/XmlBuilder.php +++ b/src/Errbit/Utils/XmlBuilder.php @@ -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) { @@ -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 $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 @@ -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) { @@ -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. * @@ -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 : ''; } }