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

Consider print, and conditionally exit / die impure #6188

Merged
merged 1 commit into from
Jul 27, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Call\ArgumentAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Codebase\TaintFlowGraph;
use Psalm\Internal\DataFlow\TaintSink;
use Psalm\Issue\ImpureFunctionCall;
use Psalm\IssueBuffer;
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Type;
use Psalm\Type\Atomic\TInt;
Expand All @@ -21,6 +24,8 @@ public static function analyze(
PhpParser\Node\Expr\Exit_ $stmt,
Context $context
) : bool {
$expr_type = null;

if ($stmt->expr) {
$context->inside_call = true;

Expand Down Expand Up @@ -80,7 +85,32 @@ public static function analyze(
$context->inside_call = false;
}

$statements_analyzer->node_data->setType($stmt, \Psalm\Type::getEmpty());
if ($expr_type
&& !$expr_type->isInt()
&& !$context->collect_mutations
&& !$context->collect_initializations
) {
if ($context->mutation_free || $context->external_mutation_free) {
$function_name = $stmt->getAttribute('kind') === PhpParser\Node\Expr\Exit_::KIND_DIE ? 'die' : 'exit';

if (IssueBuffer::accepts(
new ImpureFunctionCall(
'Cannot call ' . $function_name . ' with a non-integer argument from a mutation-free context',
new CodeLocation($statements_analyzer, $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($statements_analyzer->getSource() instanceof FunctionLikeAnalyzer
&& $statements_analyzer->getSource()->track_mutations
) {
$statements_analyzer->getSource()->inferred_has_mutation = true;
$statements_analyzer->getSource()->inferred_impure = true;
}
}

$statements_analyzer->node_data->setType($stmt, Type::getEmpty());

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Codebase\TaintFlowGraph;
use Psalm\Internal\DataFlow\TaintSink;
use Psalm\Issue\ForbiddenCode;
use Psalm\Issue\ImpureFunctionCall;
use Psalm\IssueBuffer;
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Type;
Expand Down Expand Up @@ -82,6 +84,25 @@ public static function analyze(
}
}

if (!$context->collect_initializations && !$context->collect_mutations) {
if ($context->mutation_free || $context->external_mutation_free) {
if (IssueBuffer::accepts(
new ImpureFunctionCall(
'Cannot call print from a mutation-free context',
new CodeLocation($statements_analyzer, $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($statements_analyzer->getSource() instanceof FunctionLikeAnalyzer
&& $statements_analyzer->getSource()->track_mutations
) {
$statements_analyzer->getSource()->inferred_has_mutation = true;
$statements_analyzer->getSource()->inferred_impure = true;
}
}

$statements_analyzer->node_data->setType($stmt, Type::getInt(false, 1));

return true;
Expand Down
59 changes: 59 additions & 0 deletions tests/PureAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,38 @@ function foo(array $ar): int
return $ar[0] ?? 0;
}',
],
'exitFunctionWithNoArgumentIsPure' => [
'<?php
/** @psalm-pure */
function foo(): void {
exit;
}
',
],
'exitFunctionWithIntegerArgumentIsPure' => [
'<?php
/** @psalm-pure */
function foo(): void {
exit(0);
}
',
],
'dieFunctionWithNoArgumentIsPure' => [
'<?php
/** @psalm-pure */
function foo(): void {
die;
}
',
],
'dieFunctionWithIntegerArgumentIsPure' => [
'<?php
/** @psalm-pure */
function foo(): void {
die(0);
}
',
],
'allowPureToString' => [
'<?php
class A {
Expand Down Expand Up @@ -541,6 +573,33 @@ function foo(array $ar): int
}',
'error_message' => 'ImpureFunctionCall',
],
'printFunctionIsImpure' => [
'<?php
/** @psalm-pure */
function foo(): void {
print("x");
}
',
'error_message' => 'ImpureFunctionCall',
],
'exitFunctionWithNonIntegerArgumentIsImpure' => [
'<?php
/** @psalm-pure */
function foo(): void {
exit("x");
}
',
'error_message' => 'ImpureFunctionCall',
],
'dieFunctionWithNonIntegerArgumentIsImpure' => [
'<?php
/** @psalm-pure */
function foo(): void {
die("x");
}
',
'error_message' => 'ImpureFunctionCall',
],
'impureByRef' => [
'<?php
/**
Expand Down