Skip to content
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

Psalm 1 #267

Merged
merged 31 commits into from
Dec 21, 2024
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
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand Down
18 changes: 18 additions & 0 deletions src/Collector/Console/CommandCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
use Yiisoft\Yii\Debug\Collector\TimelineCollector;

use function array_key_exists;

final class CommandCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand All @@ -23,6 +25,19 @@
* Let -1 mean that it was not set during the process.
*/
private const UNDEFINED_EXIT_CODE = -1;

/**
* @psalm-var array<string, array{
* name: string,
* command: Command|null,
* input: string|null,
* output: string|null,
* error?: string,
* exitCode?: int,
* arguments?: array,
* options?: array,
* }>
*/
private array $commands = [];

public function __construct(
Expand All @@ -41,7 +56,7 @@
return;
}

$this->timelineCollector->collect($this, spl_object_id($event));

Check warning on line 59 in src/Collector/Console/CommandCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ if (!$this->isActive()) { return; } - $this->timelineCollector->collect($this, spl_object_id($event)); + $command = $event->getCommand(); if ($event instanceof ConsoleErrorEvent) { $this->commands[$event::class] = ['name' => $event->getInput()->getFirstArgument() ?? '', 'command' => $command, 'input' => $this->castInputToString($event->getInput()), 'output' => $this->fetchOutput($event->getOutput()), 'error' => $event->getError()->getMessage(), 'exitCode' => $event->getExitCode()];

$command = $event->getCommand();

Expand All @@ -60,7 +75,7 @@

if ($event instanceof ConsoleTerminateEvent) {
$this->commands[$event::class] = [
'name' => $command?->getName() ?? $event->getInput()->getFirstArgument() ?? '',

Check warning on line 78 in src/Collector/Console/CommandCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Coalesce": --- Original +++ New @@ @@ return; } if ($event instanceof ConsoleTerminateEvent) { - $this->commands[$event::class] = ['name' => $command?->getName() ?? $event->getInput()->getFirstArgument() ?? '', 'command' => $command, 'input' => $this->castInputToString($event->getInput()), 'output' => $this->fetchOutput($event->getOutput()), 'exitCode' => $event->getExitCode()]; + $this->commands[$event::class] = ['name' => $event->getInput()->getFirstArgument() ?? $command?->getName() ?? '', 'command' => $command, 'input' => $this->castInputToString($event->getInput()), 'output' => $this->fetchOutput($event->getOutput()), 'exitCode' => $event->getExitCode()]; return; } $definition = $command?->getDefinition();
'command' => $command,
'input' => $this->castInputToString($event->getInput()),
'output' => $this->fetchOutput($event->getOutput()),
Expand All @@ -71,7 +86,7 @@

$definition = $command?->getDefinition();
$this->commands[$event::class] = [
'name' => $command?->getName() ?? $event->getInput()->getFirstArgument() ?? '',

Check warning on line 89 in src/Collector/Console/CommandCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Coalesce": --- Original +++ New @@ @@ return; } $definition = $command?->getDefinition(); - $this->commands[$event::class] = ['name' => $command?->getName() ?? $event->getInput()->getFirstArgument() ?? '', 'command' => $command, 'input' => $this->castInputToString($event->getInput()), 'output' => $this->fetchOutput($event->getOutput()), 'arguments' => $definition?->getArguments() ?? [], 'options' => $definition?->getOptions() ?? []]; + $this->commands[$event::class] = ['name' => $event->getInput()->getFirstArgument() ?? $command?->getName() ?? '', 'command' => $command, 'input' => $this->castInputToString($event->getInput()), 'output' => $this->fetchOutput($event->getOutput()), 'arguments' => $definition?->getArguments() ?? [], 'options' => $definition?->getOptions() ?? []]; } public function getSummary() : array {
'command' => $command,
'input' => $this->castInputToString($event->getInput()),
'output' => $this->fetchOutput($event->getOutput()),
Expand Down Expand Up @@ -103,7 +118,7 @@
}

return [
'command' => [

Check warning on line 121 in src/Collector/Console/CommandCollector.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ if ($commandEvent === null) { return []; } - return ['command' => ['name' => $commandEvent['name'], 'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null, 'input' => $commandEvent['input'], 'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE]]; + return ['command' => ['class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null, 'input' => $commandEvent['input'], 'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE]]; } private function reset() : void {
'name' => $commandEvent['name'],
'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null,
'input' => $commandEvent['input'],
Expand All @@ -127,6 +142,9 @@
return method_exists($input, '__toString') ? $input->__toString() : null;
}

/**
* @return string[]
*/
private function getSupportedEvents(): array
{
return [
Expand Down
18 changes: 16 additions & 2 deletions src/Collector/ContainerInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Yiisoft\Proxy\ObjectProxy;
use Yiisoft\Proxy\ProxyManager;
use Yiisoft\Proxy\ProxyTrait;
use Yiisoft\Yii\Debug\ProxyDecoratedCalls;
Expand All @@ -30,6 +31,9 @@ final class ContainerInterfaceProxy implements ContainerInterface

private array $decoratedServices = [];

/**
* @psalm-var array<string, object>
*/
private array $serviceProxy = [];

public function __construct(
Expand All @@ -40,6 +44,9 @@ public function __construct(
$this->proxyManager = new ProxyManager($this->config->getProxyCachePath());
}

/**
* @psalm-param array<string, mixed> $decoratedServices
*/
public function withDecoratedServices(array $decoratedServices): self
{
$new = clone $this;
Expand Down Expand Up @@ -108,10 +115,12 @@ private function getServiceProxy(string $service, object $instance): ?object
}

if ($this->config->hasDecoratedServiceCallableConfig($service)) {
/** @psalm-suppress MixedArgument */
return $this->getServiceProxyFromCallable($this->config->getDecoratedServiceConfig($service), $instance);
}

if ($this->config->hasDecoratedServiceArrayConfigWithStringKeys($service)) {
/** @psalm-suppress MixedArgument */
return $this->getCommonMethodProxy(
interface_exists($service) || class_exists($service) ? $service : $instance::class,
$instance,
Expand All @@ -120,6 +129,7 @@ interface_exists($service) || class_exists($service) ? $service : $instance::cla
}

if ($this->config->hasDecoratedServiceArrayConfig($service)) {
/** @psalm-suppress MixedArgument */
return $this->getServiceProxyFromArray($instance, $this->config->getDecoratedServiceConfig($service));
}

Expand All @@ -130,6 +140,9 @@ interface_exists($service) || class_exists($service) ? $service : $instance::cla
return null;
}

/**
* @psalm-param callable(ContainerInterface, object):(object|null) $callback
*/
private function getServiceProxyFromCallable(callable $callback, object $instance): ?object
{
return $callback($this, $instance);
Expand All @@ -138,7 +151,7 @@ private function getServiceProxyFromCallable(callable $callback, object $instanc
/**
* @psalm-param class-string $service
*/
private function getCommonMethodProxy(string $service, object $instance, array $callbacks): ?object
private function getCommonMethodProxy(string $service, object $instance, array $callbacks): ObjectProxy
{
$methods = [];
foreach ($callbacks as $method => $callback) {
Expand All @@ -163,10 +176,11 @@ private function getServiceProxyFromArray(object $instance, array $params): ?obj
try {
$params[$index] = $this->get($param);
} catch (Exception) {
//leave as is
// leave as is
}
}
}
/** @psalm-suppress MixedMethodCall */
return new $proxyClass($instance, ...$params);
} catch (Exception) {
return null;
samdark marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
4 changes: 4 additions & 0 deletions src/Collector/ContainerProxyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\EventDispatcher\EventDispatcherInterface;

use function in_array;
use function is_array;
use function is_callable;

final class ContainerProxyConfig
Expand Down Expand Up @@ -64,6 +65,9 @@ public function withCollector(ServiceCollector $collector): self
return $config;
}

/**
* @psalm-param array<string, mixed> $decoratedServices
*/
public function withDecoratedServices(array $decoratedServices): self
{
$config = clone $this;
Expand Down
1 change: 1 addition & 0 deletions src/Collector/LoggerInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function debug(string|Stringable $message, array $context = []): void

public function log(mixed $level, string|Stringable $message, array $context = []): void
{
$level = (string) $level;
$callStack = $this->getCallStack();

$this->collector->collect($level, $message, $context, $callStack['file'] . ':' . $callStack['line']);
Expand Down
3 changes: 3 additions & 0 deletions src/Collector/ServiceMethodProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class ServiceMethodProxy extends ServiceProxy
public function __construct(
string $service,
object $instance,
/**
* @psalm-var array<string, callable>
*/
private readonly array $methods,
ContainerProxyConfig $config
) {
Expand Down
14 changes: 9 additions & 5 deletions src/Collector/Stream/FilesystemStreamCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;

use function count;

final class FilesystemStreamCollector implements SummaryCollectorInterface
{
use CollectorTrait;
Expand All @@ -17,23 +19,25 @@ public function __construct(
* Examples:
* - '/' . preg_quote('yii-debug/src/Dumper', '/') . '/'
* - '/ClosureExporter/'
*
* @var string[]
*/
private readonly array $ignoredPathPatterns = [],
/**
* @var string[]
*/
private readonly array $ignoredClasses = [],
) {
}

/**
* @var array[]
* @psalm-var array<string, list<array{path: string, args: array}>>
*/
private array $operations = [];

public function getCollected(): array
{
if (!$this->isActive()) {
return [];
}
return array_map('array_values', $this->operations);
return $this->isActive() ? $this->operations : [];
}

public function startup(): void
Expand Down
21 changes: 18 additions & 3 deletions src/Collector/Stream/FilesystemStreamProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use const SEEK_SET;

/**
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement
*/
final class FilesystemStreamProxy implements StreamWrapperInterface
{
public static bool $registered = false;
Expand All @@ -22,8 +25,20 @@ final class FilesystemStreamProxy implements StreamWrapperInterface
public bool $ignored = false;

public static ?FilesystemStreamCollector $collector = null;

/**
* @var string[]
*/
public static array $ignoredPathPatterns = [];

/**
* @var string[]
*/
public static array $ignoredClasses = [];

/**
* @psalm-var array<string, array{path: string, args: array}>
*/
public array $operations = [];

public function __construct()
Expand Down Expand Up @@ -104,7 +119,7 @@ public function stream_read(int $count): string|false
{
if (!$this->ignored) {
$this->operations['read'] = [
'path' => $this->decorated->filename,
'path' => $this->decorated->filename ?? '',
'args' => [],
];
}
Expand Down Expand Up @@ -155,7 +170,7 @@ public function dir_readdir(): false|string
{
if (!$this->ignored) {
$this->operations['readdir'] = [
'path' => $this->decorated->filename,
'path' => $this->decorated->filename ?? '',
'args' => [],
];
}
Expand Down Expand Up @@ -236,7 +251,7 @@ public function stream_write(string $data): int
{
if (!$this->ignored) {
$this->operations['write'] = [
'path' => $this->decorated->filename,
'path' => $this->decorated->filename ?? '',
'args' => [],
];
}
Expand Down
18 changes: 15 additions & 3 deletions src/Collector/Stream/HttpStreamCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,31 @@
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;

use function count;

final class HttpStreamCollector implements SummaryCollectorInterface
{
use CollectorTrait;

public function __construct(
/**
* @var string[]
*/
private readonly array $ignoredPathPatterns = [],
/**
* @var string[]
*/
private readonly array $ignoredClasses = [],
/**
* @var string[]
*/
private readonly array $ignoredUrls = []
) {
}

/**
* @psalm-var array<string, list<array{uri: string, args: array}>>
*/
private array $requests = [];

public function getCollected(): array
Expand Down Expand Up @@ -76,9 +90,7 @@ public function getSummary(): array
'http_stream' => array_merge(
...array_map(
fn (string $operation) => [
$operation => is_countable($this->requests[$operation]) ? count(
$this->requests[$operation]
) : 0,
$operation => count($this->requests[$operation]),
vjik marked this conversation as resolved.
Show resolved Hide resolved
],
array_keys($this->requests)
)
Expand Down
27 changes: 24 additions & 3 deletions src/Collector/Stream/HttpStreamProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,32 @@
use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapper;
use Yiisoft\Yii\Debug\Helper\StreamWrapper\StreamWrapperInterface;

use function func_get_args;
use function in_array;
use function stream_get_wrappers;

use const SEEK_SET;

/**
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement
*/
final class HttpStreamProxy implements StreamWrapperInterface
{
public static bool $registered = false;

/**
* @var string[]
*/
public static array $ignoredPathPatterns = [];

/**
* @var string[]
*/
public static array $ignoredClasses = [];

/**
* @var string[]
*/
public static array $ignoredUrls = [];
/**
* @var resource|null
Expand All @@ -27,6 +44,10 @@ final class HttpStreamProxy implements StreamWrapperInterface
public bool $ignored = false;

public static ?HttpStreamCollector $collector = null;

/**
* @psalm-var array<string, array{path: string, args: array}>
*/
public array $operations = [];

public function __construct()
Expand Down Expand Up @@ -120,7 +141,7 @@ public function stream_read(int $count): string|false
$headers = (array) ($context['http']['header'] ?? $context['https']['header'] ?? []);

$this->operations['read'] = [
'path' => $this->decorated->filename,
'path' => $this->decorated->filename ?? '',
'args' => [
'method' => $method,
'response_headers' => $metadata['wrapper_data'],
Expand Down Expand Up @@ -175,7 +196,7 @@ public function dir_readdir(): false|string
{
if (!$this->ignored) {
$this->operations[__FUNCTION__] = [
'path' => $this->decorated->filename,
'path' => $this->decorated->filename ?? '',
'args' => [],
];
}
Expand Down Expand Up @@ -256,7 +277,7 @@ public function stream_write(string $data): int
{
if (!$this->ignored) {
$this->operations['write'] = [
'path' => $this->decorated->filename,
'path' => $this->decorated->filename ?? '',
'args' => [],
];
}
Expand Down
9 changes: 5 additions & 4 deletions src/DebugServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ final class DebugServiceProvider implements ServiceProviderInterface
public function getDefinitions(): array
{
return [
ContainerInterface::class => static fn (ContainerInterface $container) => new ContainerInterfaceProxy(
$container,
$container->get(ContainerProxyConfig::class),
),
ContainerInterface::class =>
static fn (ContainerInterface $container, ContainerProxyConfig $config) => new ContainerInterfaceProxy(
$container,
$config,
),
];
}

Expand Down
Loading
Loading