Skip to content

Commit

Permalink
phpstan level 7
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Jan 5, 2024
1 parent 26530bb commit 508127e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 20 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"open-telemetry/exporter-otlp": "^1.0",
"open-telemetry/sdk": "^1.0",
"open-telemetry/sem-conv": "^1.23",
"spatie/laravel-package-tools": "^1.16"
"spatie/laravel-package-tools": "^1.16",
"thecodingmachine/safe": "^2.0"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.3",
Expand All @@ -42,7 +43,8 @@
"phpstan/phpstan": "^1.10.51",
"phpstan/phpstan-deprecation-rules": "^1.1",
"spatie/test-time": "^1.3",
"spatie/valuestore": "^1.3"
"spatie/valuestore": "^1.3",
"thecodingmachine/phpstan-safe-rule": "^1.2"
},
"suggest": {
"open-telemetry/extension-propagator-b3": "Required to use B3 propagator",
Expand Down
17 changes: 9 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
parameters:
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
checkOctaneCompatibility: true
checkPhpDocMissingReturn: true

level: 7
paths:
- src
- config
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
treatPhpDocTypesAsCertain: false

level: 6

# ignoreErrors:
ignoreErrors:
13 changes: 5 additions & 8 deletions src/Instrumentation/CacheInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,25 @@ public function recordCacheHit(CacheHit $event): void
{
$this->addEvent('cache hit', [
'key' => $event->key,
'tags' => json_encode($event->tags),
'tags' => \Safe\json_encode($event->tags),
]);
}

public function recordCacheMiss(CacheMissed $event): void
{
$this->addEvent('cache miss', [
'key' => $event->key,
'tags' => json_encode($event->tags),
'tags' => \Safe\json_encode($event->tags),
]);
}

/** @psalm-suppress UndefinedPropertyFetch */
public function recordCacheSet(KeyWritten $event): void
{
$ttl = property_exists($event, 'minutes')
? $event->minutes * 60
: $event->seconds;
$ttl = $event->seconds ?? 0;

$this->addEvent('cache set', [
'key' => $event->key,
'tags' => json_encode($event->tags),
'tags' => \Safe\json_encode($event->tags),
'expires_at' => $ttl > 0 ? now()->addSeconds($ttl)->getTimestamp() : 'never',
'expires_in_seconds' => $ttl > 0 ? $ttl : 'never',
'expires_in_human' => $ttl > 0 ? now()->addSeconds($ttl)->diffForHumans() : 'never',
Expand All @@ -55,7 +52,7 @@ public function recordCacheForget(KeyForgotten $event): void
{
$this->addEvent('cache forget', [
'key' => $event->key,
'tags' => json_encode($event->tags),
'tags' => \Safe\json_encode($event->tags),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Instrumentation/RedisInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private function formatCommand(string $command, array $parameters): string
if (is_array($parameter)) {
return collect($parameter)->map(function ($value, $key) {
if (is_array($value)) {
return json_encode($value);
return \Safe\json_encode($value);
}

return is_int($key) ? $value : sprintf('%s %s', $key, $value);
Expand Down
1 change: 1 addition & 0 deletions src/LaravelOpenTelemetryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected function initTracer(): void
),
),
'http' => new OtlpSpanExporter(
// @phpstan-ignore-next-line
(new OtlpHttpTransportFactory())->create(
(new HttpEndpointResolver())->resolveToString(config('opentelemetry.exporters.http.endpoint'), Signals::TRACE),
'application/x-protobuf'
Expand Down
2 changes: 1 addition & 1 deletion src/Support/HttpServer/TraceRequestMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function startTracing(Request $request): SpanInterface
protected function recordHttpResponseToSpan(SpanInterface $span, Response $response): void
{
$span->setAttribute(TraceAttributes::HTTP_RESPONSE_STATUS_CODE, $response->getStatusCode())
->setAttribute(TraceAttributes::HTTP_RESPONSE_BODY_SIZE, strlen($response->getContent()));
->setAttribute(TraceAttributes::HTTP_RESPONSE_BODY_SIZE, strlen($response->getContent() ?: ''));

if ($response->isSuccessful()) {
$span->setStatus(StatusCode::STATUS_OK);
Expand Down

0 comments on commit 508127e

Please sign in to comment.