Skip to content

Commit

Permalink
Psalm level 2 (#246)
Browse files Browse the repository at this point in the history
* 1

* 2

* 3

* 4

* 5

* 6

* 7

* 8

* 9

* 10

* add sort

* add sort

---------

Co-authored-by: Dmitriy Derepko <[email protected]>
  • Loading branch information
vjik and xepozz authored Mar 4, 2024
1 parent 6712de9 commit b79ba7f
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 45 deletions.
2 changes: 0 additions & 2 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use Yiisoft\Cache\CacheInterface;
use Yiisoft\Injector\Injector;
use Yiisoft\Yii\Debug\Collector\Console\CommandCollector;
use Yiisoft\Yii\Debug\Collector\Console\ConsoleAppInfoCollector;
Expand Down Expand Up @@ -62,7 +61,6 @@
LoggerInterface::class => [LoggerInterfaceProxy::class, LogCollector::class],
EventDispatcherInterface::class => [EventDispatcherInterfaceProxy::class, EventCollector::class],
ClientInterface::class => [HttpClientInterfaceProxy::class, HttpClientCollector::class],
CacheInterface::class,
],
'dumper.excludedClasses' => [
'PhpParser\\Parser\\Php7',
Expand Down
3 changes: 2 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
errorLevel="2"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -15,5 +15,6 @@
</projectFiles>
<issueHandlers>
<MixedAssignment errorLevel="suppress" />
<RiskyTruthyFalsyComparison errorLevel="suppress" />
</issueHandlers>
</psalm>
5 changes: 4 additions & 1 deletion src/Collector/ContainerProxyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

final class ContainerProxyConfig
{
/**
* @psalm-param array<string, mixed> $decoratedServices
*/
public function __construct(
private bool $active = false,
private array $decoratedServices = [],
Expand Down Expand Up @@ -99,7 +102,7 @@ public function getProxyCachePath(): ?string
return $this->proxyCachePath;
}

public function getDecoratedServiceConfig($service)
public function getDecoratedServiceConfig(string $service): mixed
{
return $this->decoratedServices[$service];
}
Expand Down
28 changes: 16 additions & 12 deletions src/Collector/HttpClientCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@

use function count;

/**
* @psalm-type RequestEntry = array{
* startTime: float,
* endTime: float,
* totalTime: float,
* method: string,
* uri: string,
* headers: string[][],
* line: string,
* responseRaw?: string,
* responseStatus?: int,
* }
*/
final class HttpClientCollector implements SummaryCollectorInterface
{
use CollectorTrait;

/**
* @psalm-var array<string, non-empty-list<array{
* startTime: float,
* endTime: float,
* totalTime: float,
* method: string,
* uri: string,
* headers: string[][],
* line: string,
* responseRaw?: string,
* responseStatus?: int,
* }>>
* @psalm-var array<string, non-empty-list<RequestEntry>>
*/
private array $requests = [];

Expand Down Expand Up @@ -57,9 +60,10 @@ public function collectTotalTime(?ResponseInterface $response, float $endTime, ?
return;
}

if (!isset($this->requests[$uniqueId]) || !is_array($this->requests[$uniqueId])) {
if (!isset($this->requests[$uniqueId])) {
return;
}
/** @psalm-suppress UnsupportedReferenceUsage */
$entry = &$this->requests[$uniqueId][count($this->requests[$uniqueId]) - 1];
if ($response instanceof ResponseInterface) {
$entry['responseRaw'] = Message::toString($response);
Expand Down
4 changes: 2 additions & 2 deletions src/Collector/LogCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LogCollector implements SummaryCollectorInterface

private array $messages = [];

public function __construct(private TimelineCollector $timelineCollector, )
public function __construct(private TimelineCollector $timelineCollector)
{
}

Expand All @@ -22,7 +22,7 @@ public function getCollected(): array
return $this->messages;
}

public function collect(string $level, $message, array $context, string $line): void
public function collect(string $level, mixed $message, array $context, string $line): void
{
if (!$this->isActive()) {
return;
Expand Down
11 changes: 7 additions & 4 deletions src/Collector/ProxyLogTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected function logProxy(
object $instance,
string $method,
array $arguments,
$result,
mixed $result,
float $timeStart
): void {
$error = $this->getCurrentError();
Expand All @@ -30,7 +30,10 @@ protected function logProxy(
}
}

private function processLogData(array &$arguments, &$result, ?object &$error): void
/**
* @psalm-param-out array|null $arguments
*/
private function processLogData(array &$arguments, mixed &$result, ?object &$error): void
{
if (!($this->config->getLogLevel() & ContainerInterfaceProxy::LOG_ARGUMENTS)) {
$arguments = null;
Expand All @@ -50,7 +53,7 @@ private function logToCollector(
object $instance,
string $method,
?array $arguments,
$result,
mixed $result,
?object $error,
float $timeStart
): void {
Expand All @@ -72,7 +75,7 @@ private function logToEvent(
object $instance,
string $method,
?array $arguments,
$result,
mixed $result,
?object $error,
float $timeStart
): void {
Expand Down
2 changes: 1 addition & 1 deletion src/Collector/ServiceCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function collect(
string $class,
string $method,
?array $arguments,
$result,
mixed $result,
string $status,
?object $error,
float $timeStart,
Expand Down
3 changes: 3 additions & 0 deletions src/Collector/ServiceMethodProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ protected function afterCall(string $methodName, array $arguments, mixed $result

protected function getNewStaticInstance(object $instance): ObjectProxy
{
/**
* @psalm-suppress UnsafeInstantiation Constructor should be consistent to `getNewStaticInstance()`.
*/
return new static($this->getService(), $instance, $this->methods, $this->getConfig());
}
}
3 changes: 3 additions & 0 deletions src/Collector/ServiceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ protected function afterCall(string $methodName, array $arguments, mixed $result

protected function getNewStaticInstance(object $instance): ObjectProxy
{
/**
* @psalm-suppress UnsafeInstantiation Constructor should be consistent to `getNewStaticInstance()`.
*/
return new static($this->service, $instance, $this->config);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Collector/TimelineCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function getCollected(): array
return $this->events;
}

public function collect(CollectorInterface $collector, string|int $reference, ...$data): void
public function collect(CollectorInterface $collector, string|int $reference, mixed ...$data): void
{
if (!$this->isActive()) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/Collector/VarDumperHandlerInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function handle(mixed $variable, int $depth, bool $highlight = false): vo
$callStack = $value;
break;
}
/** @psalm-var array{file: string, line: int} $callStack */
/** @psalm-var array{file: string, line: int}|null $callStack */

$this->collector->collect(
$variable,
Expand Down
2 changes: 1 addition & 1 deletion src/Command/DebugContainerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($input->hasOption('groups') && $input->getOption('groups')) {
$build = $this->getConfigBuild($config);
$groups = array_keys($build);
ksort($groups);
sort($groups);

$io->table(['Groups'], array_map(fn ($group) => [$group], $groups));

Expand Down
7 changes: 4 additions & 3 deletions src/Command/DebugEventsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Yii\Debug\Command;

use Psr\Container\ContainerInterface;
use ReflectionClass;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -49,7 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($input->hasOption('groups') && $input->getOption('groups')) {
$build = $this->getConfigBuild($config);
$groups = array_keys($build);
ksort($groups);
sort($groups);

$io->table(['Groups'], array_map(fn ($group) => [$group], $groups));

Expand Down Expand Up @@ -89,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
is_countable($listeners) ? count($listeners) : 0,
implode(
"\n",
array_map(function ($listener) {
array_map(function (mixed $listener) {
if (is_array($listener)) {
return sprintf(
'%s::%s',
Expand All @@ -113,7 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function getConfigBuild(mixed $config): array
{
$reflection = new \ReflectionClass($config);
$reflection = new ReflectionClass($config);
$buildReflection = $reflection->getProperty('build');
$buildReflection->setAccessible(true);
return $buildReflection->getValue($config);
Expand Down
33 changes: 17 additions & 16 deletions src/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\Debug;

use __PHP_Incomplete_Class;
use Closure;
use Yiisoft\VarDumper\ClosureExporter;

Expand Down Expand Up @@ -60,7 +61,7 @@ public function asJsonObjectsMap(int $depth = 50, bool $prettyPrint = false): st
return $this->asJsonInternal($this->objects, $prettyPrint, $depth, 1, true);
}

private function buildObjectsCache($variable, int $depth, int $level = 0): void
private function buildObjectsCache(mixed $variable, int $depth, int $level = 0): void
{
if ($depth <= $level) {
return;
Expand Down Expand Up @@ -89,7 +90,7 @@ private function buildObjectsCache($variable, int $depth, int $level = 0): void
}

private function asJsonInternal(
$variable,
mixed $variable,
bool $format,
int $depth,
int $objectCollapseLevel,
Expand All @@ -109,32 +110,32 @@ private function asJsonInternal(

private function getObjectProperties(object $var): array
{
if (\__PHP_Incomplete_Class::class !== $var::class && method_exists($var, '__debugInfo')) {
if (__PHP_Incomplete_Class::class !== $var::class && method_exists($var, '__debugInfo')) {
$var = $var->__debugInfo();
}

return (array) $var;
}

private function dumpNestedInternal(
$var,
mixed $variable,
int $depth,
int $level,
int $objectCollapseLevel,
bool $inlineObject
): mixed {
switch (gettype($var)) {
switch (gettype($variable)) {
case 'array':
if ($depth <= $level) {
$valuesCount = count($var);
$valuesCount = count($variable);
if ($valuesCount === 0) {
return [];
}
return sprintf('array (%d %s) [...]', $valuesCount, $valuesCount === 1 ? 'item' : 'items');
}

$output = [];
foreach ($var as $key => $value) {
foreach ($variable as $key => $value) {
$keyDisplay = str_replace("\0", '::', trim((string) $key));
$output[$keyDisplay] = $this->dumpNestedInternal(
$value,
Expand All @@ -147,16 +148,16 @@ private function dumpNestedInternal(

break;
case 'object':
$objectDescription = $this->getObjectDescription($var);
if ($depth <= $level || array_key_exists($var::class, $this->excludedClasses)) {
$objectDescription = $this->getObjectDescription($variable);
if ($depth <= $level || array_key_exists($variable::class, $this->excludedClasses)) {
$output = $objectDescription . ' (...)';
break;
}

if ($var instanceof Closure) {
if ($variable instanceof Closure) {
$output = $inlineObject
? $this->exportClosure($var)
: [$objectDescription => $this->exportClosure($var)];
? $this->exportClosure($variable)
: [$objectDescription => $this->exportClosure($variable)];
break;
}

Expand All @@ -165,7 +166,7 @@ private function dumpNestedInternal(
break;
}

$properties = $this->getObjectProperties($var);
$properties = $this->getObjectProperties($variable);
if (empty($properties)) {
if ($inlineObject) {
$output = '{stateless object}';
Expand Down Expand Up @@ -194,10 +195,10 @@ private function dumpNestedInternal(
break;
case 'resource':
case 'resource (closed)':
$output = $this->getResourceDescription($var);
$output = $this->getResourceDescription($variable);
break;
default:
$output = $var;
$output = $variable;
}

return $output;
Expand Down Expand Up @@ -226,7 +227,7 @@ private function normalizeProperty(string $property): string
return 'public $' . $property;
}

private function getResourceDescription($resource): array|string
private function getResourceDescription(mixed $resource): array|string
{
if (!is_resource($resource)) {
return '{closed resource}';
Expand Down
3 changes: 3 additions & 0 deletions src/Helper/StreamWrapper/StreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public function rmdir(string $path, int $options): bool
return rmdir($path, $this->context);
}

/**
* @psalm-suppress InvalidReturnType Unfortunately, I don't know what to return here.
*/
public function stream_cast(int $castAs)
{
// ???
Expand Down
3 changes: 3 additions & 0 deletions src/Helper/StreamWrapper/StreamWrapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function stream_eof(): bool;

public function stream_seek(int $offset, int $whence = SEEK_SET): bool;

/**
* @return resource
*/
public function stream_cast(int $castAs);

public function stream_stat(): array|false;
Expand Down

0 comments on commit b79ba7f

Please sign in to comment.