Refactor Dispatcher to improve type hints and simplify callable handling - #721
Open
fadrian06 wants to merge 35 commits into
Open
Refactor Dispatcher to improve type hints and simplify callable handling#721fadrian06 wants to merge 35 commits into
fadrian06 wants to merge 35 commits into
Conversation
…spatcher $filters property
…umentException $message argument
…atching named callables
…(self explanatory). - FilteredCallable wraps a callable and helps static analyzers to check if after filters are using the callable return type as output type. - FilteredCallable handles after filters with two parameters (deprecated) and one parameter (new required signature)
…e namedFilteredCallable property
Now filters are handled by each FilteredCallable.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors flight\core\Dispatcher to simplify callable execution and introduces a new FilteredCallable wrapper to run “before” and “after” filters around a callable, alongside minor static-analysis / coding-standards adjustments.
Changes:
- Added
flight\core\FilteredCallableto wrap a callable with before/after filters. - Refactored
flight\core\Dispatcherto useFilteredCallablefor named callables and updated container/callable handling. - Updated PHPCS ruleset (
phpcs.xml.dist) and adjusted PHPStan docblocks inEngine.php/Dispatcher.php.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
phpcs.xml.dist |
Tunes PHPCS rules by excluding a specific PSR2 spacing sniff. |
flight/Engine.php |
Docblock annotation cleanup (but currently leaves broken PHPStan generic references). |
flight/core/FilteredCallable.php |
New callable wrapper that applies before/after filter chains. |
flight/core/Dispatcher.php |
Dispatcher refactor to route named callables through FilteredCallable and simplify execution paths. |
Suppressed comments (6)
flight/core/Dispatcher.php:252
hook()assumesget($name)returns aFilteredCallableand callspushBeforeFilter()/pushAfterFilter(). Howeverget()can return a non-object callable (e.g. aClosurefrom the deprecated$eventsmap), which would cause a fatal error here. Guard withinstanceof FilteredCallable(or wrap the callable).
$filteredCallable = $this->get($name);
if ($filteredCallable) {
if ($type === self::FILTER_BEFORE) {
$filteredCallable->pushBeforeFilter($callback);
flight/core/Dispatcher.php:300
execute()assigns$container = $this->containerHandler;but never uses it. This is dead code and can be removed to avoid confusion.
public function execute($callback, array $params = [])
{
$container = $this->containerHandler;
$this->verifyValidFunction($callback);
flight/core/Dispatcher.php:476
verifyValidClassCallable()only validates method existence when$classis an object. If$classis a class-string and the method doesn't exist, this will fall through and later cause a runtimeErrorwhen calling$class->$method(...). Validatemethod_exists($class, $method)for class-strings too and throw anInvalidArgumentExceptionconsistently.
if (!is_object($class) && !class_exists($class)) {
$message = "Class '$class' not found. Is it being correctly autoloaded with Flight::path()?";
$exception = new InvalidArgumentException($message);
} elseif ($this->containerException) {
$exception = $this->containerException;
flight/core/Dispatcher.php:506
resolveContainerClass()only catchesContainerExceptionInterface. PSR-11get()may also throwNotFoundExceptionInterface(which does not extendContainerExceptionInterface), so those exceptions will currently escape and bypass the output-buffer fix logic. CatchThrowablehere (as the previous implementation did) so all container failures are handled consistently.
if ($container instanceof Container) {
try {
return $container->get($class);
} catch (ContainerExceptionInterface $exception) {
$this->containerException = $exception;
flight/core/FilteredCallable.php:84
- When adapting 2-parameter after filters, the wrapper passes a static empty
$inputarray to the original filter. This means after-filters that expect to inspect/modify the actual invocation args will silently receive[]instead of the real input.
$filter = static function (&$output) use ($filter) {
static $input = [];
return $filter($input, $output);
};
flight/core/Dispatcher.php:186
- The
get()docblock says it returns?FilteredCallable, but the return type is?callableand the method can also return other callables from the deprecated$eventsmap. Update the docblock to match the actual return value.
/**
* Returns a callable by its name.
*
* @param string $name Callable name.
* @return ?FilteredCallable
*/
public function get(string $name): ?callable
{
return $this->namedCallables[$name] ?? $this->events[$name] ?? null;
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a new utility for handling callables with before and after filters, and makes a minor update to the coding standards configuration. The most important changes are summarized below.
New Features
FilteredCallableclass inflight/core/FilteredCallable.phpthat allows wrapping a callable with before and after filters, enabling pre- and post-processing of function input and output. This utility supports flexible extension and control over callable execution.Code Quality and Standards
phpcs.xml.distto exclude theSpacingAfterOpenBracerule from PSR2, allowing more flexibility in code formatting for control structures.Type Annotations
@phpstan-templateannotation from theEngine.phpdocblock, likely as part of code cleanup or refactoring.