Skip to content

Fix deprecations triggered by Symfony 5.3 #489

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 1 commit into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Fix the conditions to automatically enable the cache instrumentation when possible (#487)
- Fix deprecations triggered by Symfony 5.3 (#489)

## 4.1.0 (2021-04-19)

Expand Down
10 changes: 5 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,6 @@ parameters:
count: 1
path: tests/EventListener/RequestListenerTest.php

-
message: "#^Call to method isMasterRequest\\(\\) on an unknown class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent\\.$#"
count: 1
path: tests/EventListener/SubRequestListenerTest.php

-
message: "#^Instantiated class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent not found\\.$#"
count: 2
Expand All @@ -195,6 +190,11 @@ parameters:
count: 1
path: tests/EventListener/SubRequestListenerTest.php

-
message: "#^Parameter \\#1 \\$event of method Sentry\\\\SentryBundle\\\\Tests\\\\EventListener\\\\SubRequestListenerTest\\:\\:isMainRequest\\(\\) expects Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\KernelEvent, Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent\\|Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\RequestEvent given\\.$#"
count: 1
path: tests/EventListener/SubRequestListenerTest.php

-
message: "#^Parameter \\$event of method Sentry\\\\SentryBundle\\\\Tests\\\\EventListener\\\\SubRequestListenerTest\\:\\:testHandleKernelRequestEvent\\(\\) has invalid typehint type Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent\\.$#"
count: 1
Expand Down
2 changes: 2 additions & 0 deletions src/EventListener/AbstractTracingRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

abstract class AbstractTracingRequestListener
{
use KernelEventForwardCompatibilityTrait;

/**
* @var HubInterface The current hub
*/
Expand Down
23 changes: 23 additions & 0 deletions src/EventListener/KernelEventForwardCompatibilityTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\EventListener;

use Symfony\Component\HttpKernel\Event\KernelEvent;

/**
* Provides forward compatibility with newer Symfony versions.
*
* @internal
*/
trait KernelEventForwardCompatibilityTrait
{
protected function isMainRequest(KernelEvent $event): bool
{
return method_exists($event, 'isMainRequest')
? $event->isMainRequest()
: $event->isMasterRequest()
;
}
}
8 changes: 5 additions & 3 deletions src/EventListener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
final class RequestListener
{
use KernelEventForwardCompatibilityTrait;

/**
* @var HubInterface The current hub
*/
Expand Down Expand Up @@ -47,7 +49,7 @@ public function __construct(HubInterface $hub, ?TokenStorageInterface $tokenStor
*/
public function handleKernelRequestEvent(RequestListenerRequestEvent $event): void
{
if (!$event->isMasterRequest()) {
if (!$this->isMainRequest($event)) {
return;
}

Expand Down Expand Up @@ -81,7 +83,7 @@ public function handleKernelRequestEvent(RequestListenerRequestEvent $event): vo
*/
public function handleKernelControllerEvent(RequestListenerControllerEvent $event): void
{
if (!$event->isMasterRequest()) {
if (!$this->isMainRequest($event)) {
return;
}

Expand All @@ -102,7 +104,7 @@ public function handleKernelControllerEvent(RequestListenerControllerEvent $even
private function getUsername($user): ?string
{
if ($user instanceof UserInterface) {
return $user->getUsername();
return method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
}

if (\is_string($user)) {
Expand Down
6 changes: 4 additions & 2 deletions src/EventListener/SubRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
final class SubRequestListener
{
use KernelEventForwardCompatibilityTrait;

/**
* @var HubInterface The current hub
*/
Expand All @@ -36,7 +38,7 @@ public function __construct(HubInterface $hub)
*/
public function handleKernelRequestEvent(SubRequestListenerRequestEvent $event): void
{
if ($event->isMasterRequest()) {
if ($this->isMainRequest($event)) {
return;
}

Expand All @@ -51,7 +53,7 @@ public function handleKernelRequestEvent(SubRequestListenerRequestEvent $event):
*/
public function handleKernelFinishRequestEvent(FinishRequestEvent $event): void
{
if ($event->isMasterRequest()) {
if ($this->isMainRequest($event)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/TracingRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class TracingRequestListener extends AbstractTracingRequestListener
*/
public function handleKernelRequestEvent(RequestListenerRequestEvent $event): void
{
if (!$event->isMasterRequest()) {
if (!$this->isMainRequest($event)) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/EventListener/TracingSubRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class TracingSubRequestListener extends AbstractTracingRequestListener
*/
public function handleKernelRequestEvent(SubRequestListenerRequestEvent $event): void
{
if ($event->isMasterRequest()) {
if ($this->isMainRequest($event)) {
return;
}

Expand Down Expand Up @@ -53,7 +53,7 @@ public function handleKernelRequestEvent(SubRequestListenerRequestEvent $event):
*/
public function handleKernelFinishRequestEvent(FinishRequestEvent $event): void
{
if ($event->isMasterRequest()) {
if ($this->isMainRequest($event)) {
return;
}

Expand Down
14 changes: 12 additions & 2 deletions tests/EventListener/RequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ public function getSalt()
return null;
}

public function getUsername()
public function getUsername(): string
{
return $this->getUserIdentifier();
}

public function getUserIdentifier(): string
{
return 'foo_user';
}
Expand Down Expand Up @@ -411,7 +416,12 @@ public function getSalt()
return null;
}

public function getUsername()
public function getUsername(): string
{
return $this->getUserIdentifier();
}

public function getUserIdentifier(): string
{
return 'foo_user';
}
Expand Down
7 changes: 5 additions & 2 deletions tests/EventListener/SubRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\EventListener\KernelEventForwardCompatibilityTrait;
use Sentry\SentryBundle\EventListener\SubRequestListener;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
Expand All @@ -18,6 +19,8 @@

class SubRequestListenerTest extends TestCase
{
use KernelEventForwardCompatibilityTrait;

/**
* @var MockObject&HubInterface
*/
Expand All @@ -42,7 +45,7 @@ protected function setUp(): void
*/
public function testHandleKernelRequestEvent($event): void
{
$this->hub->expects($event->isMasterRequest() ? $this->never() : $this->once())
$this->hub->expects($this->isMainRequest($event) ? $this->never() : $this->once())
->method('pushScope')
->willReturn(new Scope());

Expand Down Expand Up @@ -92,7 +95,7 @@ public function handleKernelRequestEventWithSymfonyVersionLowerThan43DataProvide
*/
public function testHandleKernelFinishRequestEvent($event): void
{
$this->hub->expects($event->isMasterRequest() ? $this->never() : $this->once())
$this->hub->expects($this->isMainRequest($event) ? $this->never() : $this->once())
->method('popScope');

$this->listener->handleKernelFinishRequestEvent($event);
Expand Down