Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Writers/FunctionWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/Writers/TypeWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,7 +40,7 @@ public static function write($value)
return $closureParser->parse($serialized->getCode());
}

if ($value instanceof FuncCall) {
if ($value instanceof Expr) {
return $value;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/LaravelFunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions tests/expected/castint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'thing' => (int) env('KEY', 20),
];