1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Restrict pure annotation addition a little more

This commit is contained in:
Matthew Brown 2020-08-23 13:34:32 -04:00 committed by Daniil Gentili
parent 0c3306b002
commit 601a8fdcb6
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 46 additions and 2 deletions

View File

@ -582,6 +582,9 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
if ($codebase->alter_code
&& isset($project_analyzer->getIssuesToFix()['MissingPureAnnotation'])
&& !$this->inferred_impure
&& ($this->function instanceof Function_
|| ($this->function instanceof ClassMethod
&& $this->function->isStatic()))
) {
$manipulator = FunctionDocblockManipulator::getForFunction(
$project_analyzer,

View File

@ -1226,7 +1226,8 @@ class FunctionCallAnalyzer extends CallAnalyzer
&& ($context->mutation_free
|| $context->external_mutation_free
|| $codebase->find_unused_variables
|| !$config->remember_property_assignments_after_call)
|| !$config->remember_property_assignments_after_call
|| $codebase->alter_code)
) {
$must_use = true;

View File

@ -359,7 +359,7 @@ class FunctionDocblockManipulator
$old_phpdoc_return_type = null;
if (isset($parsed_docblock->tags['return'])) {
$old_phpdoc_return_type = array_shift($parsed_docblock->tags['return']);
$old_phpdoc_return_type = reset($parsed_docblock->tags['return']);
}
if ($this->is_pure) {

View File

@ -25,6 +25,27 @@ class PureAnnotationAdditionTest extends FileManipulationTest
['MissingPureAnnotation'],
true,
],
'addPureAnnotationToFunctionWithExistingDocblock' => [
'<?php
/**
* @return string
*/
function foo(string $s) {
return $s;
}',
'<?php
/**
* @return string
*
* @psalm-pure
*/
function foo(string $s) {
return $s;
}',
'7.4',
['MissingPureAnnotation'],
true,
],
'dontAddPureAnnotationToImpureFunction' => [
'<?php
function foo(string $s): string {
@ -61,6 +82,25 @@ class PureAnnotationAdditionTest extends FileManipulationTest
['MissingPureAnnotation'],
true,
],
'dontAddPureAnnotationToFunctionWithImpureCall' => [
'<?php
function foo(string $s): string {
if (file_exists($s)) {
return "";
}
return $s;
}',
'<?php
function foo(string $s): string {
if (file_exists($s)) {
return "";
}
return $s;
}',
'7.4',
['MissingPureAnnotation'],
true,
],
];
}
}