From 1c2b14026b7611ad0570f049eb27bcefc6cb446f Mon Sep 17 00:00:00 2001 From: brenno-duarte Date: Thu, 12 Sep 2024 09:38:44 -0300 Subject: [PATCH] Fixed PHP 8.4 implicit nullable --- CHANGELOG.md | 8 ++++ composer.json | 7 +--- functions.php | 42 +++++---------------- index-test.php | 7 ++-- src/Bench.php | 22 +++++------ src/ModernPHPException.php | 2 +- src/Resources/CliDump.php | 76 +++++++++++--------------------------- 7 files changed, 53 insertions(+), 111 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f7e1e8..bfede92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Released Notes +## v3.3.5 - (2024-09-12) + +### Fixed + +- Fixed PHP 8.4 implicit nullable + +-------------------------------------------------------------------------- + ## v3.3.4 - (2024-09-05) ### Fixed diff --git a/composer.json b/composer.json index abc4932..8628121 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "brenno-duarte/modern-php-exception", - "description": "PHP errors in a modern way", + "description": "Display PHP errors and exceptions in a modern and intuitive way", "license": "MIT", "keywords": [ "exception", @@ -29,10 +29,5 @@ "psr-4": { "Test\\": "tests/" } - }, - "config": { - "allow-plugins": { - "dealerdirect/phpcodesniffer-composer-installer": true - } } } diff --git a/functions.php b/functions.php index 06e1d59..9307f78 100644 --- a/functions.php +++ b/functions.php @@ -57,9 +57,8 @@ function var_dump_debug(...$values): void CliDump::set('string', ['0000FF', 'light_blue']); new CliDump($value); - if (!CliMessage::colorIsSupported() || !CliMessage::are256ColorsSupported()) { + if (!CliMessage::colorIsSupported() || !CliMessage::are256ColorsSupported()) CliDump::safe($value); - } } else { $dump = new BrowserDump(); echo $dump->dump($value); @@ -106,15 +105,11 @@ function closure_dump(\Closure $c): string $s .= getClass($p)->name . ' '; } - if ($p->isPassedByReference()) { - $s .= '&'; - } - + if ($p->isPassedByReference()) $s .= '&'; $s .= '$' . $p->name; - if ($p->isOptional()) { + if ($p->isOptional()) $s .= ' = ' . var_export($p->getDefaultValue(), TRUE); - } $params[] = $s; } @@ -140,16 +135,10 @@ function closure_dump(\Closure $c): string function getClass(\ReflectionParameter $parameter): ?\ReflectionClass { $type = $parameter->getType(); - - if (!$type || $type->isBuiltin()) { - return null; - } + if (!$type || $type->isBuiltin()) return null; // This line triggers autoloader! - if (!class_exists($type->getName())) { - return null; - } - + if (!class_exists($type->getName())) return null; return new \ReflectionClass($type->getName()); } } @@ -157,26 +146,15 @@ function getClass(\ReflectionParameter $parameter): ?\ReflectionClass if (!function_exists('isCli')) { function isCli(): bool { - if (defined('STDIN')) { - return true; - } - - if (php_sapi_name() === "cli") { - return true; - } - - if (PHP_SAPI === 'cli') { - return true; - } - - if (stristr(PHP_SAPI, 'cgi') and getenv('TERM')) { - return true; - } + if (defined('STDIN')) return true; + if (php_sapi_name() === "cli") return true; + if (PHP_SAPI === 'cli') return true; + if (stristr(PHP_SAPI, 'cgi') and getenv('TERM')) return true; if ( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and - count($_SERVER['argv']) > 0 + count ((array)$_SERVER["argv"]) ) { return true; } diff --git a/index-test.php b/index-test.php index d36b6d4..2cb5e2d 100644 --- a/index-test.php +++ b/index-test.php @@ -9,7 +9,7 @@ //$config = ""; $exc = new ModernPHPException($config); //$exc->enableOccurrences(); -$exc->ignoreErrors([E_USER_DEPRECATED, E_WARNING]); +//$exc->ignoreErrors([E_USER_DEPRECATED, E_WARNING]); $exc->start(); #http_response_code(404); @@ -17,8 +17,8 @@ //throw new Exception(""); -trigger_error("Test trigger", E_USER_DEPRECATED); -echo "After trigger function"; +/* trigger_error("Test trigger", E_USER_DEPRECATED); +echo "After trigger function"; */ //UserTest::staticCall(); @@ -37,7 +37,6 @@ //echo $e->getMessage(); } */ - /* $a = (new UserTest())->secondCall(); var_dump_debug($a, true); */ diff --git a/src/Bench.php b/src/Bench.php index 02555d2..f23f771 100644 --- a/src/Bench.php +++ b/src/Bench.php @@ -40,7 +40,6 @@ public function start(): void public function end(): void { if (!$this->hasStarted()) throw new LogicException("You must call start()"); - $this->end_time = microtime(true); $this->memory_usage = memory_get_usage(true); } @@ -49,12 +48,12 @@ public function end(): void * Returns the elapsed time, readable or not * * @param bool $raw - * @param string $format The format to display (printf format) + * @param null|string $format The format to display (printf format) * * @return mixed * @throws LogicException */ - public function getTime(bool $raw = false, string $format = null): mixed + public function getTime(bool $raw = false, ?string $format = null): mixed { if (!$this->hasStarted()) throw new LogicException("You must call start()"); if (!$this->hasEnded()) throw new LogicException("You must call end()"); @@ -66,12 +65,12 @@ public function getTime(bool $raw = false, string $format = null): mixed /** * Returns the memory usage at the end checkpoint * - * @param bool $readable Whether the result must be human readable - * @param string $format The format to display (printf format) + * @param bool $readable Whether the result must be human readable + * @param null|string $format The format to display (printf format) * * @return mixed */ - public function getMemoryUsage(bool $raw = false, string $format = null): mixed + public function getMemoryUsage(bool $raw = false, ?string $format = null): mixed { return $raw ? $this->memory_usage : self::readableSize($this->memory_usage, $format); } @@ -79,8 +78,8 @@ public function getMemoryUsage(bool $raw = false, string $format = null): mixed /** * Returns the memory peak, readable or not * - * @param bool $readable Whether the result must be human readable - * @param string $format The format to display (printf format) + * @param bool $readable Whether the result must be human readable + * @param null|string $format The format to display (printf format) * * @return mixed */ @@ -104,11 +103,9 @@ public function run(callable $callable): mixed { $arguments = func_get_args(); array_shift($arguments); - $this->start(); $result = call_user_func_array($callable, $arguments); $this->end(); - return $result; } @@ -116,7 +113,7 @@ public function run(callable $callable): mixed * Returns a human readable memory size * * @param int $size - * @param string $format The format to display (printf format) + * @param null|string $format The format to display (printf format) * @param int $round * * @return string @@ -139,7 +136,7 @@ public static function readableSize(int $size, ?string $format = null, int $roun * Returns a human readable elapsed time * * @param float $microtime - * @param string $format The format to display (printf format) + * @param null|string $format The format to display (printf format) * @param int $round * * @return string @@ -154,7 +151,6 @@ public static function readableElapsedTime(float $microtime, ?string $format = n } else { $unit = 'ms'; $time = round($microtime * 1000); - $format = preg_replace('/(%.[\d]+f)/', '%d', $format); } diff --git a/src/ModernPHPException.php b/src/ModernPHPException.php index b67e1ab..3fd27b2 100644 --- a/src/ModernPHPException.php +++ b/src/ModernPHPException.php @@ -9,7 +9,7 @@ class ModernPHPException { use HelpersTrait, HandlerAssetsTrait, RenderTrait; - public const VERSION = "3.3.4"; + public const VERSION = "3.3.5"; /** * @var Bench diff --git a/src/Resources/CliDump.php b/src/Resources/CliDump.php index 078bee4..b5ebd3f 100644 --- a/src/Resources/CliDump.php +++ b/src/Resources/CliDump.php @@ -40,7 +40,7 @@ class CliDump * @var bool */ private static bool $safe = false; - + /** * @var array */ @@ -200,17 +200,13 @@ public function aboveNestLevel(): bool */ private function isPosix(): bool { - if (self::$safe) { - return false; - } + if (self::$safe) return false; // disable posix errors about unknown resource types if (function_exists('posix_isatty')) { - set_error_handler(function () { - }); + set_error_handler(function () {}); $isPosix = posix_isatty(STDIN); restore_error_handler(); - return $isPosix; } @@ -225,13 +221,10 @@ private function isPosix(): bool * * @return string */ - private function format(string $string, string $format = null): string + private function format(string $string, ?string $format = null): string { // format only for POSIX - if (!$format || !$this->isPosix) { - return $string; - } - + if (!$format || !$this->isPosix) return $string; $formats = $format ? explode('|', $format) : []; $code = array_filter([ @@ -241,7 +234,6 @@ private function format(string $string, string $format = null): string ]); $code = implode(';', $code); - return "\033[{$code}m{$string}\033[0m"; } @@ -299,9 +291,8 @@ private function output(string $data) */ private function color($value, string $name): string { - if ($this->isCli) { + if ($this->isCli) return $this->format($value, $this->colors[$name][1]); - } if ($name == 'type') { return "colors[$name][0]}\">{$value}"; @@ -390,7 +381,7 @@ private function formatArray(array $array, bool $obj_call): string $this->indent += $this->pad_size; $break_line = $this->breakLine(); $indent = $this->indent($this->indent); - + foreach ($array as $key => $arr) { if (is_array($arr)) { $tmp .= "{$break_line}{$indent}{$this->arrayIndex((string)$key)} {$this->color('(size=' . count($arr) . ')', 'size')}"; @@ -405,9 +396,7 @@ private function formatArray(array $array, bool $obj_call): string if ($tmp != '') { $tmp .= $break_line; - if ($obj_call) { - $tmp .= $this->indent($this->indent); - } + if ($obj_call) $tmp .= $this->indent($this->indent); } return $tmp; @@ -425,11 +414,8 @@ private function refcount($object): string ob_start(); debug_zval_dump($object); - if (preg_match('/object\(.*?\)#(\d+)\s+\(/', ob_get_clean(), $match)) { - return $match[1]; - } - - return ''; + return (preg_match('/object\(.*?\)#(\d+)\s+\(/', ob_get_clean(), $match)) ? + $match[1] : ""; } /** @@ -441,9 +427,7 @@ private function refcount($object): string */ private function formatObject($object): string { - if ($this->aboveNestLevel()) { - return $this->color('...', 'recursion'); - } + if ($this->aboveNestLevel()) return $this->color('...', 'recursion'); $reflection = new \ReflectionObject($object); $class_name = $reflection->getName(); @@ -464,10 +448,7 @@ private function formatObject($object): string $inherited[$prop_name] = $tmp_class_name == $class_name ? null : $tmp_class_name; } - if (str_contains($comments, '@dumpignore-inheritance')) { - break; - } - + if (str_contains($comments, '@dumpignore-inheritance')) break; $reflection = $reflection->getParentClass(); } @@ -487,9 +468,8 @@ private function formatObject($object): string foreach ($properties as $name => $prop) { $prop_comment = $prop->getDocComment(); - if ($prop_comment && (str_contains($prop_comment, '@dumpignore'))) { + if ($prop_comment && (str_contains($prop_comment, '@dumpignore'))) continue; - } $from = ''; if (!$hide_in_class && isset($inherited[$name])) { @@ -499,27 +479,18 @@ private function formatObject($object): string } if ($prop->isPrivate()) { - if ($hide_private) { - continue; - } - + if ($hide_private) continue; $tmp .= "{$line_break}{$indent}{$private_color}{$string_pad_2} {$property_color} "; } elseif ($prop->isProtected()) { - if ($hide_protected) { - continue; - } - + if ($hide_protected) continue; $tmp .= "{$line_break}{$indent}{$protected_color} {$property_color} "; } elseif ($prop->isPublic()) { - if ($hide_public) { - continue; - } - + if ($hide_public) continue; $tmp .= "{$line_break}{$indent}{$public_color}{$string_pad_3} {$property_color} "; } $prop->setAccessible(true); - + if (version_compare(PHP_VERSION, '7.4.0') >= 0) { $value = $prop->isInitialized($object) ? $this->getValue($prop, $object, $class_name) : $this->type( 'uninitialized' @@ -531,10 +502,7 @@ private function formatObject($object): string $tmp .= "{$from} {$this->color("'{$prop->getName()}'", 'property_name')} {$arrow_color} {$value}"; } - if ($tmp != '') { - $tmp .= $this->breakLine(); - } - + if ($tmp != '') $tmp .= $this->breakLine(); $this->indent -= $this->pad_size; $tmp .= ($tmp != '') ? $this->indent($this->indent) : ''; @@ -561,11 +529,9 @@ private function getValue(\ReflectionProperty $property, $object, string $class_ $value = $property->getValue($object); # Prevent infinite loop caused by nested object property. e.g. when an object property is pointing to the same # object. - if (is_object($value) && $value instanceof $object && $value == $object) { - return "{$this->type($class_name)} {$this->color('::self', 'keyword')}"; - } - - return $this->evaluate([$value], true, true); + return (is_object($value) && $value instanceof $object && $value == $object) ? + "{$this->type($class_name)} {$this->color('::self', 'keyword')}" : + $this->evaluate([$value], true, true); } /**