Skip to content

Commit

Permalink
Merge pull request #7872 from hirokinoue/float-template-arithmetics-4.x
Browse files Browse the repository at this point in the history
don't emit issues when doing arithmetics on float templates
  • Loading branch information
orklah committed Apr 19, 2022
2 parents 55a45a9 + a525342 commit 2724c1d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -445,20 +445,26 @@ private static function analyzeOperands(
}

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()
Expand Down
16 changes: 16 additions & 0 deletions tests/Template/FunctionTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,22 @@ function foo($p): void {
}
}'
],
'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;
}'
],
];
}

Expand Down

0 comments on commit 2724c1d

Please sign in to comment.