Repository: phpbench/phpbench (php)

Issue:
Error with bare report when DateTime used as parameter value
```
In PhpValueFactory.php line 41:                                                                                                                         
                                                                                                                                                        
  [RuntimeException]                                                                                                                                    
  Cannot interpret PHP value "DateTime"                                                                                                                 
                                                                                                                                                        
 ```

Pull request:
Fix exception on unrepresentable values
Fix #832 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b650eea7..5af08fa2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,13 @@
 CHANGELOG
 =========
 
+master
+------
+
+Bug fix:
+
+- Error with bare report when DateTime used as param #832
+
 1.0.0 (2021-05-09)
 ------------------
 
diff --git a/lib/Expression/Ast/PhpValueFactory.php b/lib/Expression/Ast/PhpValueFactory.php
index a68aeb96..f62a1e7b 100644
--- a/lib/Expression/Ast/PhpValueFactory.php
+++ b/lib/Expression/Ast/PhpValueFactory.php
@@ -3,7 +3,6 @@
 namespace PhpBench\Expression\Ast;
 
 use function is_float;
-use RuntimeException;
 
 final class PhpValueFactory
 {
@@ -38,8 +37,6 @@ final class PhpValueFactory
             }, $value));
         }
 
-        throw new RuntimeException(sprintf(
-            'Cannot interpret PHP value "%s"', gettype($value)
-        ));
+        return new UnrepresentableValueNode($value);
     }
 }
diff --git a/lib/Expression/Ast/UnrepresentableValueNode.php b/lib/Expression/Ast/UnrepresentableValueNode.php
new file mode 100644
index 00000000..f3261b94
--- /dev/null
+++ b/lib/Expression/Ast/UnrepresentableValueNode.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace PhpBench\Expression\Ast;
+
+final class UnrepresentableValueNode implements PhpValue
+{
+    /**
+     * @var mixed
+     */
+    private $value;
+
+    /**
+     * @param mixed $value
+     */
+    public function __construct($value)
+    {
+        $this->value = $value;
+    }
+
+    /**
+     * @return mixed
+     */
+    public function value()
+    {
+        return $this->value;
+    }
+}
diff --git a/lib/Expression/NodePrinter/UnrepresentableValuePrinter.php b/lib/Expression/NodePrinter/UnrepresentableValuePrinter.php
new file mode 100644
index 00000000..fcded255
--- /dev/null
+++ b/lib/Expression/NodePrinter/UnrepresentableValuePrinter.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace PhpBench\Expression\NodePrinter;
+
+use PhpBench\Expression\Ast\Node;
+use PhpBench\Expression\Ast\UnrepresentableValueNode;
+use PhpBench\Expression\NodePrinter;
+use PhpBench\Expression\Printer;
+
+class UnrepresentableValuePrinter implements NodePrinter
+{
+    public function print(Printer $printer, Node $node): ?string
+    {
+        if (!$node instanceof UnrepresentableValueNode) {
+            return null;
+        }
+
+        return sprintf('<%s>', gettype($node->value()));
+    }
+}
diff --git a/lib/Extension/ExpressionExtension.php b/lib/Extension/ExpressionExtension.php
index 1db0685d..589dd844 100644
--- a/lib/Extension/ExpressionExtension.php
+++ b/lib/Extension/ExpressionExtension.php
@@ -73,6 +73,7 @@ use PhpBench\Expression\NodePrinter\RelativeDeviationPrinter;
 use PhpBench\Expression\NodePrinter\StringPrinter;
 use PhpBench\Expression\NodePrinter\TolerablePrinter;
 use PhpBench\Expression\NodePrinter\UnitPrinter;
+use PhpBench\Expression\NodePrinter\UnrepresentableValuePrinter;
 use PhpBench\Expression\NodePrinter\ValueWithUnitPrinter;
 use PhpBench\Expression\NodePrinters;
 use PhpBench\Expression\Parselet\ArithmeticOperatorParselet;
@@ -277,6 +278,7 @@ class ExpressionExtension implements ExtensionInterface
                 new ConcatPrinter(),
                 new PercentageDifferencePrinter(),
                 new NullPrinter(),
+                new UnrepresentableValuePrinter(),
             ]);
         });
 


Test patch:
diff --git a/tests/Unit/Expression/Ast/PhpValueFactoryTest.php b/tests/Unit/Expression/Ast/PhpValueFactoryTest.php
index d49ca6be..ba343efb 100644
--- a/tests/Unit/Expression/Ast/PhpValueFactoryTest.php
+++ b/tests/Unit/Expression/Ast/PhpValueFactoryTest.php
@@ -2,6 +2,7 @@
 
 namespace PhpBench\Tests\Unit\Expression\Ast;
 
+use DateTime;
 use Generator;
 use PhpBench\Expression\Ast\BooleanNode;
 use PhpBench\Expression\Ast\FloatNode;
@@ -10,6 +11,7 @@ use PhpBench\Expression\Ast\Node;
 use PhpBench\Expression\Ast\NullNode;
 use PhpBench\Expression\Ast\PhpValueFactory;
 use PhpBench\Expression\Ast\StringNode;
+use PhpBench\Expression\Ast\UnrepresentableValueNode;
 use PHPUnit\Framework\TestCase;
 
 class PhpValueFactoryTest extends TestCase
@@ -38,5 +40,10 @@ class PhpValueFactoryTest extends TestCase
         yield [false, new BooleanNode(false)];
 
         yield [null, new NullNode()];
+
+        yield [
+            new DateTime('2021-01-01T00:00:00+00:00'),
+            new UnrepresentableValueNode(new DateTime('2021-01-01T00:00:00+00:00'))
+        ];
     }
 }
diff --git a/tests/Unit/Expression/NodePrinter/UnrepresentableValuePrinterTest.php b/tests/Unit/Expression/NodePrinter/UnrepresentableValuePrinterTest.php
new file mode 100644
index 00000000..1f263753
--- /dev/null
+++ b/tests/Unit/Expression/NodePrinter/UnrepresentableValuePrinterTest.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace PhpBench\Tests\Unit\Expression\NodePrinter;
+
+use PhpBench\Expression\Ast\NullNode;
+use PhpBench\Expression\Ast\UnrepresentableValueNode;
+use PhpBench\Expression\NodePrinter\UnrepresentableValuePrinter;
+use PhpBench\Expression\Printer;
+use PhpBench\Tests\ProphecyTrait;
+use PHPUnit\Framework\TestCase;
+
+class UnrepresentableValuePrinterTest extends TestCase
+{
+    use ProphecyTrait;
+
+    public function testPrint(): void
+    {
+        $printer = $this->prophesize(Printer::class);
+        self::assertEquals('<string>', (new UnrepresentableValuePrinter())->print(
+            $printer->reveal(),
+            new UnrepresentableValueNode('asd')
+        ));
+    }
+
+    public function testDoesNotPrint(): void
+    {
+        $printer = $this->prophesize(Printer::class);
+        self::assertNull((new UnrepresentableValuePrinter())->print(
+            $printer->reveal(),
+            new NullNode()
+        ));
+    }
+}