diff --git a/flight/Engine.php b/flight/Engine.php index c62df84a..58b08599 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -145,7 +145,7 @@ public function init(): void ]; foreach ($methods as $name) { - $this->dispatcher->set($name, [$this, '_' . $name]); + $this->dispatcher->set($name, [$this, "_$name"]); } // Default configuration settings diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index a0f24aaa..d91b4b2d 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -7,6 +7,7 @@ use Closure; use Exception; use InvalidArgumentException; +use ReflectionClass; use TypeError; /** @@ -193,7 +194,12 @@ public static function filter(array $filters, array &$params, &$output): void */ public static function execute($callback, array &$params = []) { - if (is_string($callback) && !function_exists($callback)) { + $isInvalidFunctionName = ( + is_string($callback) + && !function_exists($callback) + ); + + if ($isInvalidFunctionName) { throw new InvalidArgumentException('Invalid callback specified.'); } @@ -231,6 +237,23 @@ public static function invokeMethod(array $func, array &$params = []) [$class, $method] = $func; if (is_string($class) && class_exists($class)) { + $constructor = (new ReflectionClass($class))->getConstructor(); + $constructorParamsNumber = 0; + + if ($constructor !== null) { + $constructorParamsNumber = count($constructor->getParameters()); + } + + if ($constructorParamsNumber > 0) { + $exceptionMessage = "Method '$class::$method' cannot be called statically. "; + $exceptionMessage .= sprintf( + "$class::__construct require $constructorParamsNumber parameter%s", + $constructorParamsNumber > 1 ? 's' : '' + ); + + throw new InvalidArgumentException($exceptionMessage, E_ERROR); + } + $class = new $class(); }