Skip to content
Permalink
Browse files

Added InvalidParent issue (#2320)

Refs #2304
  • Loading branch information
weirdan authored and muglug committed Nov 11, 2019
1 parent 2fc7f5f commit 79acbadfad19f089912dd24ad962b8b496b0efff
@@ -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" />
@@ -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
@@ -20,6 +20,7 @@
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 @@ public static function checkReturnType(
}
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,
@@ -0,0 +1,7 @@
<?php
namespace Psalm\Issue;
class InvalidParent extends CodeIssue
{
}
@@ -1007,6 +1007,36 @@ public function serialize() {}
}',
'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'],
],
];
}
}

0 comments on commit 79acbad

Please sign in to comment.
You can’t perform that action at this time.