Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Rules/Operators/InvalidIncDecOperationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public function processNode(Node $node, Scope $scope): array
$deprecatedBool = true;
}

if ($this->phpVersion->supportsBcMathNumberOperatorOverloading()) {
$allowedTypes[] = new ObjectType('BcMath\Number');
}

$allowedTypes = new UnionType($allowedTypes);

$varType = $this->ruleLevelHelper->findTypeToCheck(
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bcmath-number.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,12 @@ public function bcVsNever(Number $a): void
assertType('bool', $a or $b);
}
}

public function bcIncDec(Number $a): void
{
assertType('BcMath\Number', ++$a);
assertType('BcMath\Number', $a++);
assertType('BcMath\Number', --$a);
assertType('BcMath\Number', $a--);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,10 @@ public function testIncNonNumericString(): void
$this->analyse([__DIR__ . '/data/inc-non-numeric-string.php'], $errors);
}

#[RequiresPhp('>= 8.4')]
public function testBcMathNumber(): void
{
$this->analyse([__DIR__ . '/data/inc-dec-bcmath-number.php'], []);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to run this before 8.4 too, and set the expected errors.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the author, the test was reporting no error for version before 8.4 (cf #4957 (comment))

Looking at the playground https://phpstan.org/r/bf941228-962c-42af-a859-b909918634a7, I assume that the type is "ERROR" and no error is reported when doing increment/decrement on ErrorType.

So I thought this wasn't a relevant test to add

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh it's a new class. I get it, thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW in that playground enabling strict-rules shows more errors. I'll take a look tomorrow.

}

}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Operators/data/inc-dec-bcmath-number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace IncDecBcMathNumber;

use BcMath\Number;

function testPreInc(Number $x): void {
++$x;
}

function testPostInc(Number $x): void {
$x++;
}

function testPreDec(Number $x): void {
--$x;
}

function testPostDec(Number $x): void {
$x--;
}
Loading