Skip to content

Commit

Permalink
Fix psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Nov 26, 2023
1 parent f8d4c8d commit 4a5fe41
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 23 deletions.
12 changes: 11 additions & 1 deletion src/Client/TrapHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ public static function fromArray(array $array): self
public function if(bool|callable $condition): self
{
if (\is_callable($condition)) {
$condition = $condition();
try {
$condition = (bool)$condition();
} catch (\Throwable $e) {
$this->values[] = $e;

return $this;
}
}

$this->haveToSend = $condition;
Expand Down Expand Up @@ -106,6 +112,10 @@ private function sendDump(): void
}

// Dump sequence of values
/**
* @var string|int $key
* @var mixed $value
*/
foreach ($this->values as $key => $value) {
/** @psalm-suppress TooManyArguments */
VarDumper::dump($value, label: $key, depth: $this->depth);
Expand Down
10 changes: 8 additions & 2 deletions src/Client/TrapHandle/ContextProvider/Source.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Client\TrapHandle\ContextProvider;

use Buggregator\Trap\Client\TrapHandle\StackTrace;
Expand All @@ -18,6 +16,11 @@
* @author Nicolas Grekas <p@tchwork.com>
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*
* @link https://github.com/symfony/var-dumper/blob/7.0/Dumper/ContextProvider/SourceContextProvider.php
* @link https://github.com/symfony/var-dumper/blob/6.3/Dumper/ContextProvider/SourceContextProvider.php
*
* @psalm-suppress all
*
* todo: rewrite and decompose
*/
final class Source implements ContextProviderInterface
Expand All @@ -27,6 +30,9 @@ final class Source implements ContextProviderInterface
private ?string $projectDir;
private ?FileLinkFormatter $fileLinkFormatter;

/**
* @psalm-suppress UndefinedClass
*/
public function __construct(string $charset = null, string $projectDir = null, FileLinkFormatter $fileLinkFormatter = null, int $limit = 9)
{
$this->charset = $charset;
Expand Down
6 changes: 4 additions & 2 deletions src/Client/TrapHandle/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@

final class Counter
{
/** @var array<non-empty-string, int<0, max>> */
/** @var array<string, int<0, max>> */
private static array $counters = [];

/**
* Returns true if the counter of related stack trace is less than $times. In this case, the counter is incremented.
*
* @param int<0, max> $times
*/
public static function checkAndIncrement(string $key, int $times): bool
{
self::$counters[$key] ??= 0;

if (self::$counters[$key] < $times) {
self::$counters[$key]++;
++self::$counters[$key];
return true;
}

Expand Down
29 changes: 19 additions & 10 deletions src/Client/TrapHandle/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,35 @@
/**
* @internal
* @psalm-internal Buggregator\Trap\Client
* @psalm-type DumperHandler = Closure(mixed $var, string|int|null $label, int $depth): void
*/
final class Dumper
{
/** @var DumperHandler|null */
/** @var null|Closure(mixed, string|null, int): mixed */
private static ?Closure $handler;

public static function dump(mixed $var, string|int|null $label = null, int $depth = 0)
public static function dump(mixed $var, string|int|null $label = null, int $depth = 0): mixed
{
return (self::$handler ??= self::registerHandler())($var, $label, $depth);
return (self::$handler ??= self::registerHandler())($var, empty($label) ? null : (string)$label, $depth);
}

/**
* @return DumperHandler|null The previous handler if any
* @return null|callable(mixed, string|null, int): mixed
* @psalm-suppress MixedInferredReturnType, MixedPropertyTypeCoercion, MismatchingDocblockReturnType
*/
public static function setHandler(callable $callable = null): ?Closure
{
return ([$callable, self::$handler] = [self::$handler, $callable(...)])[0];
return ([$callable, self::$handler] = [self::$handler, $callable === null ? null : $callable(...)])[0];
}

/**
* @return DumperHandler
* @return Closure(mixed, string|null, int): mixed
*
* @author Nicolas Grekas <p@tchwork.com>
*/
private static function registerHandler(): Closure
{
$cloner = new VarCloner();
/** @psalm-suppress InvalidArgument */
$cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);

$format = $_SERVER['VAR_DUMPER_FORMAT'] ?? null;
Expand All @@ -73,31 +75,38 @@ private static function registerHandler(): Closure
$dumper = new ContextualizedDumper($dumper, [new SourceContextProvider()]);
}

return self::$handler = static function ($var, string|int|null $label = null, int $depth = 0) use ($cloner, $dumper): void {
return self::$handler = static function (mixed $var, string|null $label = null, int $depth = 0)
use ($cloner, $dumper): ?string {
$var = $cloner->cloneVar($var);

$label === null or $var = $var->withContext(['label' => $label]);
$depth > 0 and $var = $var->withMaxDepth($depth);

$dumper->dump($var);
return $dumper->dump($var);
};
}

/**
* @return array<array-key, ContextProviderInterface> The context providers
* @author Nicolas Grekas <p@tchwork.com>
*
* @psalm-suppress UndefinedClass
*/
private static function getContextProviders(): array
{
$contextProviders = [];

if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && \class_exists(Request::class)) {
$requestStack = new RequestStack();
/** @psalm-suppress MixedMethodCall */
$requestStack->push(Request::createFromGlobals());
$contextProviders['request'] = new RequestContextProvider($requestStack);
}

$fileLinkFormatter = \class_exists(FileLinkFormatter::class) ? new FileLinkFormatter(null, $requestStack ?? null) : null;
/** @var null|FileLinkFormatter $fileLinkFormatter */
$fileLinkFormatter = \class_exists(FileLinkFormatter::class)
? new FileLinkFormatter(null, $requestStack ?? null)
: null;

return $contextProviders + [
'cli' => new CliContextProvider(),
Expand Down
14 changes: 6 additions & 8 deletions src/Client/TrapHandle/StackTrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@

namespace Buggregator\Trap\Client\TrapHandle;

use Buggregator\Trap\Client\TrapHandle;

final class StackTrace
{
/**
* @param string $baseDir Base directory for relative paths
* @return array<string, array{
* function?: non-empty-string,
* line?: int<0, max>,
* file?: non-empty-string,
* @return list<array{
* function?: string,
* line?: int,
* file?: string,
* class?: class-string,
* object?: object,
* type?: non-empty-string,
* args?: array
* type?: string,
* args?: list<mixed>
* }>
*/
public static function stackTrace(string $baseDir = ''): array
Expand Down
1 change: 1 addition & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
function trap(mixed ...$values): TrapHandle
{
/** @psalm-suppress InternalMethod */
return TrapHandle::fromArray($values);
}
} catch (\Throwable $e) {
Expand Down

0 comments on commit 4a5fe41

Please sign in to comment.