Skip to content

Commit 2f75201

Browse files
committed
Address Psalm issues
1 parent b8cab56 commit 2f75201

File tree

14 files changed

+47
-15
lines changed

14 files changed

+47
-15
lines changed

psalm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
<MissingClassConstType errorLevel="info" />
2929
<UndefinedClass>
3030
<!-- BC layer for phpunit/php-text-template v1 -->
31-
<errorLevel type="info">
31+
<errorLevel type="suppress">
3232
<referencedClass name="Text_Template"/>
3333
</errorLevel>
3434
</UndefinedClass>
3535
<UndefinedDocblockClass>
3636
<!-- BC layer for phpunit/php-text-template v1 -->
37-
<errorLevel type="info">
37+
<errorLevel type="suppress">
3838
<referencedClass name="Text_Template"/>
3939
</errorLevel>
4040
</UndefinedDocblockClass>

src/analyzer/Cli/AnalyzeCommand.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7171

7272
private function doExecute(): void
7373
{
74-
/** @var string $configFile */
75-
$configFile = $this->input->getOption('config') ?? getcwd().DIRECTORY_SEPARATOR.'tombstone.yml';
74+
/** @var string|null $configFile */
75+
$configFile = $this->input->getOption('config');
76+
if (null === $configFile) {
77+
if (!($cwd = getcwd())) {
78+
throw new \RuntimeException('Could not determine current working directory, please provide a configuration file path.');
79+
}
80+
$configFile = $cwd.DIRECTORY_SEPARATOR.'tombstone.yml';
81+
}
82+
7683
if (!file_exists($configFile)) {
7784
throw new \InvalidArgumentException(\sprintf('Could not find configuration file %s', $configFile));
7885
}

src/analyzer/Config/YamlConfigProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ class YamlConfigProvider implements ConfigProviderInterface
2828
public function __construct(string $configFile)
2929
{
3030
$this->configFile = $configFile;
31+
$realpath = realpath($this->configFile);
32+
if (false === $realpath) {
33+
throw new \InvalidArgumentException("Config file '$this->configFile' is not a valid file path.");
34+
}
3135

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

3640
public function readConfiguration(): array

src/analyzer/Model/AnalyzerResult.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,12 @@ private function writeResultDirectoryTree(array &$tree, array $pathSegments, Ana
116116
$tree['files'][] = $fileResult;
117117
} else {
118118
$pathPart = array_shift($pathSegments);
119-
if (!isset($tree['dirs'][$pathPart])) {
120-
$tree['dirs'][$pathPart] = ['dirs' => [], 'files' => []];
119+
if (null !== $pathPart) {
120+
if (!isset($tree['dirs'][$pathPart])) {
121+
$tree['dirs'][$pathPart] = ['dirs' => [], 'files' => []];
122+
}
123+
$this->writeResultDirectoryTree($tree['dirs'][$pathPart], $pathSegments, $fileResult);
121124
}
122-
$this->writeResultDirectoryTree($tree['dirs'][$pathPart], $pathSegments, $fileResult);
123125
}
124126
}
125127

src/analyzer/Report/Checkstyle/CheckstyleReportGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private function getCalledBy(Tombstone $tombstone): string
8181
return '';
8282
}
8383

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

src/analyzer/Report/Html/Renderer/PhpFileFormatter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function __construct(PhpSyntaxHighlighter $highlighter)
2828
public function formatFile(string $file): array
2929
{
3030
$buffer = file_get_contents($file);
31+
if (false === $buffer) {
32+
return [];
33+
}
34+
3135
$tokens = token_get_all($buffer);
3236
$fileEndsWithNewLine = "\n" === substr($buffer, -1);
3337
unset($buffer);

src/analyzer/Stock/ParserTombstoneProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static function create(array $config, ConsoleOutputInterface $consoleOutp
4444
if (method_exists(ParserFactory::class, 'createForVersion')) {
4545
$parser = (new ParserFactory())->createForVersion(PhpVersion::getHostVersion());
4646
} else {
47+
/** @psalm-suppress UndefinedConstant Backwards compatibility for php-parser v4 */
4748
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new Lexer());
4849
}
4950
$traverser = new NodeTraverser();

src/analyzer/Stock/TombstoneExtractor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ private function parseSourceCode(string $absoluteFilePath): void
6666
{
6767
try {
6868
$content = file_get_contents($absoluteFilePath);
69+
if (false === $content) {
70+
throw new TombstoneExtractorException(\sprintf('File "%s" could not be read.', $absoluteFilePath));
71+
}
72+
6973
$stmts = $this->parser->parse($content);
7074
if (null === $stmts) {
7175
throw new TombstoneExtractorException(\sprintf('PHP code in "%s" could not be parsed.', $absoluteFilePath));

src/analyzer/Stock/TombstoneNodeVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private function extractArguments(array $args): array
179179
if ($arg instanceof Node\VariadicPlaceholder) {
180180
break; // Can't extract from ...$var arguments
181181
} elseif ($arg->value instanceof String_) {
182-
/** @psalm-suppress RedundantCastGivenDocblockType */
182+
/** @psalm-suppress RedundantCast */
183183
$params[] = (string) $arg->value->value;
184184
} else {
185185
$params[] = null;

src/core/FinderFacade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function __construct(array $items = [], array $excludes = [], array $name
2525
}
2626

2727
/**
28+
* @psalm-suppress InvalidReturnType
29+
* @psalm-suppress InvalidReturnStatement
30+
*
2831
* @return string[]
2932
*/
3033
public function findFiles(): array

src/core/Format/AnalyzerLogFormat.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class AnalyzerLogFormat
3737

3838
public static function vampireToLog(Vampire $vampire): string
3939
{
40-
return json_encode([
40+
/** @var string $encoded */
41+
$encoded = json_encode([
4142
self::FIELD_VERSION => self::CURRENT_VERSION,
4243
self::FIELD_FUNCTION_NAME => $vampire->getFunctionName(),
4344
self::FIELD_ARGUMENTS => $vampire->getArguments(),
@@ -49,6 +50,8 @@ public static function vampireToLog(Vampire $vampire): string
4950
self::FIELD_INVOCATION_DATE => $vampire->getInvocationDate(),
5051
self::FIELD_INVOKER => $vampire->getInvoker(),
5152
]);
53+
54+
return $encoded;
5255
}
5356

5457
private static function encodeStackTrace(StackTrace $stackTrace): array

src/core/Model/RootPath.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ private function createRelativePath(string $path): RelativeFilePath
7373
return new RelativeFilePath('', $this);
7474
}
7575
// Remove leading "./"
76+
/** @var string $path */
7677
$path = preg_replace('#^(\\./)+#', '', $path);
7778
}
7879

src/logger/Formatter/JsonFormatter.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ class JsonFormatter implements FormatterInterface
1212
{
1313
public function format(Vampire $vampire): string
1414
{
15-
return json_encode([
15+
/** @var string $encoded */
16+
$encoded = json_encode([
1617
'arguments' => $vampire->getArguments(),
1718
'file' => $vampire->getFile()->getReferencePath(),
1819
'line' => $vampire->getLine(),
1920
'method' => $vampire->getMethod(),
2021
'stackTrace' => $this->getStackTraceValues($vampire->getStackTrace()),
2122
'metadata' => $vampire->getMetadata(),
22-
'invocationDate' => $vampire->getInvocationDate(),
23-
'invoker' => $vampire->getInvoker(),
24-
]).PHP_EOL;
23+
'invocationDate' => $vampire->getInvocationDate(), 'invoker' => $vampire->getInvoker(),
24+
]);
25+
26+
return $encoded.PHP_EOL;
2527
}
2628

2729
private function getStackTraceValues(StackTrace $stackTrace): array

src/logger/Handler/StreamHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class StreamHandler extends AbstractHandler
1616
{
1717
/**
18-
* @var resource|closed-resource|null
18+
* @var resource|closed-resource|false|null
1919
*/
2020
protected $stream;
2121

0 commit comments

Comments
 (0)