1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 12:24:49 +01:00

Added InvalidParent issue (#2320)

Refs vimeo/psalm#2304
This commit is contained in:
Bruce Weirdan 2019-11-11 07:21:43 +02:00 committed by Matthew Brown
parent 2fc7f5fdf7
commit 79acbadfad
5 changed files with 65 additions and 0 deletions

View File

@ -208,6 +208,7 @@
<xs:element name="InvalidMethodCall" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidNullableReturnType" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidOperand" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidParent" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidPassByReference" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidPropertyAssignment" type="IssueHandlerType" minOccurs="0" />
<xs:element name="InvalidPropertyAssignmentValue" type="IssueHandlerType" minOccurs="0" />

View File

@ -717,6 +717,15 @@ Emitted when a function parameter default clashes with the type Psalm expects th
function foo(int $i = false) : void {}
```
### InvalidParent
Emitted when a function return type is `parent`, but there's no parent class
```php
class Foo {
public function f(): parent {}
}
```
### InvalidPassByReference
Emitted when passing a non-variable to a function that expects a by-ref variable

View File

@ -20,6 +20,7 @@ use Psalm\Context;
use Psalm\Internal\FileManipulation\FunctionDocblockManipulator;
use Psalm\Issue\InvalidFalsableReturnType;
use Psalm\Issue\InvalidNullableReturnType;
use Psalm\Issue\InvalidParent;
use Psalm\Issue\InvalidReturnType;
use Psalm\Issue\InvalidToString;
use Psalm\Issue\LessSpecificReturnType;
@ -657,6 +658,23 @@ class ReturnTypeAnalyzer
}
if (!$storage->signature_return_type || $storage->signature_return_type === $storage->return_type) {
foreach ($storage->return_type->getTypes() as $type) {
if ($type instanceof Type\Atomic\TNamedObject
&& 'parent' === $type->value
&& null === $parent_class
) {
if (IssueBuffer::accepts(
new InvalidParent(
'Cannot use parent as a return type when class has no parent',
$storage->return_type_location
),
$storage->suppressed_issues
)) {
return false;
}
return null;
}
}
$fleshed_out_return_type = ExpressionAnalyzer::fleshOutType(
$codebase,
$storage->return_type,

View File

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

View File

@ -1007,6 +1007,36 @@ class MethodSignatureTest extends TestCase
}',
'error_message' => 'ImplementedParamTypeMismatch',
],
'returnsParentWithNoParent' => [
'<?php
class Foo {
public function f(): parent {}
}
',
'error_message' => 'InvalidParent',
],
'returnsParentWithNoParentAndInvalidParentSuppressed' => [
'<?php
class Foo {
public function f(): parent {
}
}
',
'error_message' => 'InvalidReturnType',
2 => ['InvalidParent'],
],
// not sure how to handle it
'SKIPPED-returnsParentWithNoParentAndInvalidParentSuppressedMismatchingReturn' => [
'<?php
class Foo {
public function f(): parent {
return false;
}
}
',
'error_message' => 'InvalidReturnType',
2 => ['InvalidParent'],
],
];
}
}