Skip to content

Commit

Permalink
#2374 - Add support for object return type
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeckerson committed Aug 22, 2022
1 parent a153e83 commit c201276
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Library/ArgInfoDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,33 @@ private function richRenderStart(): void
return;
}

if ($this->functionLike->isReturnTypeObject()) {
$this->codePrinter->output('#if PHP_VERSION_ID >= 80000');
$this->codePrinter->output(
sprintf(
'ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(%s, %d, %d, %s)',
$this->name,
(int) $this->returnByRef,
$this->functionLike->getNumberOfRequiredParameters(),
'MAY_BE_OBJECT',
)
);
$this->codePrinter->output('#else');
$this->codePrinter->output(
sprintf(
'ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(%s, %d, %d, %s, %d)',
$this->name,
(int) $this->returnByRef,
$this->functionLike->getNumberOfRequiredParameters(),
'IS_OBJECT',
0,
)
);
$this->codePrinter->output('#endif');

return;
}

if (count($this->functionLike->getReturnTypes()) > 1) {
$types = [];
$mayBeTypes = $this->functionLike->getMayBeArgTypes();
Expand Down
16 changes: 16 additions & 0 deletions Library/ClassMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,11 @@ public function areReturnTypesStringCompatible(): bool
return isset($this->returnTypes['string']);
}

public function areReturnTypesObjectCompatible(): bool
{
return isset($this->returnTypes['object']);
}

/**
* Returned type hints by the method.
*
Expand Down Expand Up @@ -2323,6 +2328,7 @@ public function isReturnTypesHintDetermined(): bool
$this->areReturnTypesNullCompatible() ||
$this->areReturnTypesStringCompatible() ||
$this->areReturnTypesFalseCompatible() ||
$this->areReturnTypesObjectCompatible() ||
\array_key_exists('array', $this->getReturnTypes())
) {
continue;
Expand Down Expand Up @@ -2350,6 +2356,16 @@ public function isReturnTypeNullableObject(): bool
return count($this->returnTypes) === 2 && isset($this->returnTypes['object']) && isset($this->returnTypes['null']);
}

/**
* Checks if method's return type is object `object`.
*
* @return bool
*/
public function isReturnTypeObject(): bool
{
return count($this->returnTypes) === 1 && isset($this->returnTypes['object']);
}

/**
* Checks if the method have compatible return types.
*
Expand Down
5 changes: 5 additions & 0 deletions stub/types/obj.zep
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ class Obj
{
return new \stdClass();
}

public function objectReturn() -> object
{
return new \stdClass();
}
}

0 comments on commit c201276

Please sign in to comment.