Skip to content

Commit

Permalink
Prune @psalm annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Aug 19, 2024
1 parent d4639cd commit 77f9680
Show file tree
Hide file tree
Showing 30 changed files with 117 additions and 88 deletions.
Binary file modified build/kint.phar
Binary file not shown.
37 changes: 15 additions & 22 deletions src/CallFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ public static function getFunctionCalls(string $source, int $line, $function): a
$class = null;
/**
* @psalm-suppress RedundantFunctionCallGivenDocblockType
* Psalm bug #11075
*/
$function = \strtolower($function);
}
Expand Down Expand Up @@ -258,7 +259,10 @@ public static function getFunctionCalls(string $source, int $line, $function): a
}

// All self::$namespace tokens are T_ constants
/** @psalm-var PhpTokenArray $prev_tokens[0] */
/**
* @psalm-var PhpTokenArray $prev_tokens[0]
* Psalm bug #746
*/
$ns = \explode('\\', \strtolower($prev_tokens[0][1]));

if (\end($ns) !== $class) {
Expand Down Expand Up @@ -375,8 +379,10 @@ public static function getFunctionCalls(string $source, int $line, $function): a
continue;
}

$formatted_parameters = [];

// Format the final output parameters
foreach ($params as &$param) {
foreach ($params as $param) {
$name = self::tokensFormatted($param['short']);

$expression = false;
Expand All @@ -387,16 +393,15 @@ public static function getFunctionCalls(string $source, int $line, $function): a
}
}

$param = [
$formatted_parameters[] = [
'name' => self::tokensToString($name),
'path' => self::tokensToString(self::tokensTrim($param['full'])),
'expression' => $expression,
];
}

// Skip first-class callables
/** @psalm-var list<array{name: string, path: string, expression: bool}> $params */
if (KINT_PHP81 && 1 === \count($params) && '...' === \reset($params)['path']) {
if (KINT_PHP81 && 1 === \count($formatted_parameters) && '...' === \reset($formatted_parameters)['path']) {
continue;
}

Expand Down Expand Up @@ -429,17 +434,14 @@ public static function getFunctionCalls(string $source, int $line, $function): a
}

$function_calls[] = [
'parameters' => $params,
'parameters' => $formatted_parameters,
'modifiers' => $mods,
];
}

return $function_calls;
}

/**
* @psalm-param PhpToken[] $tokens
*/
private static function realTokenIndex(array $tokens, int $index): ?int
{
++$index;
Expand Down Expand Up @@ -467,9 +469,6 @@ private static function tokenIsOperator($token): bool
return '...' !== $token && isset(self::$operator[$token[0]]);
}

/**
* @psalm-param PhpToken[] $tokens
*/
private static function tokensToString(array $tokens): string
{
$out = '';
Expand All @@ -485,9 +484,6 @@ private static function tokensToString(array $tokens): string
return $out;
}

/**
* @psalm-param PhpToken[] $tokens
*/
private static function tokensTrim(array $tokens): array
{
foreach ($tokens as $index => $token) {
Expand All @@ -511,11 +507,6 @@ private static function tokensTrim(array $tokens): array
return \array_reverse($tokens);
}

/**
* @psalm-param PhpToken[] $tokens
*
* @psalm-return PhpToken[]
*/
private static function tokensFormatted(array $tokens): array
{
$tokens = self::tokensTrim($tokens);
Expand Down Expand Up @@ -549,8 +540,10 @@ private static function tokensFormatted(array $tokens): array
}
$next = $tokens[$next];

/** @psalm-var PhpToken $last */
// Since we call tokensTrim we know we can't be here without a $last */
/**
* @psalm-var PhpToken $last
* Since we call tokensTrim we know we can't be here without a $last
*/
if ($attribute && ']' === $last[0]) {
$attribute = false;
} elseif (!$ignorestrip && isset(self::$strip[$last[0]]) && !self::tokenIsOperator($next)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Kint.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

/**
* @psalm-consistent-constructor
* Psalm bug #8523
*
* @psalm-type KintMode = Kint::MODE_*|bool
*/
Expand Down Expand Up @@ -141,7 +142,7 @@ class Kint implements FacadeInterface
];

/**
* @psalm-var class-string[] Array of modes to renderer class names
* @psalm-var class-string<RendererInterface>[] Array of modes to renderer class names
*/
public static array $renderers = [
self::MODE_RICH => Renderer\RichRenderer::class,
Expand All @@ -151,7 +152,7 @@ class Kint implements FacadeInterface
];

/**
* @psalm-var class-string[]
* @psalm-var array<PluginInterface|class-string<ConstructablePluginInterface>>
*/
public static array $plugins = [
\Kint\Parser\ArrayLimitPlugin::class,
Expand Down Expand Up @@ -339,7 +340,6 @@ public static function createFromStatics(array $statics): ?FacadeInterface
return null;
}

/** @psalm-var class-string[] $statics['renderers'] */
if (isset($statics['renderers'][$mode]) && \is_subclass_of($statics['renderers'][$mode], RendererInterface::class)) {
$renderer = new $statics['renderers'][$mode]();
} else {
Expand Down
3 changes: 0 additions & 3 deletions src/Parser/AbstractPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@

namespace Kint\Parser;

/**
* @psalm-consistent-constructor
*/
abstract class AbstractPlugin implements ConstructablePluginInterface
{
private Parser $parser;
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/ClassMethodsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
use ReflectionClass;

/**
* @psalm-type OwnedMethodValue MethodValue&object{owner_class: class-string}
* @psalm-type OwnedMethodValue = MethodValue&object{owner_class: class-string}
*/
class ClassMethodsPlugin extends AbstractPlugin
{
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/ClassStaticsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
use UnitEnum;

/**
* @psalm-type OwnedValue Value&object{owner_class: class-string}
* @psalm-type OwnedValue = Value&object{owner_class: class-string}
*/
class ClassStaticsPlugin extends AbstractPlugin
{
Expand Down
8 changes: 6 additions & 2 deletions src/Parser/IteratorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class IteratorPlugin extends AbstractPlugin
* when traversed. Others are just huge. Either way, put them in here
* and you won't have to worry about them being parsed.
*
* @psalm-var class-string[]
* @psalm-var class-string<Traversable>[]
*/
public static array $blacklist = [
DOMNamedNodeMap::class,
Expand All @@ -72,6 +72,10 @@ public function parse(&$var, Value &$o, int $trigger): void
}

foreach (self::$blacklist as $class) {
/**
* @psalm-suppress RedundantCondition
* Psalm bug #11076
*/
if ($var instanceof $class) {
$b = new Value($class.' Iterator Contents');
$b->depth = $o->depth + 1;
Expand Down Expand Up @@ -103,7 +107,7 @@ public function parse(&$var, Value &$o, int $trigger): void
/**
* @psalm-var object{contents: array} $r->contents->value
* Since we didn't get TRIGGER_DEPTH_LIMIT and set the iterator to the
* same depth we can guarantee at least 1 level deep will exist
* same depth we can assume at least 1 level deep will exist
*/
$r->contents = $this->getParser()->parse($data, $base_obj);
$r->contents = $r->contents->value->contents;
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/JsonPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public function parse(&$var, Value &$o, int $trigger): void
}

$r = new Representation('Json');
/** @psalm-var object{contents: array} $r->contents->value */
$r->contents = $this->getParser()->parse($json, $base_obj);

if (!\in_array('depth_limit', $r->contents->hints, true)) {
/** @psalm-var object{contents: array} $r->contents->value */
$r->contents = $r->contents->value->contents;
}

Expand Down
1 change: 0 additions & 1 deletion src/Parser/MysqliPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public function parse(&$var, Value &$o, int $trigger): void
$parser = $this->getParser();

foreach ($o->value->contents as $key => $obj) {
/** @psalm-var string $obj->name */
if (isset(self::CONNECTED_READABLE[$obj->name])) {
if (!$connected) {
// No failed connections after PHP 8.1
Expand Down
3 changes: 2 additions & 1 deletion src/Parser/ProxyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class ProxyPlugin implements PluginInterface
protected $callback;
private ?Parser $parser = null;

public function __construct(array $types, int $triggers, callable $callback)
/** @psalm-param callable $callback */
public function __construct(array $types, int $triggers, $callback)
{
$this->types = $types;
$this->triggers = $triggers;
Expand Down
14 changes: 6 additions & 8 deletions src/Parser/SimpleXMLElementPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,23 @@ public function parse(&$var, Value &$o, int $trigger): void
$attribs = [];

foreach ($namespaces as $nsAlias => $nsUrl) {
/** @psalm-suppress RiskyTruthyFalsyComparison */
if ($nsAttribs = $var->attributes($nsUrl)) {
if ((bool) $nsAttribs = $var->attributes($nsUrl)) {
$cleanAttribs = [];
foreach ($nsAttribs as $name => $attrib) {
$cleanAttribs[(string) $name] = $attrib;
}

if (null === $nsUrl) {
$obj = clone $base_obj;
if ($obj->access_path) {
if (null !== $obj->access_path) {
$obj->access_path .= '->attributes()';
}

$contents = $parser->parse($cleanAttribs, $obj)->value->contents ?? null;
$a->contents = \is_array($contents) ? $contents : [];
} else {
$obj = clone $base_obj;
if ($obj->access_path) {
if (null !== $obj->access_path) {
$obj->access_path .= '->attributes('.\var_export($nsAlias, true).', true)';
}

Expand Down Expand Up @@ -145,13 +144,12 @@ public function parse(&$var, Value &$o, int $trigger): void
continue;
}

/** @psalm-suppress RiskyTruthyFalsyComparison */
if ($nsChildren = $var->children($nsUrl)) {
if ((bool) $nsChildren = $var->children($nsUrl)) {
$nsap = [];
foreach ($nsChildren as $name => $child) {
$obj = new Value((string) $name);
$obj->depth = $x->depth + 1;
if ($x->access_path) {
if (null !== $x->access_path) {
if (null === $nsUrl) {
$obj->access_path = $x->access_path.'->children()->';
} else {
Expand All @@ -174,7 +172,7 @@ public function parse(&$var, Value &$o, int $trigger): void

$value = $parser->parse($child, $obj);

if ($value->access_path && 'string' === $value->type) {
if (null !== $value->access_path && 'string' === $value->type) {
$value->access_path = '(string) '.$value->access_path;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Parser/StreamPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function parse(&$var, Value &$o, int $trigger): void

/**
* @psalm-var object{contents: array} $rep->contents->value
* We check the depth and can guarantee at least 1 level deep will exist
* We checked the depth and can guarantee at least 1 level deep will exist
*/
$rep->contents = $parser->parse($meta, $base_obj);
$rep->contents = $rep->contents->value->contents;
Expand Down
5 changes: 4 additions & 1 deletion src/Parser/TimestampPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function parse(&$var, Value &$o, int $trigger): void
return;
}

if (!$o->value instanceof Representation) {
return;
}

$len = \strlen((string) $var);

// Guess for anything between March 1973 and November 2286
Expand All @@ -71,7 +75,6 @@ public function parse(&$var, Value &$o, int $trigger): void
// Additionally it's highly unlikely the shortValue will be clipped for length
// If you're writing a plugin that interferes with this, just put your
// parser plugin further down the list so that it gets loaded afterwards.
/** @psalm-var Representation $o->value */
$o->value->label = 'Timestamp';
$o->value->hints[] = 'timestamp';
}
Expand Down
2 changes: 0 additions & 2 deletions src/Parser/TracePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,13 @@ public function parse(&$var, Value &$o, int $trigger): void
$rep->contents = [];

foreach ($old_trace as $frame) {
/** @psalm-var int $index */
$index = $frame->name;

if (!isset($trace[$index]['function'])) {
// Something's very very wrong here, but it's probably a plugin's fault
continue;
}

/** @psalm-var array $trace[$index] */
if (Utils::traceFrameIsListed($trace[$index], self::$blacklist)) {
continue;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Parser/XmlPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ protected static function xmlToSimpleXML(string $var, ?string $parent_path): ?ar
*
* If it errors loading then we wouldn't have gotten this far in the first place.
*
* @psalm-assert non-empty-string $var
*
* @param ?string $parent_path The path to the parent, in this case the XML string
*
* @return ?array The root element DOMNode, the access path, and the root element name
Expand Down
5 changes: 3 additions & 2 deletions src/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
use Kint\Zval\Value;

/**
* @psalm-type PluginMap array<string, class-string>
* @psalm-type PluginMap = array<string, class-string>
*
* @psalm-consistent-constructor
* I'd like to have PluginMap<T> but can't:
* Psalm bug #4308
*/
abstract class AbstractRenderer implements RendererInterface
{
Expand Down
3 changes: 0 additions & 3 deletions src/Renderer/Rich/AbstractPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
use Kint\Zval\InstanceValue;
use Kint\Zval\Value;

/**
* @psalm-consistent-constructor
*/
abstract class AbstractPlugin implements PluginInterface
{
protected RichRenderer $renderer;
Expand Down
3 changes: 1 addition & 2 deletions src/Renderer/Rich/SimpleXMLElementPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function renderValue(Value $o): ?string
return null;
}

/** @psalm-suppress RiskyTruthyFalsyComparison */
if (!$o->is_string_value || !empty($o->getRepresentation('attributes')->contents)) {
if (!$o->is_string_value || (bool) ($o->getRepresentation('attributes')->contents ?? false)) {
return null;
}

Expand Down
Loading

0 comments on commit 77f9680

Please sign in to comment.