diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 8a5c51fdb..aed65b5ed 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -38,6 +38,7 @@ ], 'no_whitespace_before_comma_in_array' => false, // Should be dropped when we drop support for PHP 7.x 'stringable_for_to_string' => false, + 'modern_serialization_methods' => false, // Could be re-enabled when we drop support for PHP 7.3 and lower ]) ->setRiskyAllowed(true) ->setFinder( diff --git a/src/HttpClient/HttpClient.php b/src/HttpClient/HttpClient.php index 2a373d930..fc0823373 100644 --- a/src/HttpClient/HttpClient.php +++ b/src/HttpClient/HttpClient.php @@ -57,7 +57,7 @@ public function sendRequest(Request $request, Options $options): Response } $responseHeaders = []; - $responseHeaderCallback = function ($curlHandle, $headerLine) use (&$responseHeaders): int { + $responseHeaderCallback = static function ($curlHandle, $headerLine) use (&$responseHeaders): int { return Http::parseResponseHeaders($headerLine, $responseHeaders); }; diff --git a/src/Logs/LogsAggregator.php b/src/Logs/LogsAggregator.php index a1922d346..31df0a735 100644 --- a/src/Logs/LogsAggregator.php +++ b/src/Logs/LogsAggregator.php @@ -78,7 +78,7 @@ public function add( $log->setAttribute('sentry.sdk.version', $client->getSdkVersion()); } - $hub->configureScope(function (Scope $scope) use ($log) { + $hub->configureScope(static function (Scope $scope) use ($log) { $user = $scope->getUser(); if ($user !== null) { if ($user->getId() !== null) { @@ -181,7 +181,7 @@ private function getTraceId(HubInterface $hub): string $traceId = ''; - $hub->configureScope(function (Scope $scope) use (&$traceId) { + $hub->configureScope(static function (Scope $scope) use (&$traceId) { $traceId = (string) $scope->getPropagationContext()->getTraceId(); }); diff --git a/src/Metrics/MetricsAggregator.php b/src/Metrics/MetricsAggregator.php index e18ca7ddf..a25df0b35 100644 --- a/src/Metrics/MetricsAggregator.php +++ b/src/Metrics/MetricsAggregator.php @@ -79,7 +79,7 @@ public function add( ]; if ($options->shouldSendDefaultPii()) { - $hub->configureScope(function (Scope $scope) use (&$defaultAttributes) { + $hub->configureScope(static function (Scope $scope) use (&$defaultAttributes) { $user = $scope->getUser(); if ($user !== null) { if ($user->getId() !== null) { @@ -111,7 +111,7 @@ public function add( $spanId = $span->getSpanId(); $traceId = $span->getTraceId(); } else { - $hub->configureScope(function (Scope $scope) use (&$traceId, &$spanId) { + $hub->configureScope(static function (Scope $scope) use (&$traceId, &$spanId) { $propagationContext = $scope->getPropagationContext(); $traceId = $propagationContext->getTraceId(); $spanId = $propagationContext->getSpanId(); diff --git a/src/Tracing/Span.php b/src/Tracing/Span.php index b2aa1adea..1d909ad9d 100644 --- a/src/Tracing/Span.php +++ b/src/Tracing/Span.php @@ -299,7 +299,7 @@ public function setStatus(?SpanStatus $status) */ public function setHttpStatus(int $statusCode) { - SentrySdk::getCurrentHub()->configureScope(function (Scope $scope) use ($statusCode) { + SentrySdk::getCurrentHub()->configureScope(static function (Scope $scope) use ($statusCode) { $scope->setContext('response', [ 'status_code' => $statusCode, ]); diff --git a/src/functions.php b/src/functions.php index e0a39c685..609e24198 100644 --- a/src/functions.php +++ b/src/functions.php @@ -247,7 +247,7 @@ function startTransaction(TransactionContext $context, array $customSamplingCont */ function trace(callable $trace, SpanContext $context) { - return SentrySdk::getCurrentHub()->withScope(function (Scope $scope) use ($context, $trace) { + return SentrySdk::getCurrentHub()->withScope(static function (Scope $scope) use ($context, $trace) { $parentSpan = $scope->getSpan(); // If there is a span set on the scope and it's sampled there is an active transaction. @@ -289,7 +289,7 @@ function getTraceparent(): string } $traceParent = ''; - $hub->configureScope(function (Scope $scope) use (&$traceParent) { + $hub->configureScope(static function (Scope $scope) use (&$traceParent) { $traceParent = $scope->getPropagationContext()->toTraceparent(); }); @@ -314,7 +314,7 @@ function getBaggage(): string } $baggage = ''; - $hub->configureScope(function (Scope $scope) use (&$baggage) { + $hub->configureScope(static function (Scope $scope) use (&$baggage) { $baggage = $scope->getPropagationContext()->toBaggage(); }); @@ -330,7 +330,7 @@ function getBaggage(): string function continueTrace(string $sentryTrace, string $baggage): TransactionContext { $hub = SentrySdk::getCurrentHub(); - $hub->configureScope(function (Scope $scope) use ($sentryTrace, $baggage) { + $hub->configureScope(static function (Scope $scope) use ($sentryTrace, $baggage) { $propagationContext = PropagationContext::fromHeaders($sentryTrace, $baggage); $scope->setPropagationContext($propagationContext); }); @@ -357,7 +357,7 @@ function trace_metrics(): TraceMetrics */ function addFeatureFlag(string $name, bool $result): void { - SentrySdk::getCurrentHub()->configureScope(function (Scope $scope) use ($name, $result) { + SentrySdk::getCurrentHub()->configureScope(static function (Scope $scope) use ($name, $result) { $scope->addFeatureFlag($name, $result); }); } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 8e3bd4643..6f23fba98 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -980,7 +980,7 @@ public function testAttachStacktrace(): void $transport = $this->createMock(TransportInterface::class); $transport->expects($this->once()) ->method('send') - ->with($this->callback(function (Event $event): bool { + ->with($this->callback(static function (Event $event): bool { $result = $event->getStacktrace(); return $result !== null; diff --git a/tests/FrameBuilderTest.php b/tests/FrameBuilderTest.php index 667b85a18..ecb01f0e1 100644 --- a/tests/FrameBuilderTest.php +++ b/tests/FrameBuilderTest.php @@ -290,7 +290,7 @@ public function testGetFunctionArgumentsWithVariadicParameters(): void $options = new Options([]); $frameBuilder = new FrameBuilder($options, new RepresentationSerializer($options)); - $testFunction = function (string $first, int $second, ...$rest) { + $testFunction = static function (string $first, int $second, ...$rest) { }; $backtraceFrame = [ @@ -325,7 +325,7 @@ public function testGetFunctionArgumentsWithOnlyVariadicParameters(): void $options = new Options([]); $frameBuilder = new FrameBuilder($options, new RepresentationSerializer($options)); - $testFunction = function (...$args) { + $testFunction = static function (...$args) { }; $backtraceFrame = [ @@ -352,7 +352,7 @@ public function testGetFunctionArgumentsWithEmptyVariadicParameters(): void $options = new Options([]); $frameBuilder = new FrameBuilder($options, new RepresentationSerializer($options)); - $testFunction = function (string $first, ...$rest) { + $testFunction = static function (string $first, ...$rest) { }; $backtraceFrame = [ @@ -381,7 +381,7 @@ public function testGetFunctionArgumentsWithNullValues(): void $options = new Options([]); $frameBuilder = new FrameBuilder($options, new RepresentationSerializer($options)); - $testFunction = function (string $first, $second, ...$rest) { + $testFunction = static function (string $first, $second, ...$rest) { }; $backtraceFrame = [ @@ -411,7 +411,7 @@ public function testGetFunctionArgumentsWithGapsInBacktraceArrayIndices(): void $options = new Options([]); $frameBuilder = new FrameBuilder($options, new RepresentationSerializer($options)); - $testFunction = function (string $first, int $second, ...$rest) { + $testFunction = static function (string $first, int $second, ...$rest) { }; $backtraceFrameArgs = []; diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 488a40c7a..1b5689b8d 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -225,15 +225,15 @@ public function testWithMonitor(): void $hub->expects($this->exactly(2)) ->method('captureCheckIn') ->with( - $this->callback(function (string $slug): bool { + $this->callback(static function (string $slug): bool { return $slug === 'test-crontab'; }), - $this->callback(function (CheckInStatus $checkInStatus): bool { + $this->callback(static function (CheckInStatus $checkInStatus): bool { // just check for type CheckInStatus return true; }), $this->anything(), - $this->callback(function (MonitorConfig $monitorConfig): bool { + $this->callback(static function (MonitorConfig $monitorConfig): bool { return $monitorConfig->getSchedule()->getValue() === '*/5 * * * *' && $monitorConfig->getSchedule()->getType() === MonitorSchedule::TYPE_CRONTAB && $monitorConfig->getCheckinMargin() === 5 @@ -244,7 +244,7 @@ public function testWithMonitor(): void SentrySdk::setCurrentHub($hub); - withMonitor('test-crontab', function () { + withMonitor('test-crontab', static function () { // Do something... }, new MonitorConfig( new MonitorSchedule(MonitorSchedule::TYPE_CRONTAB, '*/5 * * * *'), @@ -262,15 +262,15 @@ public function testWithMonitorCallableThrows(): void $hub->expects($this->exactly(2)) ->method('captureCheckIn') ->with( - $this->callback(function (string $slug): bool { + $this->callback(static function (string $slug): bool { return $slug === 'test-crontab'; }), - $this->callback(function (CheckInStatus $checkInStatus): bool { + $this->callback(static function (CheckInStatus $checkInStatus): bool { // just check for type CheckInStatus return true; }), $this->anything(), - $this->callback(function (MonitorConfig $monitorConfig): bool { + $this->callback(static function (MonitorConfig $monitorConfig): bool { return $monitorConfig->getSchedule()->getValue() === '*/5 * * * *' && $monitorConfig->getSchedule()->getType() === MonitorSchedule::TYPE_CRONTAB && $monitorConfig->getCheckinMargin() === 5 @@ -281,7 +281,7 @@ public function testWithMonitorCallableThrows(): void SentrySdk::setCurrentHub($hub); - withMonitor('test-crontab', function () { + withMonitor('test-crontab', static function () { throw new \Exception(); }, new MonitorConfig( new MonitorSchedule(MonitorSchedule::TYPE_CRONTAB, '*/5 * * * *'), @@ -353,7 +353,7 @@ public function testTraceReturnsClosureResult(): void { $returnValue = 'foo'; - $result = trace(function () use ($returnValue) { + $result = trace(static function () use ($returnValue) { return $returnValue; }, new SpanContext()); @@ -380,7 +380,7 @@ public function testTraceCorrectlyReplacesAndRestoresCurrentSpan(): void $this->assertSame($transaction, $hub->getSpan()); try { - trace(function () { + trace(static function () { throw new \RuntimeException('Throwing should still restore the previous span'); }, new SpanContext()); } catch (\RuntimeException $e) { diff --git a/tests/Logs/LogsAggregatorTest.php b/tests/Logs/LogsAggregatorTest.php index 1478853e8..92bcb365d 100644 --- a/tests/Logs/LogsAggregatorTest.php +++ b/tests/Logs/LogsAggregatorTest.php @@ -170,7 +170,7 @@ public function testAttributesAreAddedToLogMessage(): void $hub = new Hub($client); SentrySdk::setCurrentHub($hub); - $hub->configureScope(function (Scope $scope) { + $hub->configureScope(static function (Scope $scope) { $userDataBag = new UserDataBag(); $userDataBag->setId('unique_id'); $userDataBag->setEmail('foo@example.com'); diff --git a/tests/Logs/LogsTest.php b/tests/Logs/LogsTest.php index 3aab97419..4dd361b56 100644 --- a/tests/Logs/LogsTest.php +++ b/tests/Logs/LogsTest.php @@ -171,7 +171,7 @@ private function assertEvent(callable $assert, array $options = []): ClientInter $transport = $this->createMock(TransportInterface::class); $transport->expects($this->once()) ->method('send') - ->with($this->callback(function (Event $event) use ($assert): bool { + ->with($this->callback(static function (Event $event) use ($assert): bool { $assert($event); return true; diff --git a/tests/OptionResolverTest.php b/tests/OptionResolverTest.php index df8d9c470..176b2dd17 100644 --- a/tests/OptionResolverTest.php +++ b/tests/OptionResolverTest.php @@ -114,7 +114,7 @@ public function testNormalizerReturnsInvalidType() $resolver = new OptionsResolver(); $resolver->setDefaults(['foo' => 'bar']); $resolver->setAllowedTypes('foo', ['string']); - $resolver->setNormalizer('foo', function ($value) { + $resolver->setNormalizer('foo', static function ($value) { return 8; }); $result = $resolver->resolve(['foo' => 'test']); @@ -126,7 +126,7 @@ public function testNormalizerReturnsInvalidValue() $resolver = new OptionsResolver(); $resolver->setDefaults(['foo' => 'b']); $resolver->setAllowedValues('foo', ['a', 'b', 'c']); - $resolver->setNormalizer('foo', function ($value) { + $resolver->setNormalizer('foo', static function ($value) { return 'z'; }); $result = $resolver->resolve(['foo' => 'a']); @@ -138,7 +138,7 @@ public function testNormalizerResultFailsValidation() $resolver = new OptionsResolver(); $resolver->setDefaults(['foo' => 'b']); $resolver->setAllowedValues('foo', ['a', 'b', 'c']); - $resolver->setNormalizer('foo', function ($value) { + $resolver->setNormalizer('foo', static function ($value) { return false; }); $result = $resolver->resolve(['foo' => 'a']); @@ -237,7 +237,7 @@ public function allowedValueTestProvider(): \Generator yield 'Callback validates successfully' => [ ['count' => 50], ['count' => 10], - ['count' => function ($value) { + ['count' => static function ($value) { return $value >= 0 && $value <= 100; }], ['count' => 10], @@ -246,7 +246,7 @@ public function allowedValueTestProvider(): \Generator yield 'Callback validation fails' => [ ['count' => 50], ['count' => 200], - ['count' => function ($value) { + ['count' => static function ($value) { return $value >= 0 && $value <= 100; }], ['count' => 50], @@ -281,7 +281,7 @@ public function normalizerTestProvider(): \Generator yield 'Normalizes successful' => [ ['a' => 'b'], ['a' => ' c '], - ['a' => function ($value) { + ['a' => static function ($value) { return trim($value); }], ['a' => 'c'], diff --git a/tests/Serializer/AbstractSerializerTest.php b/tests/Serializer/AbstractSerializerTest.php index 752e7e44b..a40479ad7 100644 --- a/tests/Serializer/AbstractSerializerTest.php +++ b/tests/Serializer/AbstractSerializerTest.php @@ -438,7 +438,7 @@ public function serializableCallableProvider(): array return [ [ - 'callable' => function (array $param1) { + 'callable' => static function (array $param1) { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames @@ -446,7 +446,7 @@ public function serializableCallableProvider(): array : 'Lambda ' . __NAMESPACE__ . '\\{closure} [array param1]', ], [ - 'callable' => function ($param1a) { + 'callable' => static function ($param1a) { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames @@ -454,7 +454,7 @@ public function serializableCallableProvider(): array : 'Lambda ' . __NAMESPACE__ . '\\{closure} [mixed|null param1a]', ], [ - 'callable' => function (callable $param1c) { + 'callable' => static function (callable $param1c) { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames @@ -462,7 +462,7 @@ public function serializableCallableProvider(): array : 'Lambda ' . __NAMESPACE__ . '\\{closure} [callable param1c]', ], [ - 'callable' => function (\stdClass $param1d) { + 'callable' => static function (\stdClass $param1d) { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames @@ -470,7 +470,7 @@ public function serializableCallableProvider(): array : 'Lambda ' . __NAMESPACE__ . '\\{closure} [stdClass param1d]', ], [ - 'callable' => function (?\stdClass $param1e = null) { + 'callable' => static function (?\stdClass $param1e = null) { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames @@ -478,7 +478,7 @@ public function serializableCallableProvider(): array : 'Lambda ' . __NAMESPACE__ . '\\{closure} [stdClass|null [param1e]]', ], [ - 'callable' => function (array &$param1f) { + 'callable' => static function (array &$param1f) { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames @@ -486,7 +486,7 @@ public function serializableCallableProvider(): array : 'Lambda ' . __NAMESPACE__ . '\\{closure} [array ¶m1f]', ], [ - 'callable' => function (?array &$param1g = null) { + 'callable' => static function (?array &$param1g = null) { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames @@ -514,7 +514,7 @@ public function serializableCallableProvider(): array 'expected' => 'Callable void ' . SerializerTestObject::class . '::testy []', ], [ - 'callable' => function (int $param1_70a) { + 'callable' => static function (int $param1_70a) { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames @@ -522,7 +522,7 @@ public function serializableCallableProvider(): array : 'Lambda ' . __NAMESPACE__ . '\\{closure} [int param1_70a]', ], [ - 'callable' => function (&$param): int { + 'callable' => static function (&$param): int { return (int) $param; }, 'expected' => $prettyClosureNames @@ -530,7 +530,7 @@ public function serializableCallableProvider(): array : 'Lambda int ' . __NAMESPACE__ . '\\{closure} [mixed|null ¶m]', ], [ - 'callable' => function (int $param): ?int { + 'callable' => static function (int $param): ?int { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames @@ -538,7 +538,7 @@ public function serializableCallableProvider(): array : 'Lambda int ' . __NAMESPACE__ . '\\{closure} [int param]', ], [ - 'callable' => function (?int $param1_70b) { + 'callable' => static function (?int $param1_70b) { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames @@ -546,7 +546,7 @@ public function serializableCallableProvider(): array : 'Lambda ' . __NAMESPACE__ . '\\{closure} [int|null param1_70b]', ], [ - 'callable' => function (?int $param1_70c): void { + 'callable' => static function (?int $param1_70c): void { throw new \Exception('Don\'t even think about invoke me'); }, 'expected' => $prettyClosureNames diff --git a/tests/State/HubTest.php b/tests/State/HubTest.php index 0b8c2286e..54c5a9dfa 100644 --- a/tests/State/HubTest.php +++ b/tests/State/HubTest.php @@ -241,7 +241,7 @@ public function testCaptureMessage(array $functionCallArgs, array $expectedFunct $eventId = EventId::generate(); $hub = new Hub(new NoOpClient()); - $hub->configureScope(function (Scope $scope) use ($propagationContext): void { + $hub->configureScope(static function (Scope $scope) use ($propagationContext): void { $scope->setPropagationContext($propagationContext); }); @@ -300,7 +300,7 @@ public function testCaptureException(array $functionCallArgs, array $expectedFun $eventId = EventId::generate(); $hub = new Hub(new NoOpClient()); - $hub->configureScope(function (Scope $scope) use ($propagationContext): void { + $hub->configureScope(static function (Scope $scope) use ($propagationContext): void { $scope->setPropagationContext($propagationContext); }); @@ -355,7 +355,7 @@ public function testCaptureLastError(array $functionCallArgs, array $expectedFun $eventId = EventId::generate(); $hub = new Hub(new NoOpClient()); - $hub->configureScope(function (Scope $scope) use ($propagationContext): void { + $hub->configureScope(static function (Scope $scope) use ($propagationContext): void { $scope->setPropagationContext($propagationContext); }); @@ -814,7 +814,7 @@ public function testStartTransactionUpdatesTheDscSampleRate(): void $client->expects($this->once()) ->method('getOptions') ->willReturn(new Options([ - 'traces_sampler' => function (SamplingContext $samplingContext): float { + 'traces_sampler' => static function (SamplingContext $samplingContext): float { return 1.0; }, ])); diff --git a/tests/State/ScopeTest.php b/tests/State/ScopeTest.php index 20b90e928..b5f96f7dc 100644 --- a/tests/State/ScopeTest.php +++ b/tests/State/ScopeTest.php @@ -406,7 +406,7 @@ public function testAddEventProcessor(): void return null; }); - $scope->addEventProcessor(function () use (&$callback3Called) { + $scope->addEventProcessor(static function () use (&$callback3Called) { $callback3Called = true; return null; @@ -426,7 +426,7 @@ public function testEventProcessorReceivesTheEventAndEventHint(): void $processorCalled = false; $processorReceivedHint = null; - $scope->addEventProcessor(function (Event $eventArg, EventHint $hint) use (&$processorCalled, &$processorReceivedHint): ?Event { + $scope->addEventProcessor(static function (Event $eventArg, EventHint $hint) use (&$processorCalled, &$processorReceivedHint): ?Event { $processorCalled = true; $processorReceivedHint = $hint; diff --git a/tests/Transport/HttpTransportTest.php b/tests/Transport/HttpTransportTest.php index 1d34137ae..206261eb0 100644 --- a/tests/Transport/HttpTransportTest.php +++ b/tests/Transport/HttpTransportTest.php @@ -62,7 +62,7 @@ public function testSend(Response $response, ResultStatus $expectedResultStatus, $this->logger->expects($this->exactly(\count($messages))) ->method($level) ->with($this->logicalOr( - ...array_map(function (string $message) { + ...array_map(static function (string $message) { return new StringMatchesFormatDescription($message); }, $messages) ));