Skip to content
Open
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
20 changes: 20 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,10 @@
return $this->getNeverType($leftType, $rightType);
}

if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) {

Check warning on line 996 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if (!(new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->no()) { return new ErrorType(); }

Check warning on line 996 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if ($leftType->isSuperTypeOf(new ObjectType('BcMath\Number'))->yes()) { return new ErrorType(); }

Check warning on line 996 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if (!(new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->no()) { return new ErrorType(); }

Check warning on line 996 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if ($leftType->isSuperTypeOf(new ObjectType('BcMath\Number'))->yes()) { return new ErrorType(); }
return new ErrorType();
}

$result = $this->getFiniteOrConstantScalarTypes($leftType, $rightType, static fn ($a, $b) => $a & $b);
if ($result instanceof Type) {
return $result;
Expand Down Expand Up @@ -1051,6 +1055,10 @@
return $this->getNeverType($leftType, $rightType);
}

if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) {

Check warning on line 1058 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if (!(new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->no()) { return new ErrorType(); }

Check warning on line 1058 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if ($leftType->isSuperTypeOf(new ObjectType('BcMath\Number'))->yes()) { return new ErrorType(); }

Check warning on line 1058 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if (!(new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->no()) { return new ErrorType(); }

Check warning on line 1058 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if ($leftType->isSuperTypeOf(new ObjectType('BcMath\Number'))->yes()) { return new ErrorType(); }
return new ErrorType();
}

$result = $this->getFiniteOrConstantScalarTypes($leftType, $rightType, static fn ($a, $b) => $a | $b);
if ($result instanceof Type) {
return $result;
Expand Down Expand Up @@ -1159,6 +1167,10 @@
return $this->getNeverType($leftType, $rightType);
}

if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) {

Check warning on line 1170 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if (!(new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->no()) { return new ErrorType(); }

Check warning on line 1170 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if ($leftType->isSuperTypeOf(new ObjectType('BcMath\Number'))->yes()) { return new ErrorType(); }

Check warning on line 1170 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if (!(new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->no()) { return new ErrorType(); }

Check warning on line 1170 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if ($leftType->isSuperTypeOf(new ObjectType('BcMath\Number'))->yes()) { return new ErrorType(); }
return new ErrorType();
}

$result = $this->getFiniteOrConstantScalarTypes($leftType, $rightType, static fn ($a, $b) => $a ^ $b);
if ($result instanceof Type) {
return $result;
Expand Down Expand Up @@ -1765,6 +1777,10 @@
return $this->getNeverType($leftType, $rightType);
}

if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) {

Check warning on line 1780 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if (!(new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->no()) { return new ErrorType(); }

Check warning on line 1780 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if ($leftType->isSuperTypeOf(new ObjectType('BcMath\Number'))->yes()) { return new ErrorType(); }

Check warning on line 1780 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if (!(new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->no()) { return new ErrorType(); }

Check warning on line 1780 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if ($leftType->isSuperTypeOf(new ObjectType('BcMath\Number'))->yes()) { return new ErrorType(); }
return new ErrorType();
}

$leftTypes = $leftType->getConstantScalarTypes();
$rightTypes = $rightType->getConstantScalarTypes();
$leftTypesCount = count($leftTypes);
Expand Down Expand Up @@ -1829,6 +1845,10 @@
return $this->getNeverType($leftType, $rightType);
}

if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) {

Check warning on line 1848 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if (!(new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->no()) { return new ErrorType(); }

Check warning on line 1848 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if ($leftType->isSuperTypeOf(new ObjectType('BcMath\Number'))->yes()) { return new ErrorType(); }

Check warning on line 1848 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if (!(new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->no()) { return new ErrorType(); }

Check warning on line 1848 in src/Reflection/InitializerExprTypeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ return $this->getNeverType($leftType, $rightType); } - if ((new ObjectType('BcMath\Number'))->isSuperTypeOf($leftType)->yes()) { + if ($leftType->isSuperTypeOf(new ObjectType('BcMath\Number'))->yes()) { return new ErrorType(); }
return new ErrorType();
}

$leftTypes = $leftType->getConstantScalarTypes();
$rightTypes = $rightType->getConstantScalarTypes();
$leftTypesCount = count($leftTypes);
Expand Down
4 changes: 4 additions & 0 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,10 @@ public function toNumber(): Type
]);
}

if ($this->isInstanceOf('BcMath\Number')->yes()) {
return new ObjectType('BcMath\Number');
}

return new ErrorType();
}

Expand Down
13 changes: 10 additions & 3 deletions tests/PHPStan/Analyser/nsrt/bcmath-number.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ public function bcVsNever(Number $a): void
{
for ($b = 1; $b < count([]); $b++) {
assertType('*NEVER*', $a + $b);
assertType('*ERROR*', $a - $b); // Inconsistency: getPlusType handles never types right at the beginning, getMinusType doesn't.
assertType('*ERROR*', $a * $b);
assertType('*ERROR*', $a / $b);
assertType('*NEVER*', $a - $b);
assertType('*NEVER*', $a * $b);
assertType('*NEVER*', $a / $b);
assertType('*NEVER*', $a % $b);
assertType('non-empty-string&numeric-string', $a . $b);
assertType('*ERROR*', $a ** $b);
Expand Down Expand Up @@ -413,4 +413,11 @@ public function bcIncDec(Number $a): void
assertType('BcMath\Number', --$a);
assertType('BcMath\Number', $a--);
}

public function bcUnaryOp(Number $a): void
{
assertType('BcMath\Number', +$a);
assertType('BcMath\Number', -$a);
assertType('*ERROR*', ~$a);
}
}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Operators/InvalidUnaryOperationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,15 @@ public function testUnion(): void
]);
}

#[RequiresPhp('>= 8.4')]
public function testBcMathNumber(): void
{
$this->analyse([__DIR__ . '/data/unary-bcmath-number.php'], [
[
'Unary operation "~" on BcMath\Number results in an error.',
16,
],
]);
}

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

namespace BcMathNumberUnaryOperators;

use BcMath\Number;

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

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

function testBitwiseNot(Number $x): void {
~$x;
}
Loading