mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Fix #1313 - don’t allow mixed function calls
This commit is contained in:
parent
7a1ff78bb3
commit
c58100e3af
@ -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" />
|
||||
|
@ -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
|
||||
|
@ -38,6 +38,7 @@ class Config
|
||||
'MixedArrayAssignment',
|
||||
'MixedArrayOffset',
|
||||
'MixedAssignment',
|
||||
'MixedFunctionCall',
|
||||
'MixedInferredReturnType',
|
||||
'MixedMethodCall',
|
||||
'MixedOperand',
|
||||
|
@ -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
|
||||
) {
|
||||
|
7
src/Psalm/Issue/MixedFunctionCall.php
Normal file
7
src/Psalm/Issue/MixedFunctionCall.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Psalm\Issue;
|
||||
|
||||
class MixedFunctionCall extends CodeIssue
|
||||
{
|
||||
}
|
@ -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)) {
|
||||
|
@ -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',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user