Skip to content

Commit

Permalink
Merge pull request vimeo#10693 from weirdan/update-master
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Feb 11, 2024
2 parents 1d53427 + 76c0262 commit cb0e6a1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,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 @@ -277,6 +277,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
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,9 @@ private function getTranslatedFunctionParam(
$this->codebase->analysis_php_version_id,
);

if ($is_nullable) {
if ($param_type->isMixed()) {
$is_nullable = false;
} elseif ($is_nullable) {
$param_type = $param_type->getBuilder()->addType(new TNull)->freeze();
} else {
$is_nullable = $param_type->isNullable();
Expand Down
14 changes: 14 additions & 0 deletions tests/ArgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,20 @@ function foo($a, ...$b) {}
var_caller("foo");',
],
'mixedNullable' => [
'code' => '<?php
class A {
public function __construct(public mixed $default = null) {
}
}
$a = new A;
$_v = $a->default;',
'assertions' => [
'$_v===' => 'mixed',
],
'ignored_issues' => [],
'php_version' => '8.0',
],
];
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1914,6 +1914,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 cb0e6a1

Please sign in to comment.