From 4d3b607b19fa27e8b065b8302f2260d1c7c3fa63 Mon Sep 17 00:00:00 2001 From: ghostff Date: Mon, 1 Jan 2018 19:37:31 -0600 Subject: [PATCH] HTML characters escape --- README.md | 2 ++ src/Dump.php | 43 +++++++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 1e300e7..1077b86 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Pretty Data Dump A pretty version of php [var_dump](http://php.net/manual/en/function.var-dump.php). This class displays structured information about one or more expressions that includes its type and value. +_Check out [Dump5](https://github.com/Ghostff/Dump5) for PHP 5+_ + # Installation You can download the Latest [release version ](https://github.com/Ghostff/pretty_data_dump/releases/) as a standalone, alternatively you can use [Composer](https://getcomposer.org/) ```json diff --git a/src/Dump.php b/src/Dump.php index e5d5800..9bd8a73 100644 --- a/src/Dump.php +++ b/src/Dump.php @@ -285,7 +285,7 @@ private function output(string $data): void break; } } - $file = $bt['file'] . '(line:' . $bt['line'] . ')'; + $file = "{$bt['file']}(line:{$bt['line']})"; if (! $this->isCli) { echo '' . $file . '
' . $data . '
'; @@ -332,7 +332,7 @@ private function color($value, string $name): ?string */ private function counter(int $size, int $type = 0): string { - return $this->color('(' . ($type ? 'length' : 'size') . '=' . $size . ')', 'size'); + return $this->color('(' . ($type ? 'length' : 'size') . "={$size})", 'size'); } /** @@ -344,7 +344,7 @@ private function counter(int $size, int $type = 0): string */ private function type(string $type, string $before = ' '): string { - return $before . $this->color($type, 'type'); + return "{$before}{$this->color($type, 'type')}"; } /** @@ -390,11 +390,11 @@ private function arrayIndex(string $key, bool $parent = false): string { if (!$parent) { - return $this->color("'$key'", 'multi_array_key') . ' ' . $this->color('=', 'multi_array_arrow') . ' '; + return $this->color("'$key'", 'multi_array_key') . " {$this->color('=', 'multi_array_arrow')} "; } else { - return $this->color("'$key'", 'single_array_key') . ' ' . $this->color('=>', 'single_array_arrow') . ' '; + return $this->color("'$key'", 'single_array_key') . " {$this->color('=>', 'single_array_arrow')} "; } } @@ -472,24 +472,20 @@ private function formatObject($object) { if ($prop->isPrivate()) { - $tmp .= $this->breakLine() . $this->indent($this->indent) . $this->color('private', 'property_visibility') - . $this->pad(2) . ' ' . $this->color(':', 'property_arrow') . ' '; + $tmp .= "{$this->breakLine()}{$this->indent($this->indent)}{$this->color('private', 'property_visibility')}{$this->pad(2)} {$this->color(':', 'property_arrow')} "; } elseif ($prop->isProtected()) { - $tmp .= $this->breakLine() . $this->indent($this->indent) . $this->color('protected', 'property_visibility') . ' ' - . $this->color(':', 'property_arrow') . ' '; + $tmp .= "{$this->breakLine()}{$this->indent($this->indent)}{$this->color('protected', 'property_visibility')} {$this->color(':', 'property_arrow')} "; } elseif ($prop->isPublic()) { - $tmp .= $this->breakLine() . $this->indent($this->indent) . $this->color('public', 'property_visibility') - . $this->pad(3) . ' ' . $this->color(':', 'property_arrow') . ' '; + $tmp .= "{$this->breakLine()}{$this->indent($this->indent)}{$this->color('public', 'property_visibility')}{$this->pad(3)} {$this->color(':', 'property_arrow')} "; } $prop->setAccessible(true); - $tmp .= $this->color('\'' . $prop->getName() . '\'', 'property_name') . ' ' - . $this->color('=>', 'property_arrow') . ' ' - . $this->evaluate([$prop->getValue($object)], true, true); + $tmp .= $this->color("'{$prop->getName()}'", 'property_name') + . " {$this->color('=>', 'property_arrow')} {$this->evaluate(array($prop->getValue($object)), true, true)}"; } if ($tmp != '') @@ -502,7 +498,7 @@ private function formatObject($object) $tmp = str_replace([':name', ':id', ':content'], [ $reflection->getName(), - $this->color('#' . $this->refcount($object), 'size'), + $this->color("#{$this->refcount($object)}", 'size'), $tmp ], $this->color('object (:name) [:id] [:content]', 'object')); @@ -527,19 +523,26 @@ private function evaluate(array $args, bool $called = false, bool $from_obj = fa switch ($type) { case 'string': - $tmp .= $this->color('\'' . $each . '\'', $type) . ' ' . $this->counter(strlen($each), 1) . $this->type($type); + + if (! $this->isCli) + { + $each = nl2br(str_replace(array('<', ' '), array('<', ' '), $each)); + } + + $tmp .= $this->color("'{$each}'", $type) + . "{$this->counter(strlen($each), 1)}{$this->type($type)}"; break; case 'integer': - $tmp .= $this->color((string) $each, $type) . $this->type($type); + $tmp .= "{$this->color((string) $each, $type)}{$this->type($type)}"; break; case 'double': - $tmp .= $this->color((string) $each, $type) . $this->type($type); + $tmp .= "{$this->color((string) $each, $type)}{$this->type($type)}"; break; case 'NULL': - $tmp .= $this->color('null', 'null') . $this->type($type); + $tmp .= "{$this->color('null', 'null')}{$this->type($type)}"; break; case 'boolean': - $tmp .= $this->color($each ? 'true' : 'false', $type) . $this->type($type); + $tmp .= "{$this->color($each ? 'true' : 'false', $type)}{$this->type($type)}"; break; case 'array': $tmp .= str_replace([':size', ':content'], [