Skip to content

Commit

Permalink
Merge pull request #9257 from weirdan/fix-crashes-with-invalid-check-…
Browse files Browse the repository at this point in the history
…type-syntax

Fixes #9201
  • Loading branch information
weirdan committed Feb 10, 2023
2 parents 1ee0f1b + 9541366 commit a4d593c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 1 addition & 4 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="dev-master@7966fd574a1607ccb1abf69c64eb860c7b1c735b">
<files psalm-version="dev-master@1ee0f1bcb81fa87d8b33a74ede37a6b1d8317294">
<file src="examples/TemplateChecker.php">
<PossiblyUndefinedIntArrayOffset>
<code><![CDATA[$comment_block->tags['variablesfrom'][0]]]></code>
Expand Down Expand Up @@ -227,9 +227,6 @@
<PossiblyUndefinedArrayOffset>
<code><![CDATA[$stmt->expr->getArgs()[0]]]></code>
</PossiblyUndefinedArrayOffset>
<PossiblyUndefinedIntArrayOffset>
<code>$check_type_string</code>
</PossiblyUndefinedIntArrayOffset>
</file>
<file src="src/Psalm/Internal/Cli/LanguageServer.php">
<PossiblyInvalidArgument>
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,9 +656,9 @@ private static function analyzeStatement(
}

foreach ($checked_types as [$check_type_line, $is_exact]) {
[$checked_var, $check_type_string] = array_map('trim', explode('=', $check_type_line));
[$checked_var, $check_type_string] = array_map('trim', explode('=', $check_type_line, 2)) + ['', ''];

if ($check_type_string === '') {
if ($check_type_string === '' || $checked_var === '') {
IssueBuffer::maybeAdd(
new InvalidDocblock(
"Invalid format for @psalm-check-type" . ($is_exact ? "-exact" : ""),
Expand Down
18 changes: 18 additions & 0 deletions tests/CheckTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,23 @@ public function providerInvalidCodeParse(): iterable
',
'error_message' => 'Checked variable $foo? = 1 does not match $foo = 1',
];
yield 'invalidIncompleteSyntax' => [
'code' => '<?php
/** @psalm-check-type */
',
'error_message' => 'InvalidDocblock',
];
yield 'invalidIncompleteSyntaxNoVar' => [
'code' => '<?php
/** @psalm-check-type = 1 */
',
'error_message' => 'InvalidDocblock',
];
yield 'invalidIncompleteSyntaxNoType' => [
'code' => '<?php
/** @psalm-check-type $var = */
',
'error_message' => 'InvalidDocblock',
];
}
}

0 comments on commit a4d593c

Please sign in to comment.