mirror of
https://github.com/danog/psalm.git
synced 2024-12-02 09:37:59 +01:00
Fix #2866 - prevent use of impure __toString via concatenation in pure contexts
This commit is contained in:
parent
3f226e2e86
commit
0a8bb32115
@ -929,6 +929,7 @@ class BinaryOpAnalyzer
|
|||||||
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
if ($stmt instanceof PhpParser\Node\Expr\BinaryOp\Equal
|
||||||
&& $stmt_left_type
|
&& $stmt_left_type
|
||||||
&& $stmt_right_type
|
&& $stmt_right_type
|
||||||
|
&& $context->mutation_free
|
||||||
) {
|
) {
|
||||||
if ($stmt_left_type->hasString() && $stmt_right_type->hasObjectType()) {
|
if ($stmt_left_type->hasString() && $stmt_right_type->hasObjectType()) {
|
||||||
foreach ($stmt_right_type->getAtomicTypes() as $atomic_type) {
|
foreach ($stmt_right_type->getAtomicTypes() as $atomic_type) {
|
||||||
@ -944,9 +945,7 @@ class BinaryOpAnalyzer
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($context->mutation_free
|
if (!$storage->mutation_free) {
|
||||||
&& !$storage->mutation_free
|
|
||||||
) {
|
|
||||||
if (IssueBuffer::accepts(
|
if (IssueBuffer::accepts(
|
||||||
new ImpureMethodCall(
|
new ImpureMethodCall(
|
||||||
'Cannot call a possibly-mutating method '
|
'Cannot call a possibly-mutating method '
|
||||||
@ -974,9 +973,7 @@ class BinaryOpAnalyzer
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($context->mutation_free
|
if (!$storage->mutation_free) {
|
||||||
&& !$storage->mutation_free
|
|
||||||
) {
|
|
||||||
if (IssueBuffer::accepts(
|
if (IssueBuffer::accepts(
|
||||||
new ImpureMethodCall(
|
new ImpureMethodCall(
|
||||||
'Cannot call a possibly-mutating method '
|
'Cannot call a possibly-mutating method '
|
||||||
@ -1834,6 +1831,36 @@ class BinaryOpAnalyzer
|
|||||||
// fall through
|
// fall through
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($context->mutation_free) {
|
||||||
|
foreach ($left_type->getAtomicTypes() as $atomic_type) {
|
||||||
|
if ($atomic_type instanceof TNamedObject) {
|
||||||
|
try {
|
||||||
|
$storage = $codebase->methods->getStorage(
|
||||||
|
new \Psalm\Internal\MethodIdentifier(
|
||||||
|
$atomic_type->value,
|
||||||
|
'__tostring'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$storage->mutation_free) {
|
||||||
|
if (IssueBuffer::accepts(
|
||||||
|
new ImpureMethodCall(
|
||||||
|
'Cannot call a possibly-mutating method '
|
||||||
|
. $atomic_type->value . '::__toString from a pure context',
|
||||||
|
new CodeLocation($statements_analyzer, $left)
|
||||||
|
),
|
||||||
|
$statements_analyzer->getSuppressedIssues()
|
||||||
|
)) {
|
||||||
|
// fall through
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($right_type->getAtomicTypes() as $right_type_part) {
|
foreach ($right_type->getAtomicTypes() as $right_type_part) {
|
||||||
@ -1880,6 +1907,36 @@ class BinaryOpAnalyzer
|
|||||||
// fall through
|
// fall through
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($context->mutation_free) {
|
||||||
|
foreach ($right_type->getAtomicTypes() as $atomic_type) {
|
||||||
|
if ($atomic_type instanceof TNamedObject) {
|
||||||
|
try {
|
||||||
|
$storage = $codebase->methods->getStorage(
|
||||||
|
new \Psalm\Internal\MethodIdentifier(
|
||||||
|
$atomic_type->value,
|
||||||
|
'__tostring'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$storage->mutation_free) {
|
||||||
|
if (IssueBuffer::accepts(
|
||||||
|
new ImpureMethodCall(
|
||||||
|
'Cannot call a possibly-mutating method '
|
||||||
|
. $atomic_type->value . '::__toString from a pure context',
|
||||||
|
new CodeLocation($statements_analyzer, $right)
|
||||||
|
),
|
||||||
|
$statements_analyzer->getSuppressedIssues()
|
||||||
|
)) {
|
||||||
|
// fall through
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$left_type_match
|
if (!$left_type_match
|
||||||
|
@ -400,7 +400,7 @@ class PureAnnotationTest extends TestCase
|
|||||||
}',
|
}',
|
||||||
'error_message' => 'ImpureStaticProperty',
|
'error_message' => 'ImpureStaticProperty',
|
||||||
],
|
],
|
||||||
'preventImpureToString' => [
|
'preventImpureToStringViaComparison' => [
|
||||||
'<?php
|
'<?php
|
||||||
class A {
|
class A {
|
||||||
public function __toString() {
|
public function __toString() {
|
||||||
@ -418,6 +418,23 @@ class PureAnnotationTest extends TestCase
|
|||||||
}',
|
}',
|
||||||
'error_message' => 'ImpureMethodCall'
|
'error_message' => 'ImpureMethodCall'
|
||||||
],
|
],
|
||||||
|
'preventImpureToStringViaConcatenation' => [
|
||||||
|
'<?php
|
||||||
|
class A {
|
||||||
|
public function __toString() {
|
||||||
|
echo "hi";
|
||||||
|
return "bar";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @psalm-pure
|
||||||
|
*/
|
||||||
|
function foo(string $s, A $a) : string {
|
||||||
|
return $a . $s;
|
||||||
|
}',
|
||||||
|
'error_message' => 'ImpureMethodCall'
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user