Skip to content

Commit

Permalink
Address Psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Feb 4, 2025
1 parent b8cab56 commit 4604796
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 25 deletions.
12 changes: 0 additions & 12 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,5 @@
<DeprecatedTrait errorLevel="info" />
<!-- Not supported before PHP 8.3 -->
<MissingClassConstType errorLevel="info" />
<UndefinedClass>
<!-- BC layer for phpunit/php-text-template v1 -->
<errorLevel type="info">
<referencedClass name="Text_Template"/>
</errorLevel>
</UndefinedClass>
<UndefinedDocblockClass>
<!-- BC layer for phpunit/php-text-template v1 -->
<errorLevel type="info">
<referencedClass name="Text_Template"/>
</errorLevel>
</UndefinedDocblockClass>
</issueHandlers>
</psalm>
11 changes: 9 additions & 2 deletions src/analyzer/Cli/AnalyzeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int

private function doExecute(): void
{
/** @var string $configFile */
$configFile = $this->input->getOption('config') ?? getcwd().DIRECTORY_SEPARATOR.'tombstone.yml';
/** @var string|null $configFile */
$configFile = $this->input->getOption('config');
if (null === $configFile) {
if (!($cwd = getcwd())) {
throw new \RuntimeException('Could not determine current working directory, please provide a configuration file path.');
}
$configFile = $cwd.DIRECTORY_SEPARATOR.'tombstone.yml';
}

if (!file_exists($configFile)) {
throw new \InvalidArgumentException(\sprintf('Could not find configuration file %s', $configFile));
}
Expand Down
6 changes: 5 additions & 1 deletion src/analyzer/Config/YamlConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ class YamlConfigProvider implements ConfigProviderInterface
public function __construct(string $configFile)
{
$this->configFile = $configFile;
$realpath = realpath($this->configFile);
if (false === $realpath) {
throw new \InvalidArgumentException("Config file '$this->configFile' is not a valid file path.");
}

// Make all paths relative to config file path
$this->rootPath = new RootPath(\dirname(realpath($this->configFile)));
$this->rootPath = new RootPath(\dirname($realpath));
}

public function readConfiguration(): array
Expand Down
8 changes: 5 additions & 3 deletions src/analyzer/Model/AnalyzerResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ private function writeResultDirectoryTree(array &$tree, array $pathSegments, Ana
$tree['files'][] = $fileResult;
} else {
$pathPart = array_shift($pathSegments);
if (!isset($tree['dirs'][$pathPart])) {
$tree['dirs'][$pathPart] = ['dirs' => [], 'files' => []];
if (null !== $pathPart) {
if (!isset($tree['dirs'][$pathPart])) {
$tree['dirs'][$pathPart] = ['dirs' => [], 'files' => []];
}
$this->writeResultDirectoryTree($tree['dirs'][$pathPart], $pathSegments, $fileResult);
}
$this->writeResultDirectoryTree($tree['dirs'][$pathPart], $pathSegments, $fileResult);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private function getCalledBy(Tombstone $tombstone): string
return '';
}

/** @psalm-suppress PossiblyNullReference Handled in the if statement above */
$invoker = array_shift($vampires)->getInvoker();
$calledBy = \sprintf(' by "%s"', null !== $invoker ? $invoker : 'global scope');

Expand Down
4 changes: 4 additions & 0 deletions src/analyzer/Report/Html/Renderer/PhpFileFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function __construct(PhpSyntaxHighlighter $highlighter)
public function formatFile(string $file): array
{
$buffer = file_get_contents($file);
if (false === $buffer) {
return [];
}

$tokens = token_get_all($buffer);
$fileEndsWithNewLine = "\n" === substr($buffer, -1);
unset($buffer);
Expand Down
1 change: 1 addition & 0 deletions src/analyzer/Stock/ParserTombstoneProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static function create(array $config, ConsoleOutputInterface $consoleOutp
if (method_exists(ParserFactory::class, 'createForVersion')) {
$parser = (new ParserFactory())->createForVersion(PhpVersion::getHostVersion());
} else {
/** @psalm-suppress UndefinedConstant Backwards compatibility for php-parser v4 */
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new Lexer());
}
$traverser = new NodeTraverser();
Expand Down
4 changes: 4 additions & 0 deletions src/analyzer/Stock/TombstoneExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ private function parseSourceCode(string $absoluteFilePath): void
{
try {
$content = file_get_contents($absoluteFilePath);
if (false === $content) {
throw new TombstoneExtractorException(\sprintf('File "%s" could not be read.', $absoluteFilePath));
}

$stmts = $this->parser->parse($content);
if (null === $stmts) {
throw new TombstoneExtractorException(\sprintf('PHP code in "%s" could not be parsed.', $absoluteFilePath));
Expand Down
2 changes: 1 addition & 1 deletion src/analyzer/Stock/TombstoneNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private function extractArguments(array $args): array
if ($arg instanceof Node\VariadicPlaceholder) {
break; // Can't extract from ...$var arguments
} elseif ($arg->value instanceof String_) {
/** @psalm-suppress RedundantCastGivenDocblockType */
/** @psalm-suppress RedundantCast */
$params[] = (string) $arg->value->value;
} else {
$params[] = null;
Expand Down
3 changes: 3 additions & 0 deletions src/core/FinderFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function __construct(array $items = [], array $excludes = [], array $name
}

/**
* @psalm-suppress InvalidReturnType
* @psalm-suppress InvalidReturnStatement
*
* @return string[]
*/
public function findFiles(): array
Expand Down
5 changes: 4 additions & 1 deletion src/core/Format/AnalyzerLogFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class AnalyzerLogFormat

public static function vampireToLog(Vampire $vampire): string
{
return json_encode([
/** @var string $encoded */
$encoded = json_encode([
self::FIELD_VERSION => self::CURRENT_VERSION,
self::FIELD_FUNCTION_NAME => $vampire->getFunctionName(),
self::FIELD_ARGUMENTS => $vampire->getArguments(),
Expand All @@ -49,6 +50,8 @@ public static function vampireToLog(Vampire $vampire): string
self::FIELD_INVOCATION_DATE => $vampire->getInvocationDate(),
self::FIELD_INVOKER => $vampire->getInvoker(),
]);

return $encoded;
}

private static function encodeStackTrace(StackTrace $stackTrace): array
Expand Down
1 change: 1 addition & 0 deletions src/core/Model/RootPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private function createRelativePath(string $path): RelativeFilePath
return new RelativeFilePath('', $this);
}
// Remove leading "./"
/** @var string $path */
$path = preg_replace('#^(\\./)+#', '', $path);
}

Expand Down
10 changes: 6 additions & 4 deletions src/logger/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ class JsonFormatter implements FormatterInterface
{
public function format(Vampire $vampire): string
{
return json_encode([
/** @var string $encoded */
$encoded = json_encode([
'arguments' => $vampire->getArguments(),
'file' => $vampire->getFile()->getReferencePath(),
'line' => $vampire->getLine(),
'method' => $vampire->getMethod(),
'stackTrace' => $this->getStackTraceValues($vampire->getStackTrace()),
'metadata' => $vampire->getMetadata(),
'invocationDate' => $vampire->getInvocationDate(),
'invoker' => $vampire->getInvoker(),
]).PHP_EOL;
'invocationDate' => $vampire->getInvocationDate(), 'invoker' => $vampire->getInvoker(),
]);

return $encoded.PHP_EOL;
}

private function getStackTraceValues(StackTrace $stackTrace): array
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 @@ -15,7 +15,7 @@
class StreamHandler extends AbstractHandler
{
/**
* @var resource|closed-resource|null
* @var resource|closed-resource|false|null
*/
protected $stream;

Expand Down

0 comments on commit 4604796

Please sign in to comment.