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

Merge pull request #7872 from hirokinoue/float-template-arithmetics-4.x

don't emit issues when doing arithmetics on float templates
This commit is contained in:
orklah 2022-04-19 19:35:57 +02:00 committed by GitHub
commit 2724c1dba9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View File

@ -445,20 +445,26 @@ class ArithmeticOpAnalyzer
}
if ($left_type_part instanceof TTemplateParam || $right_type_part instanceof TTemplateParam) {
if ($left_type_part instanceof TTemplateParam && !$left_type_part->as->isInt()) {
if ($left_type_part instanceof TTemplateParam
&& !$left_type_part->as->isInt()
&& !$left_type_part->as->isFloat()
) {
if ($statements_source && IssueBuffer::accepts(
new MixedOperand(
'Left operand cannot be a non-int template',
'Left operand cannot be a non-numeric template',
new CodeLocation($statements_source, $left)
),
$statements_source->getSuppressedIssues()
)) {
// fall through
}
} elseif ($right_type_part instanceof TTemplateParam && !$right_type_part->as->isInt()) {
} elseif ($right_type_part instanceof TTemplateParam
&& !$right_type_part->as->isInt()
&& !$right_type_part->as->isFloat()
) {
if ($statements_source && IssueBuffer::accepts(
new MixedOperand(
'Right operand cannot be a non-int template',
'Right operand cannot be a non-numeric template',
new CodeLocation($statements_source, $right)
),
$statements_source->getSuppressedIssues()

View File

@ -1603,6 +1603,22 @@ class FunctionTemplateTest extends TestCase
}
}'
],
'dontScreamForArithmeticsOnFloatTemplates' => [
'<?php
/**
* @template T of ?float
* @param T $p
* @return (T is null ? null : float)
*/
function foo(?float $p): ?float
{
if ($p === null) {
return null;
}
return $p - 1;
}'
],
];
}