Skip to content

Commit

Permalink
Added Exception for call instance methods when a Class constructor re…
Browse files Browse the repository at this point in the history
…quire parameters
  • Loading branch information
fadrian06 committed Feb 4, 2024
1 parent afba9c1 commit 6e29e66
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion flight/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion flight/core/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Exception;
use InvalidArgumentException;
use ReflectionClass;
use TypeError;

/**
Expand Down Expand Up @@ -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.');
}

Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 6e29e66

Please sign in to comment.