Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
burzum opened this issue Oct 31, 2021 · 5 comments
Open

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

burzum opened this issue Oct 31, 2021 · 5 comments

Comments

@burzum
Copy link

burzum commented Oct 31, 2021

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
Copy link
Owner

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

@burzum
Copy link
Author

burzum commented Nov 27, 2021

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
Copy link
Owner

Hi!

looked at what the tools reported

What tools do you use?

@burzum
Copy link
Author

burzum commented Nov 29, 2021

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants