Skip to content

Commit

Permalink
Feature/upgrade psalm (#29)
Browse files Browse the repository at this point in the history
Adding psalm v5 for php versions which support it
  • Loading branch information
JimTools authored Feb 29, 2024
1 parent 0e2e4d9 commit 50d5344
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"squizlabs/php_codesniffer": "^3.5",
"symfony/css-selector": "^4.4|^5.0",
"symfony/dom-crawler": "^4.4|^5.0",
"vimeo/psalm": "^3.12|^4.0"
"vimeo/psalm": "^4.30|^5.22"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src/" />
Expand Down
3 changes: 3 additions & 0 deletions src/analyzer/Model/TombstoneIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Scheb\Tombstone\Core\Model\FilePathInterface;
use Scheb\Tombstone\Core\Model\Tombstone;

/**
* @template-implements \IteratorAggregate<array-key, Tombstone>
*/
class TombstoneIndex implements \Countable, \IteratorAggregate
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/analyzer/Model/VampireIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Scheb\Tombstone\Core\Model\Vampire;

/**
* @template-implements \IteratorAggregate<array-key, Vampire>
*/
class VampireIndex implements \Countable, \IteratorAggregate
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function getCalledBy(Tombstone $tombstone): string
}

$invoker = array_shift($vampires)->getInvoker();
$calledBy = sprintf(' by "%s"', $invoker ?: 'global scope');
$calledBy = sprintf(' by "%s"', null !== $invoker ? $invoker : 'global scope');

$numAdditionalVampires = $numVampires - 1;
if ($numAdditionalVampires > 0) {
Expand Down
9 changes: 5 additions & 4 deletions src/analyzer/Report/Console/ConsoleReportGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ private function displayTombstones(array $result): void
$this->newLine();
$this->printTombstone($tombstone, 'RIP');
$date = $tombstone->getTombstoneDate();
if ($date) {
if ($age = TimePeriodFormatter::formatAge($date)) {
if (null !== $date) {
$age = TimePeriodFormatter::formatAge($date);
if (null !== $age) {
$this->output->writeln(sprintf(' was not called for %s', $age));
} else {
$this->output->writeln(sprintf(' was not called since %s', $date));
Expand All @@ -109,10 +110,10 @@ private function printTombstone(Tombstone $tombstone, string $prefix): void
$this->output->writeln(sprintf(' [%s] <info>%s</info>', $prefix, (string) $tombstone));
$this->output->writeln(sprintf(' in <comment>line %s</comment>', $tombstone->getLine()));
$method = $tombstone->getMethod();
if ($method) {
if (null !== $method) {
$this->output->writeln(sprintf(' in method <comment>%s</comment>', $method));
} else {
$this->output->writeln(sprintf(' in global scope'));
$this->output->writeln(' in global scope');
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/analyzer/Report/Html/Renderer/DashboardRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ private function getDeadSince(Tombstone $tombstone): string
return 'since unknown';
}

if ($age = TimePeriodFormatter::formatAge($date)) {
$age = TimePeriodFormatter::formatAge($date);
if (null !== $age) {
return 'for '.$age;
}

Expand Down Expand Up @@ -167,7 +168,7 @@ private function renderInvokers(Tombstone $tombstone): string
$invokersString = '';
foreach ($invokers as $invoker) {
$this->invokerTemplate->setVar([
'invoker' => $invoker ? htmlspecialchars($invoker) : 'global scope',
'invoker' => null !== $invoker ? htmlspecialchars($invoker) : 'global scope',
]);
$invokersString .= $this->invokerTemplate->render();
}
Expand Down Expand Up @@ -218,7 +219,8 @@ private function renderDeletedTombstones(AnalyzerFileResult $fileResult): string
private function getLastCalled(Vampire $vampire): string
{
$invocationDate = $vampire->getInvocationDate();
if ($age = TimePeriodFormatter::formatAge($invocationDate)) {
$age = TimePeriodFormatter::formatAge($invocationDate);
if (null !== $age) {
return $age;
}

Expand All @@ -228,7 +230,7 @@ private function getLastCalled(Vampire $vampire): string
private function getTombstoneScope(Tombstone $tombstone): string
{
$method = $tombstone->getMethod();
if ($method) {
if (null !== $method) {
return sprintf('method <samp>%s</samp>', htmlspecialchars($method));
}

Expand Down
3 changes: 2 additions & 1 deletion src/analyzer/Report/Html/Renderer/PhpFileFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ private function formatTokens(array $tokens): \Traversable
$stringFlag = false;
$result = '';
foreach ($tokens as $j => $token) {
$previousToken = $j > 0 ? $tokens[$j - 1] : null;
if (\is_string($token)) {
if ('"' === $token && '\\' !== $tokens[$j - 1]) {
if ('"' === $token && '\\' !== $previousToken) {
$result .= $this->highlighter->formatString($token);
$stringFlag = !$stringFlag;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/analyzer/Report/TimePeriodFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TimePeriodFormatter
public static function formatAge(string $date): ?string
{
$tombstoneDate = strtotime($date);
if (!$tombstoneDate) {
if (false === $tombstoneDate) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/Stock/TombstoneNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(TombstoneExtractor $tombstoneCallback, array $tombst
$this->tombstoneFunctionNames = $tombstoneFunctionNames;
}

public function enterNode(Node $node)
public function enterNode(Node $node): void
{
parent::enterNode($node);
if ($node instanceof Class_) {
Expand Down Expand Up @@ -114,7 +114,7 @@ private function visitStaticCallNode(StaticCall $node): void
}
}

public function leaveNode(Node $node)
public function leaveNode(Node $node): void
{
parent::leaveNode($node);
if ($node instanceof ClassMethod || $node instanceof Function_) {
Expand Down
8 changes: 4 additions & 4 deletions src/core/Model/RootPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ private function createRelativePath(string $path): RelativeFilePath
{
if ('' !== $path && '.' === $path[0]) {
if ('.' === $path) {
$path = ''; // Path is equal root path
} elseif ('.' === $path[0]) {
// Remove leading "./"
$path = preg_replace('#^(\\./)+#', '', $path);
// Path is equal root path
return new RelativeFilePath('', $this);
}
// Remove leading "./"
$path = preg_replace('#^(\\./)+#', '', $path);
}

return new RelativeFilePath($path, $this);
Expand Down
4 changes: 2 additions & 2 deletions src/core/Model/StackTrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Scheb\Tombstone\Core\Model;

class StackTrace implements \Countable, \IteratorAggregate, \ArrayAccess
class StackTrace implements \Countable, StackTraceInterface
{
/**
* @var StackTraceFrame[]
Expand Down Expand Up @@ -32,7 +32,7 @@ public function count(): int
}

/**
* @return \Traversable<int, StackTraceFrame>
* @return \Traversable<array-key, StackTraceFrame>
*/
public function getIterator(): \Traversable
{
Expand Down
14 changes: 14 additions & 0 deletions src/core/Model/StackTraceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Scheb\Tombstone\Core\Model;

/**
* @extends \IteratorAggregate<array-key, StackTraceFrame>
* @extends \ArrayAccess<array-key, StackTraceFrame>
*/
interface StackTraceInterface extends \IteratorAggregate, \ArrayAccess
{
public function getHash(): int;
}
6 changes: 6 additions & 0 deletions src/logger/Handler/PsrLoggerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ class PsrLoggerHandler extends AbstractHandler
*/
private $logger;

/**
* @var string|int|mixed
*/
private $level;

/**
* @param string|int|mixed $level
*/
public function __construct(LoggerInterface $logger, $level)
{
$this->logger = $logger;
Expand Down
2 changes: 1 addition & 1 deletion src/logger/Handler/StreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct($stream, ?int $filePermission = null, $useLocking =

public function close(): void
{
if ($this->url && \is_resource($this->stream)) {
if (null !== $this->url && \is_resource($this->stream)) {
fclose($this->stream);
}
$this->stream = null;
Expand Down

0 comments on commit 50d5344

Please sign in to comment.