Skip to content

Instantly share code, notes, and snippets.

@smarek
Created May 27, 2020 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smarek/cd6dcdad918444d2e9679f3644990b55 to your computer and use it in GitHub Desktop.
Save smarek/cd6dcdad918444d2e9679f3644990b55 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace App\Controller;
use Cake\Controller\Controller;
use Closure;
use ReflectionFunction;
class AppController extends Controller
{
public function invokeAction(Closure $action, array $args): void
{
$reflection = new ReflectionFunction($action);
$passedArgs = [];
$counter = 0;
foreach ($reflection->getParameters() as $reflectionParameter) {
if (!$reflectionParameter->hasType()) {
$passedArgs[] = $args[$counter];
} else {
switch ($reflectionParameter->getType()->getName()) {
default:
dump($reflectionParameter->getType()->getName());
die();
case "int":
if (!isset($args[$counter]) && $reflectionParameter->allowsNull()) {
$passedArgs[] = null;
} else {
$passedArgs[] = isset($args[$counter]) ? intval($args[$counter]) : 0;
}
break;
}
}
$counter++;
}
parent::invokeAction($action, $passedArgs);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment