Skip to content

Commit

Permalink
Merge pull request #10686 from weirdan/forbid-constructor-return-values
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Feb 10, 2024
2 parents ba4e312 + 6b40593 commit 2a063ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 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="5.x-dev@76364ab2ccde9930513b0e3cc7e1757d3d0469f1">
<files psalm-version="5.x-dev@ba4e312594006059b0d9afb0c5ebeea649a59112">
<file src="vendor/nikic/php-parser/lib/PhpParser/Node/Expr/ArrowFunction.php">
<PossiblyUndefinedStringArrayOffset>
<code><![CDATA[$subNodes['expr']]]></code>
Expand Down Expand Up @@ -1057,6 +1057,7 @@
<file src="src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php">
<PossiblyUndefinedIntArrayOffset>
<code><![CDATA[$method_name]]></code>
<code><![CDATA[$method_name]]></code>
</PossiblyUndefinedIntArrayOffset>
<RiskyTruthyFalsyComparison>
<code><![CDATA[!$context->calling_function_id]]></code>
Expand Down
12 changes: 12 additions & 0 deletions src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ public static function analyze(
$statements_analyzer,
null,
);

[, $method_name] = explode('::', $cased_method_id);
if ($method_name === '__construct') {
IssueBuffer::maybeAdd(
new InvalidReturnStatement(
'No return values are expected for ' . $cased_method_id,
new CodeLocation($source, $stmt->expr),
),
$statements_analyzer->getSuppressedIssues(),
);
return;
}
} else {
$declared_return_type = $storage->return_type;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/ReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,17 @@ function foo(bool $x): never
'ignored_issues' => [],
'php_version' => '8.1',
],
'constructorsShouldReturnVoid' => [
'code' => <<<'PHP'
<?php
class A {
public function __construct() {
return 5;
}
}
PHP,
'error_message' => 'InvalidReturnStatement',
],
];
}
}

0 comments on commit 2a063ff

Please sign in to comment.