1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #1313 - don’t allow mixed function calls

This commit is contained in:
Matthew Brown 2019-02-10 16:15:52 -05:00
parent 7a1ff78bb3
commit c58100e3af
7 changed files with 44 additions and 2 deletions

View File

@ -221,6 +221,7 @@
<xs:element name="MixedArrayAssignment" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MixedArrayOffset" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MixedAssignment" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MixedFunctionCall" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MixedInferredReturnType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MixedMethodCall" type="IssueHandlerType" minOccurs="0" />
<xs:element name="MixedOperand" type="IssueHandlerType" minOccurs="0" />

View File

@ -1023,6 +1023,16 @@ Emitted when assigning a variable to a value for which Psalm cannot infer a type
$a = $_GET['foo'];
```
### MixedFunctionCall
Emitted when calling a function on a value whose type Psalm cannot infer.
```php
/** @psalm-suppress MixedAssignment */
$a = $_GET['foo'];
$a();
```
### MixedInferredReturnType
Emitted when Psalm cannot determine a function's return type

View File

@ -38,6 +38,7 @@ class Config
'MixedArrayAssignment',
'MixedArrayOffset',
'MixedAssignment',
'MixedFunctionCall',
'MixedInferredReturnType',
'MixedMethodCall',
'MixedOperand',

View File

@ -12,6 +12,7 @@ use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use Psalm\Issue\ForbiddenCode;
use Psalm\Issue\MixedFunctionCall;
use Psalm\Issue\InvalidFunctionCall;
use Psalm\Issue\NullFunctionCall;
use Psalm\Issue\PossiblyInvalidFunctionCall;
@ -119,7 +120,16 @@ class FunctionCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expressio
$has_valid_function_call_type = true;
} elseif ($var_type_part instanceof TMixed || $var_type_part instanceof TGenericParam) {
$has_valid_function_call_type = true;
// @todo maybe emit issue here
if (IssueBuffer::accepts(
new MixedFunctionCall(
'Cannot call function on mixed',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// fall through
}
} elseif ($var_type_part instanceof TCallableObject
|| $var_type_part instanceof TCallableString
) {

View File

@ -0,0 +1,7 @@
<?php
namespace Psalm\Issue;
class MixedFunctionCall extends CodeIssue
{
}

View File

@ -804,7 +804,10 @@ class CallableTest extends TestCase
$foo =
/** @param mixed $bar */
/**
* @param mixed $bar
* @psalm-suppress MixedFunctionCall
*/
function ($bar) use (&$foo): string
{
if (is_array($bar)) {

View File

@ -1882,6 +1882,16 @@ class FunctionCallTest extends TestCase
usort($a, "strcmp");',
'error_message' => 'InvalidArgument',
],
'functionCallOnMixed' => [
'<?php
/**
* @var mixed $s
* @psalm-suppress MixedAssignment
*/
$s = 1;
$s();',
'error_message' => 'MixedFunctionCall',
],
];
}
}