alexdodonov / mezon-router

Small and fast router
https://twitter.com/mezonphp
269 stars 18 forks source link

Result object instead of exception when calling getCallback()? #29

Open burzum opened 3 years ago

burzum commented 3 years ago

I don't mind implementing this and proving a PR but before I do so, I would like to know if there is a chance this will be accepted.

The exception is nothing exceptional, I'm expecting that some routes don't match. If, for some weird reason, an exception is prefered here, then it should be something specific, like "NoMatchingRouteException". Using \Exception is very generic.

I would expect to get a result object back that contains the actual route and handler and a method to verify the outcome of the result if ($result->matched()) { $handler = $result->getHandler(); }.

alexdodonov commented 3 years ago

Hi! Sounds good. It will be very nice of you to create a PR.

burzum commented 2 years ago

Hey, sorry for the late answer, I've been sick and busy with work. I checked the lib today and before adding a new feature I looked at what the tools reported and other improvements like adding strict types because it's set to 7.4 anyways in composer.json. In the UrlParser trait this caused a type problem coming from this line $this->parameters = $result[1];

                if (array_key_exists(1, $result)) {
                    $this->parameters = $result[1];
                }

I have no idea how to fix it right now and I'm done for today. If you have any suggestions?

Oh, and Pslam reports LOTS of infos.

alexdodonov commented 2 years ago

Hi!

looked at what the tools reported

What tools do you use?

burzum commented 2 years ago

composer test after I added types:

> php ./vendor/phpunit/phpunit/phpunit --order-by=random
PHPUnit 8.5.21 by Sebastian Bergmann and contributors.

Random seed:   1638210655
Error:         This version of PHPUnit does not support code coverage on PHP 8

...............................................................  63 / 360 ( 17%)
............................................................... 126 / 360 ( 35%)
........................E

Time: 1.27 seconds, Memory: 12.00 MB

There was 1 error:

1) Mezon\Router\Tests\Standart\MiddlewareUnitTest::testMiddleware
TypeError: Cannot assign int to property Mezon\Router\Router::$parameters of type array

The code there has not been good before, the types make it just visible.


composer psalm and this is just an excerpt of all the things it reports:

INFO: MixedInferredReturnType - Mezon/Router/UrlParser.php:327:16 - Could not verify return type 'array<array-key, mixed>|bool|callable' for Mezon\Router\UrlParser::getStaticRouteProcessor (see https://psalm.dev/047)
     * @return array|callable|bool route handler

INFO: MixedAssignment - Mezon/Router/UrlParser.php:331:9 - Unable to determine the type that $processors is being assigned to (see https://psalm.dev/032)
        $processors = $this->staticRoutes[$_SERVER['REQUEST_METHOD']];

INFO: MixedArrayOffset - Mezon/Router/UrlParser.php:331:23 - Cannot access value on variable $this->staticRoutes using mixed offset (see https://psalm.dev/031)
        $processors = $this->staticRoutes[$_SERVER['REQUEST_METHOD']];

INFO: MixedArrayAccess - Mezon/Router/UrlParser.php:336:20 - Cannot access array value on mixed variable $processors (see https://psalm.dev/051)
            return $processors[$route];

INFO: MixedInferredReturnType - Mezon/Router/UrlParser.php:345:16 - Could not verify return type 'array<array-key, mixed>|bool|callable' for Mezon\Router\UrlParser::getUniversalRouteProcessor (see https://psalm.dev/047)
     * @return array|callable|bool route handler

INFO: MixedAssignment - Mezon/Router/UrlParser.php:349:9 - Unable to determine the type that $processors is being assigned to (see https://psalm.dev/032)
        $processors = $this->staticRoutes[$_SERVER['REQUEST_METHOD']];

INFO: MixedArrayOffset - Mezon/Router/UrlParser.php:349:23 - Cannot access value on variable $this->staticRoutes using mixed offset (see https://psalm.dev/031)
        $processors = $this->staticRoutes[$_SERVER['REQUEST_METHOD']];

INFO: MixedArrayAccess - Mezon/Router/UrlParser.php:354:20 - Cannot access array value on mixed variable $processors (see https://psalm.dev/051)
            return $processors['*'];

INFO: MixedInferredReturnType - Mezon/Router/UrlParser.php:401:16 - Could not verify return type 'string' for Mezon\Router\UrlParser::getParam (see https://psalm.dev/047)
     * @return string Route parameter

INFO: MixedAssignment - Mezon/Router/UrlParser.php:446:42 - Unable to determine the type that $value is being assigned to (see https://psalm.dev/032)
        foreach ($parameters as $name => $value) {

INFO: MixedArgument - Mezon/Router/UrlParser.php:447:72 - Argument 2 of preg_replace cannot be mixed, expecting array<array-key, float|int|string>|string (see https://psalm.dev/030)
            $route = preg_replace('/\[([A-Za-z_\-])\:' . $name . ']/', $value, $route);
alexdodonov commented 2 years ago

And what settings have you made?

I have added strict-types=1 in all files in the project but still do not see any errors