Skip to content

Fix deprecation notice in route name extraction #543

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

Merged
merged 9 commits into from
Feb 14, 2022

Conversation

X-Coder264
Copy link
Contributor

We just updated one of our projects from Laravel 8 and PHP 8.0 to Laravel 9 and PHP 8.1 and immediately

str_ends_with(): Passing null to parameter #1 ($haystack) of type string is deprecated in /code/vendor/laravel/framework/src/Illuminate/Support/Str.php on line 247

started popping up on Sentry (as we have the deprecation logger configured for the app).

Turns out there is a code path here that triggers that deprecation (specifically the code path when the route has no name so Laravel auto generates the name for the route).

This PR fixes that deprecation and adds tests for the method that this PR touched.

@stayallive stayallive changed the title Fix PHP 8.1 deprecation Fix deprecation notice in route name extraction Feb 14, 2022
@stayallive
Copy link
Collaborator

@X-Coder264, nice catch! We indeed missed that.

I did some refactors and added some more tests, I hope you do not mind 😄

(CI seems to have some temporary issues at the moment, will retry them later)

@X-Coder264
Copy link
Contributor Author

X-Coder264 commented Feb 14, 2022

@stayallive Since you went and refactored a bunch of code, just one more thing that I've noticed.

public static function extractNameForRoute(Route $route): ?string

The return type is nullable even though null can never be returned from this method:

        // /someaction // Fallback to the url
        if (empty($routeName) || $routeName === 'Closure') {
            $routeName = '/' . ltrim($route->uri(), '/');
        }

        return $routeName;

If $routeName is null empty($routeName) will return true and the if will be executed turning $routeName into a string so the return type should be string, not ?string.

Also np for the refactors, those are always welcome 😄

@stayallive
Copy link
Collaborator

Yep, good catch! Copy pasta error :)

@X-Coder264
Copy link
Contributor Author

You changed the return type of extractNameForActionRoute though, instead of extractNameForRoute. I'm not sure if extractNameForActionRoute can or cannot return null.

@stayallive
Copy link
Collaborator

Yep, I should take some time to read better, both were "wrong" though 👍

@X-Coder264
Copy link
Contributor Author

X-Coder264 commented Feb 14, 2022

Now that the return type was narrowed \Sentry\Laravel\Tracing\Middleware::updateTransactionNameIfDefault parameter type for $name can be narrowed too.

And

$routeName = Integration::extractNameForRoute($route) ?? '<unlabeled transaction>';

in \Sentry\Laravel\EventHandler::routerMatchedHandler can be just

$routeName = Integration::extractNameForRoute($route);

@stayallive stayallive merged commit 426ce3e into getsentry:master Feb 14, 2022
@X-Coder264 X-Coder264 deleted the fix-deprecation branch February 14, 2022 19:56
@stayallive
Copy link
Collaborator

Thanks for the PR and the feedback @X-Coder264, 2.11.1 with this fix should be released soon 💪

@X-Coder264
Copy link
Contributor Author

Cool, thank you for such a quick review / merging / release tagging process.

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

Successfully merging this pull request may close these issues.

2 participants