1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Forbid constructors from returning any values

Fixes vimeo/psalm#9713
This commit is contained in:
Bruce Weirdan 2024-02-10 01:28:19 +01:00
parent ba4e312594
commit 6b405937ab
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
3 changed files with 25 additions and 1 deletions

View File

@ -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>
@ -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>

View File

@ -257,6 +257,18 @@ final class ReturnAnalyzer
$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;
}

View File

@ -1863,6 +1863,17 @@ class ReturnTypeTest extends TestCase
'ignored_issues' => [],
'php_version' => '8.1',
],
'constructorsShouldReturnVoid' => [
'code' => <<<'PHP'
<?php
class A {
public function __construct() {
return 5;
}
}
PHP,
'error_message' => 'InvalidReturnStatement',
],
];
}
}