Skip to content

Commit

Permalink
Merge pull request #7103 from AndrolGenhald/feature/3938-decimal-oper…
Browse files Browse the repository at this point in the history
…ator-overloads
  • Loading branch information
weirdan committed Dec 8, 2021
2 parents b65e37c + 466696c commit eb2f4dc
Show file tree
Hide file tree
Showing 5 changed files with 611 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Expand Up @@ -81,6 +81,7 @@ jobs:
php-version: '8.0'
tools: composer:v2
coverage: none
extensions: decimal

- uses: actions/checkout@v2

Expand Down
5 changes: 5 additions & 0 deletions src/Psalm/Config.php
Expand Up @@ -1969,6 +1969,11 @@ public function visitStubFiles(Codebase $codebase, ?Progress $progress = null):
$this->internal_stubs[] = $ext_mysqli_path;
}

if (extension_loaded('decimal')) {
$ext_decimal_path = $dir_lvl_2 . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'decimal.phpstub';
$this->internal_stubs[] = $ext_decimal_path;
}

foreach ($this->internal_stubs as $stub_path) {
if (!file_exists($stub_path)) {
throw new UnexpectedValueException('Cannot locate ' . $stub_path);
Expand Down
Expand Up @@ -36,6 +36,7 @@
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TNumeric;
use Psalm\Type\Atomic\TNumericString;
use Psalm\Type\Atomic\TPositiveInt;
use Psalm\Type\Atomic\TTemplateParam;

Expand Down Expand Up @@ -611,6 +612,49 @@ private static function analyzeOperands(
return null;
}

if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Plus
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\Minus
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\Mul
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\Div
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\Mod
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\Pow
) {
$non_decimal_type = null;
if ($left_type_part instanceof TNamedObject
&& strtolower($left_type_part->value) === "decimal\\decimal"
) {
$non_decimal_type = $right_type_part;
} elseif ($right_type_part instanceof TNamedObject
&& strtolower($right_type_part->value) === "decimal\\decimal"
) {
$non_decimal_type = $left_type_part;
}
if ($non_decimal_type !== null) {
if ($non_decimal_type instanceof TInt
|| $non_decimal_type instanceof TNumericString
|| $non_decimal_type instanceof TNamedObject
&& strtolower($non_decimal_type->value) === "decimal\\decimal"
) {
$result_type = Type::combineUnionTypes(
new Type\Union([new TNamedObject("Decimal\\Decimal")]),
$result_type
);
} else {
if ($statements_source) {
IssueBuffer::maybeAdd(
new InvalidOperand(
"Cannot add Decimal\\Decimal to {$non_decimal_type->getId()}",
new CodeLocation($statements_source, $parent)
),
$statements_source->getSuppressedIssues()
);
}
}

return null;
}
}

if ($left_type_part instanceof Type\Atomic\TLiteralString) {
if (preg_match('/^\-?\d+$/', $left_type_part->value)) {
$left_type_part = new Type\Atomic\TLiteralInt((int) $left_type_part->value);
Expand Down

0 comments on commit eb2f4dc

Please sign in to comment.