From 1f3ec039671d105ab17dd98d756340ee34f00899 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Tue, 22 Mar 2022 21:12:07 +0200 Subject: [PATCH 1/2] Update Dumper code with php 8.1 features --- src/Dumper/src/Dumper.php | 111 +++++++------------ src/Dumper/src/Renderer/AbstractRenderer.php | 30 +---- src/Dumper/src/Renderer/ConsoleRenderer.php | 46 ++------ src/Dumper/src/Renderer/HtmlRenderer.php | 66 +++-------- src/Dumper/src/Renderer/PlainRenderer.php | 24 +--- src/Dumper/src/RendererInterface.php | 13 +-- src/Dumper/src/System.php | 30 ++--- src/Dumper/src/helpers.php | 16 +-- 8 files changed, 92 insertions(+), 244 deletions(-) diff --git a/src/Dumper/src/Dumper.php b/src/Dumper/src/Dumper.php index afbc5a261..37793ba7b 100644 --- a/src/Dumper/src/Dumper.php +++ b/src/Dumper/src/Dumper.php @@ -1,12 +1,5 @@ HtmlRenderer::class, self::OUTPUT_CLI => PlainRenderer::class, self::OUTPUT_CLI_COLORS => ConsoleRenderer::class, @@ -73,12 +65,11 @@ public function setMaxLevel(int $maxLevel): void /** * Dump given value into target output. * - * @param mixed $value - * @param int $target Possible options: OUTPUT, RETURN, ERROR_LOG, LOGGER. - * @return string + * @param int $target Possible options: OUTPUT, RETURN, ERROR_LOG, LOGGER. + * * @throws DumperException */ - public function dump($value, int $target = self::OUTPUT): ?string + public function dump(mixed $value, int $target = self::OUTPUT): ?string { $r = $this->getRenderer($target); $dump = $r->wrapContent($this->renderValue($r, $value)); @@ -115,7 +106,7 @@ public function dump($value, int $target = self::OUTPUT): ?string public function setRenderer(int $target, RendererInterface $renderer): Dumper { if (!isset($this->targets[$target])) { - throw new DumperException(sprintf('Undefined dump target %d', $target)); + throw new DumperException(\sprintf('Undefined dump target %d', $target)); } $this->targets[$target] = $renderer; @@ -140,10 +131,10 @@ private function getRenderer(int $target): RendererInterface } if (!isset($this->targets[$target])) { - throw new DumperException(sprintf('Undefined dump target %d', $target)); + throw new DumperException(\sprintf('Undefined dump target %d', $target)); } - if (is_string($this->targets[$target])) { + if (\is_string($this->targets[$target])) { $this->targets[$target] = new $this->targets[$target](); } @@ -154,14 +145,13 @@ private function getRenderer(int $target): RendererInterface * Variable dumper. This is the oldest spiral function originally written in 2007. :). * * @param RendererInterface $r Render to style value content. - * @param mixed $value * @param string $name Variable name, internal. * @param int $level Dumping level, internal. * @param bool $hideHeader Hide array/object header, internal. */ private function renderValue( RendererInterface $r, - $value, + mixed $value, string $name = '', int $level = 0, bool $hideHeader = false @@ -177,33 +167,33 @@ private function renderValue( return $r->indent($level) . $r->apply('-too deep-', 'maxLevel') . "\n"; } - $type = strtolower(gettype($value)); + $type = \strtolower(gettype($value)); - if ($type == 'array') { + if ($type === 'array') { return $header . $this->renderArray($r, $value, $level, $hideHeader); } - if ($type == 'object') { + if ($type === 'object') { return $header . $this->renderObject($r, $value, $level, $hideHeader); } - if ($type == 'resource') { + if ($type === 'resource') { //No need to dump resource value - $element = get_resource_type($value) . ' resource '; + $element = \get_resource_type($value) . ' resource '; return $header . $r->apply($element, 'type', 'resource') . "\n"; } //Value length - $length = strlen((string)$value); + $length = \strlen((string)$value); //Including type size - $header .= $r->apply("{$type}({$length})", 'type', $type); + $header .= $r->apply(\sprintf('%s(%d)', $type, $length), 'type', $type); $element = null; switch ($type) { case 'string': - $element = $r->escapeStrings() ? htmlspecialchars($value) : $value; + $element = $r->escapeStrings() ? \htmlspecialchars($value) : $value; break; case 'boolean': @@ -213,7 +203,7 @@ private function renderValue( default: if ($value !== null) { //Not showing null value, type is enough - $element = var_export($value, true); + $element = \var_export($value, true); } } @@ -224,25 +214,25 @@ private function renderValue( private function renderArray(RendererInterface $r, array $array, int $level, bool $hideHeader = false): string { if (!$hideHeader) { - $count = count($array); + $count = \count($array); //Array size and scope - $output = $r->apply("array({$count})", 'type', 'array') . "\n"; + $output = $r->apply(\sprintf('array(%d)', $count), 'type', 'array') . "\n"; $output .= $r->indent($level) . $r->apply('[', 'syntax', '[') . "\n"; } else { $output = ''; } foreach ($array as $key => $value) { - if (!is_numeric($key)) { - if (is_string($key) && $r->escapeStrings()) { - $key = htmlspecialchars($key); + if (!\is_numeric($key)) { + if (\is_string($key) && $r->escapeStrings()) { + $key = \htmlspecialchars($key); } - $key = "'{$key}'"; + $key = \sprintf("'%s'", $key); } - $output .= $this->renderValue($r, $value, "[{$key}]", $level + 1); + $output .= $this->renderValue($r, $value, \sprintf('[%s]', $key), $level + 1); } if (!$hideHeader) { @@ -253,19 +243,15 @@ private function renderArray(RendererInterface $r, array $array, int $level, boo return $output; } - /** - * @param object $value - * - */ private function renderObject( RendererInterface $r, - $value, + object $value, int $level, bool $hideHeader = false, string $class = '' ): string { if (!$hideHeader) { - $type = ($class ?: get_class($value)) . ' object '; + $type = ($class ?: $value::class) . ' object '; $header = $r->apply($type, 'type', 'object') . "\n"; $header .= $r->indent($level) . $r->apply('(', 'syntax', '(') . "\n"; @@ -274,25 +260,25 @@ private function renderObject( } //Let's use method specifically created for dumping - if (method_exists($value, '__debugInfo') || $value instanceof \Closure) { + if (\method_exists($value, '__debugInfo') || $value instanceof \Closure) { if ($value instanceof \Closure) { $debugInfo = $this->describeClosure($value); } else { $debugInfo = $value->__debugInfo(); } - if (is_array($debugInfo)) { + if (\is_array($debugInfo)) { //Pretty view $debugInfo = (object)$debugInfo; } - if (is_object($debugInfo)) { + if (\is_object($debugInfo)) { //We are not including syntax elements here - return $this->renderObject($r, $debugInfo, $level, false, get_class($value)); + return $this->renderObject($r, $debugInfo, $level, false, $value::class); } return $header - . $this->renderValue($r, $debugInfo, '', $level + (is_scalar($value)), true) + . $this->renderValue($r, $debugInfo, '', $level + (\is_scalar($value)), true) . $r->indent($level) . $r->apply(')', 'syntax', ')') . "\n"; } @@ -307,20 +293,16 @@ private function renderObject( return $header . $output . $r->indent($level) . $r->apply(')', 'syntax', ')') . "\n"; } - /** - * @param object $value - * - */ - private function renderProperty(RendererInterface $r, $value, \ReflectionProperty $p, int $level): string + private function renderProperty(RendererInterface $r, object $value, \ReflectionProperty $p, int $level): string { if ($p->isStatic()) { return ''; } if ( - !($value instanceof \stdClass) - && is_string($p->getDocComment()) - && strpos($p->getDocComment(), '@internal') !== false + !($value instanceof \stdClass) && + \is_string($p->getDocComment()) && + str_contains($p->getDocComment(), '@internal') ) { // Memory loop while reading doc comment for stdClass variables? // Report a PHP bug about treating comment INSIDE property declaration as doc comment. @@ -330,9 +312,6 @@ private function renderProperty(RendererInterface $r, $value, \ReflectionPropert //Property access level $access = $this->getAccess($p); - //To read private and protected properties - $p->setAccessible(true); - if ($value instanceof \stdClass) { $name = $r->apply($p->getName(), 'dynamic'); } else { @@ -350,12 +329,12 @@ private function describeClosure(\Closure $closure): array { try { $r = new \ReflectionFunction($closure); - } catch (\ReflectionException $e) { + } catch (\ReflectionException) { return ['closure' => 'unable to resolve']; } return [ - 'name' => $r->getName() . " (lines {$r->getStartLine()}:{$r->getEndLine()})", + 'name' => $r->getName() . \sprintf(' (lines %s:%s)', $r->getStartLine(), $r->getEndLine()), 'file' => $r->getFileName(), 'this' => $r->getClosureThis(), ]; @@ -363,17 +342,13 @@ private function describeClosure(\Closure $closure): array /** * Property access level label. - * - * */ private function getAccess(\ReflectionProperty $p): string { - if ($p->isPrivate()) { - return 'private'; - } elseif ($p->isProtected()) { - return 'protected'; - } - - return 'public'; + return match (true) { + $p->isPrivate() => 'private', + $p->isProtected() => 'protected', + default => 'public' + }; } } diff --git a/src/Dumper/src/Renderer/AbstractRenderer.php b/src/Dumper/src/Renderer/AbstractRenderer.php index 8cd4fb807..1ca38b06f 100644 --- a/src/Dumper/src/Renderer/AbstractRenderer.php +++ b/src/Dumper/src/Renderer/AbstractRenderer.php @@ -1,12 +1,5 @@ body, $body); + return \sprintf($this->body, $body); } - /** - * @inheritdoc - */ public function indent(int $level): string { - if ($level == 0) { + if ($level === 0) { return ''; } - return $this->apply(str_repeat($this->indent, $level), 'indent'); + return $this->apply(\str_repeat($this->indent, $level), 'indent'); } - /** - * @inheritdoc - */ public function escapeStrings(): bool { return true; diff --git a/src/Dumper/src/Renderer/ConsoleRenderer.php b/src/Dumper/src/Renderer/ConsoleRenderer.php index 12c10164b..d59a0f3eb 100644 --- a/src/Dumper/src/Renderer/ConsoleRenderer.php +++ b/src/Dumper/src/Renderer/ConsoleRenderer.php @@ -1,12 +1,5 @@ Color::BOLD_WHITE, 'name' => Color::LIGHT_WHITE, 'dynamic' => Color::PURPLE, @@ -57,21 +46,15 @@ final class ConsoleRenderer extends AbstractRenderer 'access' => Color::GRAY, ]; - /** - * @inheritdoc - */ - public function apply($element, string $type, string $context = ''): string + public function apply(mixed $element, string $type, string $context = ''): string { if (!empty($style = $this->getStyle($type, $context))) { - return sprintf($this->element, $style, $element); + return \sprintf($this->element, $style, $element); } return $element; } - /** - * @inheritdoc - */ public function escapeStrings(): bool { return false; @@ -79,23 +62,14 @@ public function escapeStrings(): bool /** * Get valid style based on type and context/. - * - * */ private function getStyle(string $type, string $context): string { - if (isset($this->styles[$type][$context])) { - return $this->styles[$type][$context]; - } - - if (isset($this->styles[$type]['common'])) { - return $this->styles[$type]['common']; - } - - if (isset($this->styles[$type]) && is_string($this->styles[$type])) { - return $this->styles[$type]; - } - - return $this->styles['common']; + return match(true) { + isset($this->styles[$type][$context]) => $this->styles[$type][$context], + isset($this->styles[$type]['common']) => $this->styles[$type]['common'], + isset($this->styles[$type]) && \is_string($this->styles[$type]) => $this->styles[$type], + default => $this->styles['common'] + }; } } diff --git a/src/Dumper/src/Renderer/HtmlRenderer.php b/src/Dumper/src/Renderer/HtmlRenderer.php index 5288e36c2..e05e65c76 100644 --- a/src/Dumper/src/Renderer/HtmlRenderer.php +++ b/src/Dumper/src/Renderer/HtmlRenderer.php @@ -1,12 +1,5 @@ '
%s
', 'element' => '%s', 'indent' => '· ', @@ -60,7 +53,7 @@ final class HtmlRenderer implements RendererInterface /** * Inverted coloring schema. */ - public const INVERTED = [ + final public const INVERTED = [ 'body' => '
%s
', 'element' => '%s', 'indent' => '· ', @@ -97,52 +90,36 @@ final class HtmlRenderer implements RendererInterface ]; /** - * Set of styles associated with different dumping properties. - * - * @var array + * @param array $style Set of styles associated with different dumping properties. */ - protected $style = self::DEFAULT; - - public function __construct(array $style = self::DEFAULT) - { - $this->style = $style; + public function __construct( + protected array $style = self::DEFAULT + ) { } - /** - * @inheritdoc - */ - public function apply($element, string $type, string $context = ''): string + public function apply(mixed $element, string $type, string $context = ''): string { if (!empty($style = $this->getStyle($type, $context))) { - return sprintf($this->style['element'], $style, $element); + return \sprintf($this->style['element'], $style, $element); } return $element; } - /** - * @inheritdoc - */ public function wrapContent(string $body): string { - return sprintf($this->style['body'], $body); + return \sprintf($this->style['body'], $body); } - /** - * @inheritdoc - */ public function indent(int $level): string { - if ($level == 0) { + if ($level === 0) { return ''; } - return $this->apply(str_repeat($this->style['indent'], $level), 'indent'); + return $this->apply(\str_repeat($this->style['indent'], $level), 'indent'); } - /** - * @inheritdoc - */ public function escapeStrings(): bool { return true; @@ -150,23 +127,14 @@ public function escapeStrings(): bool /** * Get valid stype based on type and context/. - * - * */ private function getStyle(string $type, string $context): string { - if (isset($this->style[$type][$context])) { - return $this->style[$type][$context]; - } - - if (isset($this->style[$type]['common'])) { - return $this->style[$type]['common']; - } - - if (isset($this->style[$type]) && is_string($this->style[$type])) { - return $this->style[$type]; - } - - return $this->style['common']; + return match(true) { + isset($this->style[$type][$context]) => $this->style[$type][$context], + isset($this->style[$type]['common']) => $this->style[$type]['common'], + isset($this->style[$type]) && \is_string($this->style[$type]) => $this->style[$type], + default => $this->style['common'] + }; } } diff --git a/src/Dumper/src/Renderer/PlainRenderer.php b/src/Dumper/src/Renderer/PlainRenderer.php index 8beb5dfb7..578fe137f 100644 --- a/src/Dumper/src/Renderer/PlainRenderer.php +++ b/src/Dumper/src/Renderer/PlainRenderer.php @@ -1,12 +1,5 @@ escapeStrings = $escapeStrings; + public function __construct( + private readonly bool $escapeStrings = true + ) { } - /** - * @inheritdoc - */ - public function apply($element, string $type, string $context = ''): string + public function apply(mixed $element, string $type, string $context = ''): string { return (string)$element; } - /** - * @inheritdoc - */ public function escapeStrings(): bool { return $this->escapeStrings; diff --git a/src/Dumper/src/RendererInterface.php b/src/Dumper/src/RendererInterface.php index 911feb8e9..be2d55155 100644 --- a/src/Dumper/src/RendererInterface.php +++ b/src/Dumper/src/RendererInterface.php @@ -1,12 +1,5 @@ dump($value, $output); } $container = \Spiral\Core\ContainerScope::getContainer(); - if (is_null($container) || !$container->has(Dumper::class)) { + if (\is_null($container) || !$container->has(Dumper::class)) { $dumper = new Dumper(); return $dumper->dump($value, $output); From f75c0ba7f83032562d0f7f3232d5e636bdcf0847 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Fri, 25 Mar 2022 18:08:28 +0200 Subject: [PATCH 2/2] Add namespaces --- src/Dumper/src/Dumper.php | 6 +++--- src/Dumper/src/Exception/DumperException.php | 7 ------- src/Dumper/src/Renderer/ConsoleRenderer.php | 2 +- src/Dumper/src/Renderer/HtmlRenderer.php | 2 +- src/Dumper/src/System.php | 4 ++-- 5 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Dumper/src/Dumper.php b/src/Dumper/src/Dumper.php index 37793ba7b..751380f2f 100644 --- a/src/Dumper/src/Dumper.php +++ b/src/Dumper/src/Dumper.php @@ -59,7 +59,7 @@ public function __construct(LoggerInterface $logger = null) */ public function setMaxLevel(int $maxLevel): void { - $this->maxLevel = max($maxLevel, 1); + $this->maxLevel = \max($maxLevel, 1); } /** @@ -91,7 +91,7 @@ public function dump(mixed $value, int $target = self::OUTPUT): ?string break; case self::ERROR_LOG: - error_log($dump, 0); + \error_log($dump, 0); break; } @@ -167,7 +167,7 @@ private function renderValue( return $r->indent($level) . $r->apply('-too deep-', 'maxLevel') . "\n"; } - $type = \strtolower(gettype($value)); + $type = \strtolower(\gettype($value)); if ($type === 'array') { return $header . $this->renderArray($r, $value, $level, $hideHeader); diff --git a/src/Dumper/src/Exception/DumperException.php b/src/Dumper/src/Exception/DumperException.php index 73f5dc9be..1e2ef9fe0 100644 --- a/src/Dumper/src/Exception/DumperException.php +++ b/src/Dumper/src/Exception/DumperException.php @@ -1,12 +1,5 @@ styles[$type][$context]) => $this->styles[$type][$context], isset($this->styles[$type]['common']) => $this->styles[$type]['common'], isset($this->styles[$type]) && \is_string($this->styles[$type]) => $this->styles[$type], diff --git a/src/Dumper/src/Renderer/HtmlRenderer.php b/src/Dumper/src/Renderer/HtmlRenderer.php index e05e65c76..8e9ee167d 100644 --- a/src/Dumper/src/Renderer/HtmlRenderer.php +++ b/src/Dumper/src/Renderer/HtmlRenderer.php @@ -130,7 +130,7 @@ public function escapeStrings(): bool */ private function getStyle(string $type, string $context): string { - return match(true) { + return match (true) { isset($this->style[$type][$context]) => $this->style[$type][$context], isset($this->style[$type]['common']) => $this->style[$type]['common'], isset($this->style[$type]) && \is_string($this->style[$type]) => $this->style[$type], diff --git a/src/Dumper/src/System.php b/src/Dumper/src/System.php index 50ff1d042..7f815b811 100644 --- a/src/Dumper/src/System.php +++ b/src/Dumper/src/System.php @@ -40,13 +40,13 @@ public static function isColorsSupported(mixed $stream = STDOUT): bool if (\DIRECTORY_SEPARATOR === '\\') { return ( \function_exists('sapi_windows_vt100_support') - && @sapi_windows_vt100_support($stream) + && @\sapi_windows_vt100_support($stream) ) || \getenv('ANSICON') !== false || \getenv('ConEmuANSI') === 'ON' || \getenv('TERM') === 'xterm'; } - return @stream_isatty($stream); + return @\stream_isatty($stream); } catch (\Throwable) { return false; }