diff --git a/src/Writers/FunctionWriter.php b/src/Writers/FunctionWriter.php index 0fafd93..024608b 100644 --- a/src/Writers/FunctionWriter.php +++ b/src/Writers/FunctionWriter.php @@ -5,6 +5,7 @@ use Closure; use PhpParser\Node\Arg; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\Cast\Int_ as IntCast; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; @@ -85,6 +86,11 @@ public function closure(Closure $closure) return $closure; } + public function castInt(Expr $expr): IntCast + { + return new IntCast($expr); + } + private function convertToArgs($args) { return collect($args) diff --git a/src/Writers/TypeWriter.php b/src/Writers/TypeWriter.php index aaa7e6b..562031f 100644 --- a/src/Writers/TypeWriter.php +++ b/src/Writers/TypeWriter.php @@ -5,10 +5,10 @@ use Closure; use Exception; use Laravel\SerializableClosure\Support\ReflectionClosure; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\Cast\Bool_; use PhpParser\Node\Expr\Cast\Int_; -use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Scalar\String_; use Stillat\Proteus\Analyzers\TypeAnalyzer; use Stillat\Proteus\Analyzers\Types; @@ -40,7 +40,7 @@ public static function write($value) return $closureParser->parse($serialized->getCode()); } - if ($value instanceof FuncCall) { + if ($value instanceof Expr) { return $value; } diff --git a/tests/LaravelFunctionCallTest.php b/tests/LaravelFunctionCallTest.php index e7ae1a3..7129139 100644 --- a/tests/LaravelFunctionCallTest.php +++ b/tests/LaravelFunctionCallTest.php @@ -116,6 +116,19 @@ public function testFunctionCallsCanBeNestedAsArguments() ); } + public function testCastIntCanBeWritten() + { + $f = new FunctionWriter(); + + $this->assertChangeEquals( + __DIR__.'/configs/empty.php', + __DIR__.'/expected/castint.php', + [ + 'thing' => $f->castInt($f->env('KEY', 20)), + ] + ); + } + public function testWritingNewClosuresOrArrowFunctions() { $f = new FunctionWriter(); diff --git a/tests/expected/castint.php b/tests/expected/castint.php new file mode 100644 index 0000000..ca7d9a7 --- /dev/null +++ b/tests/expected/castint.php @@ -0,0 +1,5 @@ + (int) env('KEY', 20), +];