Skip to content

Commit

Permalink
Fix new psalm warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Jul 18, 2024
1 parent 51f5bcb commit 6c0ba8c
Show file tree
Hide file tree
Showing 28 changed files with 98 additions and 40 deletions.
Binary file modified build/kint.phar
Binary file not shown.
11 changes: 6 additions & 5 deletions src/CallFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,15 @@ public static function getFunctionCalls(string $source, int $line, $function): a

// Check if it matches the signature
if (null === $class) {
if ($prev_tokens[1] && isset(self::$classcalls[$prev_tokens[1][0]])) {
if (null !== $prev_tokens[1] && isset(self::$classcalls[$prev_tokens[1][0]])) {
continue;
}
} else {
if (!$prev_tokens[1] || T_DOUBLE_COLON !== $prev_tokens[1][0]) {
if (null === $prev_tokens[1] || T_DOUBLE_COLON !== $prev_tokens[1][0]) {
continue;
}

if (!$prev_tokens[0] || !isset(self::$namespace[$prev_tokens[0][0]])) {
if (null === $prev_tokens[0] || !isset(self::$namespace[$prev_tokens[0][0]])) {
continue;
}

Expand Down Expand Up @@ -539,20 +539,21 @@ 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 */
if ($attribute && ']' === $last[0]) {
$attribute = false;
} elseif (!$ignorestrip && isset(self::$strip[$last[0]]) && !self::tokenIsOperator($next)) {
continue;
}

if (!$ignorestrip && isset(self::$strip[$next[0]]) && $last && !self::tokenIsOperator($last)) {
if (!$ignorestrip && isset(self::$strip[$next[0]]) && !self::tokenIsOperator($last)) {
continue;
}

$token = ' ';
$space = true;
} else {
if (KINT_PHP80 && $last && T_ATTRIBUTE == $last[0]) {
if (KINT_PHP80 && null !== $last && T_ATTRIBUTE == $last[0]) {

Check failure on line 556 in src/CallFinder.php

View workflow job for this annotation

GitHub Actions / Static analysis check

RedundantCondition

src/CallFinder.php:556:21: RedundantCondition: Operand of type true is always truthy (see https://psalm.dev/122)
$attribute = true;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Kint.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ public static function getBasesFromParamInfo(array $params, int $argc): array
* @param array $args Arguments
*
* @return array Call info
*
* @psalm-param list<non-empty-array> $trace
*/
public static function getCallInfo(array $aliases, array $trace, array $args): array
{
Expand Down Expand Up @@ -456,7 +458,7 @@ public static function getCallInfo(array $aliases, array $trace, array $args): a
'trace' => $miniTrace,
];

if ($call) {
if (null !== $call) {
$ret['params'] = $call['parameters'];
$ret['modifiers'] = $call['modifiers'];
}
Expand Down Expand Up @@ -648,7 +650,7 @@ protected static function getSingleCall(array $frame, array $args): ?array
if (
!isset($frame['file'], $frame['line'], $frame['function']) ||
!\is_readable($frame['file']) ||
!$source = \file_get_contents($frame['file'])
false === ($source = \file_get_contents($frame['file']))
) {
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Parser/ClassMethodsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ public function parse(&$var, Value &$o, int $trigger): void
$methods[] = new MethodValue($method);
}

/** @psalm-suppress InvalidArgument */
\usort($methods, ['Kint\\Parser\\ClassMethodsPlugin', 'sort']);

self::$cache[$class] = $methods;
}

if (!empty(self::$cache[$class])) {
$rep = new Representation('Available methods', 'methods');
$rep->contents = [];

// Can't cache access paths
foreach (self::$cache[$class] as $m) {
Expand Down
6 changes: 5 additions & 1 deletion src/Parser/ClassStaticsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function parse(&$var, Value &$o, int $trigger): void
$const->access_path = '\\'.$class.'::'.$name;
}

/** @psalm-var Value */
$const = $this->parser->parse($val, $const);

$consts[] = $const;
Expand All @@ -96,7 +97,8 @@ public function parse(&$var, Value &$o, int $trigger): void
}

$statics = new Representation('Static class properties', 'statics');
$statics->contents = self::$cache[$class];
/** @psalm-var list<Value> */
$statics->contents = self::$cache[$class] ?? [];

foreach ($reflection->getProperties(ReflectionProperty::IS_STATIC) as $static) {
$prop = new Value();
Expand Down Expand Up @@ -124,6 +126,7 @@ public function parse(&$var, Value &$o, int $trigger): void
$statics->contents[] = $prop;
} else {
$static = $static->getValue();
/** @psalm-var Value */
$statics->contents[] = $this->parser->parse($static, $prop);
}
}
Expand All @@ -132,6 +135,7 @@ public function parse(&$var, Value &$o, int $trigger): void
return;
}

/** @psalm-suppress InvalidArgument */
\usort($statics->contents, ['Kint\\Parser\\ClassStaticsPlugin', 'sort']);

$o->addRepresentation($statics);
Expand Down
11 changes: 5 additions & 6 deletions src/Parser/DOMDocumentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ protected function parseList($var, InstanceValue &$o, int $trigger): void
}

$r = new Representation('Iterator');
$r->contents = [];
$o->replaceRepresentation($r, 0);

foreach ($var as $key => $item) {
Expand Down Expand Up @@ -222,6 +223,7 @@ protected function parseNode(DOMNode $var, InstanceValue &$o): void
$attributes = null;

$rep = $o->value;
$rep->contents = [];

foreach ($known_properties as $prop) {
$prop_obj = $this->parseProperty($o, $prop, $var);
Expand Down Expand Up @@ -250,15 +252,14 @@ protected function parseNode(DOMNode $var, InstanceValue &$o): void
// Set the attributes
if ($attributes) {
$a = new Representation('Attributes');
foreach ($attributes->contents as $attribute) {
$a->contents[] = $attribute;
}
$a->contents = $attributes->contents;
$o->addRepresentation($a, 0);
}

// Set the children
if ($childNodes) {
if ($childNodes && \is_array($childNodes->contents)) {
$c = new Representation('Children');
$c->contents = [];

if (1 === \count($childNodes->contents) && ($node = \reset($childNodes->contents)) && \in_array('depth_limit', $node->hints, true)) {
$n = new InstanceValue();
Expand All @@ -278,9 +279,7 @@ protected function parseNode(DOMNode $var, InstanceValue &$o): void
}

$o->addRepresentation($c, 0);
}

if ($childNodes) {
$o->size = \count($childNodes->contents);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ private function parseArray(array &$var, Value $o): Value
}

$rep = new Representation('Contents');
$rep->contents = [];
$rep->implicit_label = true;
$array->addRepresentation($rep);
$array->value = $rep;
Expand Down Expand Up @@ -442,6 +443,7 @@ private function parseObject(&$var, Value $o): Value
}

$rep = new Representation('Properties');
$rep->contents = [];

$readonly = [];

Expand Down
4 changes: 4 additions & 0 deletions src/Parser/SimpleXMLElementPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function parse(&$var, Value &$o, int $trigger): void

// Attributes
$a = new Representation('Attributes');
$a->contents = [];

$base_obj = new Value();
$base_obj->depth = $x->depth;
Expand All @@ -96,6 +97,7 @@ public function parse(&$var, Value &$o, int $trigger): void
$attribs = [];

foreach ($namespaces as $nsAlias => $nsUrl) {
/** @psalm-suppress RiskyTruthyFalsyComparison */
if ($nsAttribs = $var->attributes($nsUrl)) {
$cleanAttribs = [];
foreach ($nsAttribs as $name => $attrib) {
Expand Down Expand Up @@ -131,6 +133,7 @@ public function parse(&$var, Value &$o, int $trigger): void

// Children
$c = new Representation('Children');
$c->contents = [];

foreach ($namespaces as $nsAlias => $nsUrl) {
// This is doubling items because of the root namespace
Expand All @@ -140,6 +143,7 @@ public function parse(&$var, Value &$o, int $trigger): void
continue;
}

/** @psalm-suppress RiskyTruthyFalsyComparison */
if ($nsChildren = $var->children($nsUrl)) {
$nsap = [];
foreach ($nsChildren as $name => $child) {
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/TracePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function parse(&$var, Value &$o, int $trigger): void
continue;
}

if (isset($trace[$index]['file']) && ($realfile = \realpath($trace[$index]['file']))) {
if (isset($trace[$index]['file']) && false !== ($realfile = \realpath($trace[$index]['file']))) {
foreach ($path_blacklist as $path) {
if (0 === \strpos($realfile, $path)) {
continue 2;
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/XmlPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected static function xmlToSimpleXML(string $var, ?string $parent_path): ?ar
protected static function xmlToDOMDocument(string $var, ?string $parent_path): ?array
{
// There's no way to check validity in DOMDocument without making errors. For shame!
if (!self::xmlToSimpleXML($var, $parent_path)) {
if (null === self::xmlToSimpleXML($var, $parent_path)) {
return null;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Renderer/CliRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class CliRenderer extends TextRenderer

protected static $terminal_width = null;

/** @var bool */
protected $windows_output = false;

protected $colors = false;
Expand Down Expand Up @@ -100,7 +101,10 @@ public function __construct()
if (!self::$terminal_width) {
if (!KINT_WIN && self::$detect_width) {
try {
self::$terminal_width = (int) \exec('tput cols');
$tput = \exec('tput cols');
if (false !== $tput) {
self::$terminal_width = (int) $tput;
}
} catch (Throwable $t) {
self::$terminal_width = self::$default_width;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Rich/AbstractPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function renderLockedHeader(Value $o, string $content): string
{
$header = '<dt class="kint-parent kint-locked">';

if (RichRenderer::$access_paths && $o->depth > 0 && $ap = $o->getAccessPath()) {
if (RichRenderer::$access_paths && $o->depth > 0 && null !== ($ap = $o->getAccessPath())) {
$header .= '<span class="kint-access-path-trigger" title="Show access path">&rlarr;</span>';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Rich/CallablePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function renderMethod(MethodValue $o): string
$header .= '<var>'.$s;

if ($o->return_reference) {
if ($s) {
if (\is_string($s) && \strlen($s)) {
$header .= ' ';
}
$header .= $this->renderer->escape('&');
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Rich/MethodDefinitionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function renderTab(Representation $r): ?string
return null;
}

if (isset($r->contents)) {
if (\is_string($r->contents)) {
$docstring = [];
foreach (\explode("\n", $r->contents) as $line) {
$docstring[] = \trim($line);
Expand Down
1 change: 1 addition & 0 deletions src/Renderer/Rich/SimpleXMLElementPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function renderValue(Value $o): ?string
return null;
}

/** @psalm-suppress RiskyTruthyFalsyComparison */
if (!$o->isStringValue() || !empty($o->getRepresentation('attributes')->contents)) {
return null;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Renderer/Rich/TablePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ class TablePlugin extends AbstractPlugin implements TabPluginInterface
{
public static $respect_str_length = true;

public function renderTab(Representation $r): string
public function renderTab(Representation $r): ?string
{
if (!\is_array($r->contents)) {
return null;
}

$out = '<pre><table><thead><tr><th></th>';

$firstrow = \reset($r->contents);
Expand All @@ -51,6 +55,7 @@ public function renderTab(Representation $r): string

$out .= '</tr></thead><tbody>';

/** $psalm-var \Kint\Zval\Value[] $r->contents */
foreach ($r->contents as $row) {
$out .= '<tr><th>';
if (null !== ($s = $row->getName())) {
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Rich/TimestampPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TimestampPlugin extends AbstractPlugin implements TabPluginInterface
{
public function renderTab(Representation $r): ?string
{
if ($dt = DateTime::createFromFormat('U', (string) $r->contents)) {
if (\is_scalar($r->contents) && ($dt = DateTime::createFromFormat('U', (string) $r->contents))) {
return '<pre>'.$dt->setTimeZone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s T').'</pre>';
}

Expand Down
4 changes: 2 additions & 2 deletions src/Renderer/RichRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function renderHeaderWrapper(Value $o, bool $has_children, string $conten

$out .= '>';

if (self::$access_paths && $o->depth > 0 && ($ap = $o->getAccessPath())) {
if (self::$access_paths && $o->depth > 0 && null !== ($ap = $o->getAccessPath())) {
$out .= '<span class="kint-access-path-trigger" title="Show access path">&rlarr;</span>';
}

Expand Down Expand Up @@ -304,7 +304,7 @@ public function renderHeader(Value $o): string
if (null !== ($s = $o->getName())) {
$output .= '<dfn>'.$this->escape($s).'</dfn> ';

if ($s = $o->getOperator()) {
if (null !== ($s = $o->getOperator())) {
$output .= $this->escape($s, 'ASCII').' ';
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ private function __construct()
* @param int $value Amount of bytes
*
* @return array Human readable value and unit
*
* @psalm-return array{value: float, unit: 'B'|'KB'|'MB'|'GB'|'TB'}
*/
public static function getHumanReadableBytes(int $value): array
{
Expand Down
7 changes: 4 additions & 3 deletions src/Zval/BlobValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static function strlen(string $string, $encoding = false): int
$encoding = self::detectEncoding($string);
}

if ($encoding && 'ASCII' !== $encoding) {
if (false !== $encoding && 'ASCII' !== $encoding) {
return \mb_strlen($string, $encoding);
}
}
Expand All @@ -148,7 +148,7 @@ public static function substr(string $string, int $start, ?int $length = null, $
$encoding = self::detectEncoding($string);
}

if ($encoding && 'ASCII' !== $encoding) {
if (false !== $encoding && 'ASCII' !== $encoding) {
return \mb_substr($string, $start, $length, $encoding);
}
}
Expand All @@ -167,7 +167,8 @@ public static function substr(string $string, int $start, ?int $length = null, $
public static function detectEncoding(string $string)
{
if (\function_exists('mb_detect_encoding')) {
if ($ret = \mb_detect_encoding($string, self::$char_encodings, true)) {
$ret = \mb_detect_encoding($string, self::$char_encodings, true);
if (false !== $ret) {
return $ret;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Zval/MethodValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function __construct(ReflectionFunctionAbstract $method)
$this->startline = $method->getStartLine();
$this->endline = $method->getEndLine();
$this->internal = $method->isInternal();
$this->docstring = $method->getDocComment() ?: null;
$ds = $method->getDocComment();
$this->docstring = false === $ds ? null : $ds;
$this->return_reference = $method->returnsReference();

foreach ($method->getParameters() as $param) {
Expand Down Expand Up @@ -149,7 +150,7 @@ public function getValueShort(): ?string

$ds = $this->value->getDocstringWithoutComments();

if (!$ds) {
if (null === $ds) {
return null;
}

Expand Down
Loading

0 comments on commit 6c0ba8c

Please sign in to comment.