Skip to content

Commit

Permalink
chore(psalm): fix psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jun 1, 2024
1 parent 41c4a76 commit 0403cb3
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 45 deletions.
14 changes: 0 additions & 14 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,4 @@
<code><![CDATA[getDescriptorByClassName]]></code>
</MixedMethodCall>
</file>
<file src="src/functions.php">
<MixedArrayAccess>
<code><![CDATA[AbstractCloner::$defaultCasters[EnumValue::class]]]></code>
<code><![CDATA[AbstractCloner::$defaultCasters[MapField::class]]]></code>
<code><![CDATA[AbstractCloner::$defaultCasters[Message::class]]]></code>
<code><![CDATA[AbstractCloner::$defaultCasters[RepeatedField::class]]]></code>
</MixedArrayAccess>
<MixedArrayAssignment>
<code><![CDATA[AbstractCloner::$defaultCasters[EnumValue::class]]]></code>
<code><![CDATA[AbstractCloner::$defaultCasters[MapField::class]]]></code>
<code><![CDATA[AbstractCloner::$defaultCasters[Message::class]]]></code>
<code><![CDATA[AbstractCloner::$defaultCasters[RepeatedField::class]]]></code>
</MixedArrayAssignment>
</file>
</files>
1 change: 1 addition & 0 deletions src/Client/TrapHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static function fromArray(array $array): self
*
* @param int<0, max> $number The tick number.
* @param float $delta The time delta between the current and previous tick.
* @param int<0, max> $memory The memory usage.
*
* @internal
*/
Expand Down
18 changes: 13 additions & 5 deletions src/Client/TrapHandle/StackTrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,19 @@ public static function stackTrace(string $baseDir = '', bool $provideObjects = f
$cwdLen = \strlen($dir);
$stack = [];
$internal = false;
foreach (
\debug_backtrace(
($provideObjects ? \DEBUG_BACKTRACE_PROVIDE_OBJECT : 0) | \DEBUG_BACKTRACE_IGNORE_ARGS,
) as $frame
) {

/** @var array{
* function: non-empty-string,
* line?: int,
* file?: string,
* class?: class-string,
* type?: string,
* object?: object,
* args?: list<mixed>
* } $frame */
foreach (\debug_backtrace(
($provideObjects ? \DEBUG_BACKTRACE_PROVIDE_OBJECT : 0) | \DEBUG_BACKTRACE_IGNORE_ARGS,
) as $frame) {
$class = $frame['class'] ?? '';
if (\str_starts_with($class, 'Buggregator\\Trap\\Client\\')) {
$internal = true;
Expand Down
2 changes: 2 additions & 0 deletions src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public function createRegistry(OutputInterface $output): Sender\SenderRegistry
public function getSubscribedSignals(): array
{
$result = [];
/** @psalm-suppress MixedAssignment */
\defined('SIGINT') and $result[] = \SIGINT;
/** @psalm-suppress MixedAssignment */
\defined('SIGTERM') and $result[] = \SIGTERM;

return $result;
Expand Down
1 change: 1 addition & 0 deletions src/Config/Server/Files/ObserverConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ abstract class ObserverConfig
*/
public function isValid(): bool

Check warning on line 27 in src/Config/Server/Files/ObserverConfig.php

View check run for this annotation

Codecov / codecov/patch

src/Config/Server/Files/ObserverConfig.php#L27

Added line #L27 was not covered by tests
{
/** @psalm-suppress RedundantCondition */
return $this->path !== null && $this->converterClass !== null && $this->path !== ''
&& \is_a($this->converterClass, FrameConverter::class, true) && $this->scanInterval > 0.0;

Check warning on line 31 in src/Config/Server/Files/ObserverConfig.php

View check run for this annotation

Codecov / codecov/patch

src/Config/Server/Files/ObserverConfig.php#L30-L31

Added lines #L30 - L31 were not covered by tests
}
Expand Down
4 changes: 4 additions & 0 deletions src/Proto/Frame/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Buggregator\Trap\Support\Json;

/**
* @psalm-import-type Calls from \Buggregator\Trap\Proto\Frame\Profiler\Payload
* @psalm-import-type Metadata from \Buggregator\Trap\Proto\Frame\Profiler\Payload
*
* @internal
* @psalm-internal Buggregator
*/
Expand All @@ -23,6 +26,7 @@ public function __construct(

public static function fromString(string $payload, \DateTimeImmutable $time): static

Check warning on line 27 in src/Proto/Frame/Profiler.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler.php#L27

Added line #L27 was not covered by tests
{
/** @var array{type: non-empty-string}&Calls&Metadata $data */
$data = Json::decode($payload);

Check warning on line 30 in src/Proto/Frame/Profiler.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler.php#L30

Added line #L30 was not covered by tests

return new self(Frame\Profiler\Payload::fromArray($data), $time);

Check warning on line 32 in src/Proto/Frame/Profiler.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler.php#L32

Added line #L32 was not covered by tests
Expand Down
19 changes: 15 additions & 4 deletions src/Proto/Frame/Profiler/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @internal
* @psalm-internal Buggregator
*/
class Payload implements \JsonSerializable
final class Payload implements \JsonSerializable
{
/**
* @param PayloadType $type
Expand All @@ -46,17 +46,25 @@ public static function new(
array $metadata,
\Closure $callsProvider,
): self {
return new static($type, $metadata, $callsProvider);
return new self($type, $metadata, $callsProvider);

Check warning on line 49 in src/Proto/Frame/Profiler/Payload.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler/Payload.php#L49

Added line #L49 was not covered by tests
}

/**
* @param array{type: non-empty-string}&Calls&Metadata $data
* @param PayloadType|null $type
*/
public static function fromArray(array $data, ?Type $type = null): static

Check warning on line 56 in src/Proto/Frame/Profiler/Payload.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler/Payload.php#L56

Added line #L56 was not covered by tests
{
$metadata = $data;
unset($metadata['edges'], $metadata['peaks']);

Check warning on line 59 in src/Proto/Frame/Profiler/Payload.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler/Payload.php#L58-L59

Added lines #L58 - L59 were not covered by tests
return new static(

/** @var \Closure(): Calls $provider */
$provider = static fn(): array => $data;

Check warning on line 62 in src/Proto/Frame/Profiler/Payload.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler/Payload.php#L62

Added line #L62 was not covered by tests

return new self(
$type ?? PayloadType::from($data['type']),
$metadata,
static fn(): array => $data,
$provider,

Check warning on line 67 in src/Proto/Frame/Profiler/Payload.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler/Payload.php#L64-L67

Added lines #L64 - L67 were not covered by tests
);
}

Expand All @@ -76,6 +84,9 @@ public function getMetadata(): array
return $this->metadata;

Check warning on line 84 in src/Proto/Frame/Profiler/Payload.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler/Payload.php#L84

Added line #L84 was not covered by tests
}

/**
* @return array{type: non-empty-string}&Calls&Metadata
*/
public function toArray(): array

Check warning on line 90 in src/Proto/Frame/Profiler/Payload.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler/Payload.php#L90

Added line #L90 was not covered by tests
{
return ['type' => $this->type->value] + $this->getCalls() + $this->getMetadata();

Check warning on line 92 in src/Proto/Frame/Profiler/Payload.php

View check run for this annotation

Codecov / codecov/patch

src/Proto/Frame/Profiler/Payload.php#L92

Added line #L92 was not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion src/Sender/ConsoleSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function create(OutputInterface $output): self
$renderer->register(new Renderer\Http());
$renderer->register(new Renderer\Profiler());

Check warning on line 44 in src/Sender/ConsoleSender.php

View check run for this annotation

Codecov / codecov/patch

src/Sender/ConsoleSender.php#L44

Added line #L44 was not covered by tests
$renderer->register(new Renderer\Binary());
$renderer->register(new Renderer\Plain($templateRenderer));
$renderer->register(new Renderer\Plain());

Check warning on line 46 in src/Sender/ConsoleSender.php

View check run for this annotation

Codecov / codecov/patch

src/Sender/ConsoleSender.php#L46

Added line #L46 was not covered by tests

return new self($renderer);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Service/Config/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ private function injectValue(object $config, \ReflectionProperty $property, arra

/** @var mixed $value */
$value = match (true) {
$attribute instanceof XPath => (static fn(?array $value, int $key): mixed => match (true) {
\array_key_exists($key, $value) => $value[$key],
default => null,
})($this->xml?->xpath($attribute->path), $attribute->key),
$attribute instanceof XPath => (static fn(array|false|null $value, int $key): mixed
=> \is_array($value) && \array_key_exists($key, $value)
? $value[$key]
: null)($this->xml?->xpath($attribute->path), $attribute->key),
$attribute instanceof Env => $this->env[$attribute->name] ?? null,
$attribute instanceof InputOption => $this->inputOptions[$attribute->name] ?? null,
$attribute instanceof InputArgument => $this->inputArguments[$attribute->name] ?? null,
Expand Down
15 changes: 15 additions & 0 deletions src/Service/FilesObserver/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
*/
final class FileInfo
{
/**
* @param non-empty-string $path
* @param int<0, max> $size
* @param int<0, max> $ctime
* @param int<0, max> $mtime
*/
public function __construct(

Check warning on line 18 in src/Service/FilesObserver/FileInfo.php

View check run for this annotation

Codecov / codecov/patch

src/Service/FilesObserver/FileInfo.php#L18

Added line #L18 was not covered by tests
public readonly string $path,
public readonly int $size,
Expand All @@ -18,6 +24,7 @@ public function __construct(

public static function fromSplFileInfo(\SplFileInfo $fileInfo): self

Check warning on line 25 in src/Service/FilesObserver/FileInfo.php

View check run for this annotation

Codecov / codecov/patch

src/Service/FilesObserver/FileInfo.php#L25

Added line #L25 was not covered by tests
{
/** @psalm-suppress ArgumentTypeCoercion */
return new self(
$fileInfo->getRealPath(),
$fileInfo->getSize(),
Expand All @@ -26,6 +33,14 @@ public static function fromSplFileInfo(\SplFileInfo $fileInfo): self
);
}

/**
* @param array{
* path: non-empty-string,
* size: int<0, max>,
* ctime: int<0, max>,
* mtime: int<0, max>
* } $data
*/
public static function fromArray(array $data): self

Check warning on line 44 in src/Service/FilesObserver/FileInfo.php

View check run for this annotation

Codecov / codecov/patch

src/Service/FilesObserver/FileInfo.php#L44

Added line #L44 was not covered by tests
{
return new self(
Expand Down
3 changes: 1 addition & 2 deletions src/Service/FilesObserver/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private function syncFiles(): array

foreach ($files as $info) {
$path = $info->path;
if (!\is_string($path) || \array_key_exists($path, $this->cache)) {
if (\array_key_exists($path, $this->cache)) {
$newState[$path] = $this->cache[$path];
continue;

Check warning on line 74 in src/Service/FilesObserver/Handler.php

View check run for this annotation

Codecov / codecov/patch

src/Service/FilesObserver/Handler.php#L70-L74

Added lines #L70 - L74 were not covered by tests
}
Expand Down Expand Up @@ -102,7 +102,6 @@ private function getFiles(): \Traversable
} catch (\Throwable $e) {
$this->logger->info('Failed to read files from path `%s`', $this->path);
$this->logger->exception($e);

Check warning on line 104 in src/Service/FilesObserver/Handler.php

View check run for this annotation

Codecov / codecov/patch

src/Service/FilesObserver/Handler.php#L102-L104

Added lines #L102 - L104 were not covered by tests
return [];
}
}
}
8 changes: 6 additions & 2 deletions src/Socket/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public function process(): void
*/
public function setOnPayload(callable $callable): void
{
$this->onPayload = @\Closure::bind($callable(...), $this) ?? $callable(...);
$closure = $callable(...);

Check warning on line 115 in src/Socket/Client.php

View check run for this annotation

Codecov / codecov/patch

src/Socket/Client.php#L115

Added line #L115 was not covered by tests
/** @psalm-suppress PossiblyNullPropertyAssignmentValue, InvalidArgument */
$this->onPayload = @\Closure::bind($closure, $this) ?? $closure;

Check warning on line 117 in src/Socket/Client.php

View check run for this annotation

Codecov / codecov/patch

src/Socket/Client.php#L117

Added line #L117 was not covered by tests
}

/**
Expand All @@ -121,7 +123,9 @@ public function setOnPayload(callable $callable): void
*/
public function setOnClose(callable $callable): void
{
$this->onClose = @\Closure::bind($callable(...), $this) ?? $callable(...);
$closure = $callable(...);

Check warning on line 126 in src/Socket/Client.php

View check run for this annotation

Codecov / codecov/patch

src/Socket/Client.php#L126

Added line #L126 was not covered by tests
/** @psalm-suppress PossiblyNullPropertyAssignmentValue, InvalidArgument */
$this->onClose = @\Closure::bind($closure, $this) ?? $closure;

Check warning on line 128 in src/Socket/Client.php

View check run for this annotation

Codecov / codecov/patch

src/Socket/Client.php#L128

Added line #L128 was not covered by tests
}

public function send(string $payload): void
Expand Down
27 changes: 14 additions & 13 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ function tr(mixed ...$values): mixed
$mem = $time = \microtime(true);
try {
if ($values === []) {
/** @var int<0, max> $memory */
$memory = \memory_get_usage();

Check warning on line 58 in src/functions.php

View check run for this annotation

Codecov / codecov/patch

src/functions.php#L58

Added line #L58 was not covered by tests
/** @psalm-suppress InternalMethod */
return TrapHandle::fromTicker(
$counter,
$counter === 0 ? 0 : $mem - $previous,
\memory_get_usage(),
$memory,

Check warning on line 63 in src/functions.php

View check run for this annotation

Codecov / codecov/patch

src/functions.php#L63

Added line #L63 was not covered by tests
)->return();
}

Expand Down Expand Up @@ -95,16 +97,15 @@ function td(mixed ...$values): never
* Register the var-dump caster for protobuf messages
*/
if (\class_exists(AbstractCloner::class)) {
/** @psalm-suppress MixedAssignment */
AbstractCloner::$defaultCasters[Message::class] ??= [ProtobufCaster::class, 'cast'];
/** @psalm-suppress MixedAssignment */
AbstractCloner::$defaultCasters[RepeatedField::class] ??= [ProtobufCaster::class, 'castRepeated'];
/** @psalm-suppress MixedAssignment */
AbstractCloner::$defaultCasters[MapField::class] ??= [ProtobufCaster::class, 'castMap'];
/** @psalm-suppress MixedAssignment */
AbstractCloner::$defaultCasters[EnumValue::class] ??= [ProtobufCaster::class, 'castEnum'];
/** @psalm-suppress MixedAssignment */
AbstractCloner::$defaultCasters[Trace::class] = [TraceCaster::class, 'cast'];
/** @psalm-suppress MixedAssignment */
AbstractCloner::$defaultCasters[TraceFile::class] = [TraceCaster::class, 'castLine'];
/** @var array<class-string, callable> $casters */
$casters = &AbstractCloner::$defaultCasters;

Check failure on line 101 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

UnsupportedPropertyReferenceUsage

src/functions.php:101:5: UnsupportedPropertyReferenceUsage: This reference cannot be analyzed by Psalm. (see https://psalm.dev/321)

Check failure on line 101 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

UnsupportedPropertyReferenceUsage

src/functions.php:101:5: UnsupportedPropertyReferenceUsage: This reference cannot be analyzed by Psalm. (see https://psalm.dev/321)

$casters[Message::class] ??= [ProtobufCaster::class, 'cast'];

Check failure on line 103 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAccess

src/functions.php:103:5: MixedArrayAccess: Cannot access array value on mixed variable $casters (see https://psalm.dev/051)

Check failure on line 103 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedAssignment

src/functions.php:103:5: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 103 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAssignment

src/functions.php:103:5: MixedArrayAssignment: Cannot access array value on mixed variable $casters['Google\Protobuf\Internal\Message'] (see https://psalm.dev/117)

Check failure on line 103 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAccess

src/functions.php:103:5: MixedArrayAccess: Cannot access array value on mixed variable $casters (see https://psalm.dev/051)

Check failure on line 103 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedAssignment

src/functions.php:103:5: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 103 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAssignment

src/functions.php:103:5: MixedArrayAssignment: Cannot access array value on mixed variable $casters['Google\Protobuf\Internal\Message'] (see https://psalm.dev/117)
$casters[RepeatedField::class] ??= [ProtobufCaster::class, 'castRepeated'];

Check failure on line 104 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAccess

src/functions.php:104:5: MixedArrayAccess: Cannot access array value on mixed variable $casters (see https://psalm.dev/051)

Check failure on line 104 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedAssignment

src/functions.php:104:5: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 104 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAssignment

src/functions.php:104:5: MixedArrayAssignment: Cannot access array value on mixed variable $casters['Google\Protobuf\Internal\RepeatedField'] (see https://psalm.dev/117)

Check failure on line 104 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAccess

src/functions.php:104:5: MixedArrayAccess: Cannot access array value on mixed variable $casters (see https://psalm.dev/051)

Check failure on line 104 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedAssignment

src/functions.php:104:5: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 104 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAssignment

src/functions.php:104:5: MixedArrayAssignment: Cannot access array value on mixed variable $casters['Google\Protobuf\Internal\RepeatedField'] (see https://psalm.dev/117)
$casters[MapField::class] ??= [ProtobufCaster::class, 'castMap'];

Check failure on line 105 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAccess

src/functions.php:105:5: MixedArrayAccess: Cannot access array value on mixed variable $casters (see https://psalm.dev/051)

Check failure on line 105 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedAssignment

src/functions.php:105:5: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 105 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAssignment

src/functions.php:105:5: MixedArrayAssignment: Cannot access array value on mixed variable $casters['Google\Protobuf\Internal\MapField'] (see https://psalm.dev/117)

Check failure on line 105 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAccess

src/functions.php:105:5: MixedArrayAccess: Cannot access array value on mixed variable $casters (see https://psalm.dev/051)

Check failure on line 105 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedAssignment

src/functions.php:105:5: MixedAssignment: Unable to determine the type of this assignment (see https://psalm.dev/032)

Check failure on line 105 in src/functions.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

MixedArrayAssignment

src/functions.php:105:5: MixedArrayAssignment: Cannot access array value on mixed variable $casters['Google\Protobuf\Internal\MapField'] (see https://psalm.dev/117)
$casters[EnumValue::class] ??= [ProtobufCaster::class, 'castEnum'];
$casters[Trace::class] = [TraceCaster::class, 'cast'];
$casters[TraceFile::class] = [TraceCaster::class, 'castLine'];

unset($casters);
}

0 comments on commit 0403cb3

Please sign in to comment.