Skip to content

Commit

Permalink
Merge pull request #10234 from tuqqu/block-mode-declare-check
Browse files Browse the repository at this point in the history
Check for declare strict_types in block mode
  • Loading branch information
orklah authored Sep 28, 2023
2 parents a8ef5a2 + 09fbe99 commit 83485f3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Psalm/Internal/Analyzer/Statements/DeclareAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public static function analyze(
$declaration_key = (string) $declaration->key;

if ($declaration_key === 'strict_types') {
if ($stmt->stmts !== null) {
IssueBuffer::maybeAdd(
new UnrecognizedStatement(
'strict_types declaration must not use block mode',
new CodeLocation($statements_analyzer, $stmt),
),
$statements_analyzer->getSuppressedIssues(),
);
}

self::analyzeStrictTypesDeclaration($statements_analyzer, $declaration, $context);
} elseif ($declaration_key === 'ticks') {
self::analyzeTicksDeclaration($statements_analyzer, $declaration);
Expand Down
25 changes: 25 additions & 0 deletions tests/Internal/Analyzer/DeclareAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,27 @@ public function providerValidCodeParse(): iterable
PHP,
];

yield 'declareTicksBlockMode' => [
'code' => <<<'PHP'
<?php declare(ticks=5) {
$foo = 'bar';
}
PHP,
];

yield 'declareEncoding' => [
'code' => <<<'PHP'
<?php declare(encoding='ISO-8859-1');
PHP,
];

yield 'declareEncodingBlockMode' => [
'code' => <<<'PHP'
<?php declare(encoding='ISO-8859-1') {
$foo = 'bar';
}
PHP,
];
}

public function providerInvalidCodeParse(): iterable
Expand All @@ -56,6 +72,15 @@ public function providerInvalidCodeParse(): iterable
'error_message' => 'UnrecognizedStatement',
];

yield 'declareStrictTypesBlockMode' => [
'code' => <<<'PHP'
<?php declare(strict_types=1) {
$foo = 'bar';
}
PHP,
'error_message' => 'UnrecognizedStatement',
];

yield 'declareInvalidValueForTicks' => [
'code' => <<<'PHP'
<?php declare(ticks='often');
Expand Down

0 comments on commit 83485f3

Please sign in to comment.