From 0518ee9e1b5b3c7bbf3c9ad837a6591e39dbfab0 Mon Sep 17 00:00:00 2001 From: Jonathan Vollebregt Date: Sat, 27 Aug 2022 13:43:54 +0200 Subject: [PATCH] EnumPlugin --- build/kint.phar | Bin 408365 -> 415084 bytes src/Kint.php | 1 + src/Parser/ClassStaticsPlugin.php | 6 +++ src/Parser/EnumPlugin.php | 86 ++++++++++++++++++++++++++++++ src/Renderer/Text/EnumPlugin.php | 44 +++++++++++++++ src/Renderer/TextRenderer.php | 2 + src/Zval/EnumValue.php | 62 +++++++++++++++++++++ 7 files changed, 201 insertions(+) create mode 100644 src/Parser/EnumPlugin.php create mode 100644 src/Renderer/Text/EnumPlugin.php create mode 100644 src/Zval/EnumValue.php diff --git a/build/kint.phar b/build/kint.phar index 898c5ffb12d13d8d12015f46c24535e333dea65e..5f7b89006510731d0eb42081ee60a94bc710622a 100644 GIT binary patch delta 1438 zcmZuxe@q*76z}(Y-)rfujmm)f|` z{)C!oUGe3aOeSS8R1*{j6=|@hSsv;SMuvLAvOn4zJ;tRnsGS?P?Nk@I+=P{Tl|zC$ zpXDmriVm1lXlh?Mc1To*9eL_f7MnPojgeg|OGT+Qzat+|FSxE&>5`4=hvH(ysC`h3 z3`w-~8A}RIttY?vZ}lecIsrKv zi5wYeDj5A1S3}zYCIgPMnt)< zqSUyQ-&_unL|kD167vxJ31@&khRiVaA#%a_MP%nT+6U-4oX_dp zX^N#UaIX6L9hObWJ2HU_;9?AMCh<06Q@h&@g!Qr9{l$0RwXJlQ2Qz{&;ehLQcG z5N4~e1=Jj4;-iTzAfFsE|^Uso5p9MuLyX4#a@`7$9tha z$yz277OcOFmavfbhhgmnl9gi72Ur4uhZ?IKX zI$nL)^&Cvv*<#k1HjHL6JOp+zHb**BC|w7Fc%I}s*AKM!X@7Ag5+Jg~mKUajn?9Hg zK~TF&cxc~-thU~;K8SSs%|BQkqQ7AdenhO9k8KY68jW@Id++u_@+B-l@GvG${j;ek z-GYM+k{@mpTh{+@z>J&Z!=AU04URj_PMH3k5NH?JitP1m+)5ajVM|~>#1L@vX8XCg z`Fk=E_pn;rV;(5jH^w~sO;cy%r4RmkNNpM0^-*=zwVCYY`PH9)ne$%hnQgf?zL=Td nTX(mL*LTAs4cdb*ldmV<98uS%K7M>cU8spQ>?NS>UCo)Zcu$hrV>n&NgueW3?mGbjfB85OfDo#Z!o zcndk$+!J>H;_vW)iPH9h3_9zaG#9_WT9WS6qtC*? Kz~JuW>jVJe3SkTY diff --git a/src/Kint.php b/src/Kint.php index e2145b062..701283c28 100644 --- a/src/Kint.php +++ b/src/Kint.php @@ -150,6 +150,7 @@ class Kint 'Kint\\Parser\\ClosurePlugin', 'Kint\\Parser\\ColorPlugin', 'Kint\\Parser\\DateTimePlugin', + 'Kint\\Parser\\EnumPlugin', 'Kint\\Parser\\FsPathPlugin', 'Kint\\Parser\\IteratorPlugin', 'Kint\\Parser\\JsonPlugin', diff --git a/src/Parser/ClassStaticsPlugin.php b/src/Parser/ClassStaticsPlugin.php index 89601af2f..7d2673f84 100644 --- a/src/Parser/ClassStaticsPlugin.php +++ b/src/Parser/ClassStaticsPlugin.php @@ -30,6 +30,7 @@ use Kint\Zval\Value; use ReflectionClass; use ReflectionProperty; +use UnitEnum; class ClassStaticsPlugin extends Plugin { @@ -56,6 +57,11 @@ public function parse(&$var, Value &$o, $trigger) $consts = []; foreach ($reflection->getConstants() as $name => $val) { + // Skip enum constants + if ($var instanceof UnitEnum && $val instanceof UnitEnum && $o->classname == \get_class($val)) { + continue; + } + $const = Value::blank($name, '\\'.$class.'::'.$name); $const->const = true; $const->depth = $o->depth + 1; diff --git a/src/Parser/EnumPlugin.php b/src/Parser/EnumPlugin.php new file mode 100644 index 000000000..3fe25f4eb --- /dev/null +++ b/src/Parser/EnumPlugin.php @@ -0,0 +1,86 @@ +contents = []; + + foreach ($var->cases() as $case) { + $base_obj = Value::blank($class.'::'.$case->name, '\\'.$class.'::'.$case->name); + $base_obj->depth = $o->depth + 1; + + if ($var instanceof BackedEnum) { + $c = $case->value; + $cases->contents[] = $this->parser->parse($c, $base_obj); + } else { + $cases->contents[] = $base_obj; + } + } + + self::$cache[$class] = $cases; + } + + $object = new EnumValue($var); + $object->transplant($o); + + $object->addRepresentation(self::$cache[$class], 0); + + $o = $object; + } +} diff --git a/src/Renderer/Text/EnumPlugin.php b/src/Renderer/Text/EnumPlugin.php new file mode 100644 index 000000000..efb5ba5d1 --- /dev/null +++ b/src/Renderer/Text/EnumPlugin.php @@ -0,0 +1,44 @@ +depth) { + $out .= $this->renderer->colorTitle($this->renderer->renderTitle($o)).PHP_EOL; + } + + $out .= $this->renderer->renderHeader($o).PHP_EOL; + + return $out; + } +} diff --git a/src/Renderer/TextRenderer.php b/src/Renderer/TextRenderer.php index 0cfba527c..3421e132b 100644 --- a/src/Renderer/TextRenderer.php +++ b/src/Renderer/TextRenderer.php @@ -42,6 +42,7 @@ class TextRenderer extends Renderer 'microtime' => 'Kint\\Renderer\\Text\\MicrotimePlugin', 'recursion' => 'Kint\\Renderer\\Text\\RecursionPlugin', 'trace' => 'Kint\\Renderer\\Text\\TracePlugin', + 'enum' => 'Kint\\Renderer\\Text\\EnumPlugin', ]; /** @@ -55,6 +56,7 @@ class TextRenderer extends Renderer 'Kint\\Parser\\MicrotimePlugin', 'Kint\\Parser\\StreamPlugin', 'Kint\\Parser\\TracePlugin', + 'Kint\\Parser\\EnumPlugin', ]; /** diff --git a/src/Zval/EnumValue.php b/src/Zval/EnumValue.php new file mode 100644 index 000000000..018eb68f0 --- /dev/null +++ b/src/Zval/EnumValue.php @@ -0,0 +1,62 @@ +enumval = $enumval; + } + + public function getValueShort() + { + if ($this->enumval instanceof BackedEnum) { + if (\is_string($this->enumval->value)) { + return '"'.$this->enumval->value.'"'; + } + if (\is_int($this->enumval->value)) { + return (string) $this->enumval->value; + } + } + } + + public function getType() + { + return $this->classname.'::'.$this->enumval->name; + } + + public function getSize() + { + } +}