Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow operator overloading for Decimal extension (fixes #3938). #7103

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
1 change: 1 addition & 0 deletions .github/workflows/windows-ci.yml
Expand Up @@ -23,6 +23,7 @@ jobs:
php-version: '8.0'
tools: composer:v2
coverage: none
extensions: decimal
AndrolGenhald marked this conversation as resolved.
Show resolved Hide resolved

- 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::accepts(
new InvalidOperand(
"Cannot add Decimal\\Decimal to {$non_decimal_type->getId()}",
new CodeLocation($statements_source, $parent)
),
$statements_source->getSuppressedIssues()
)) {
// fall through
}
AndrolGenhald marked this conversation as resolved.
Show resolved Hide resolved
}

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