Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2374 - Add support for object return type #2382

Merged
merged 6 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]

## [0.16.2] - 2022-08-22
### Added
- Added support for `object` return type [#2374](https://github.com/zephir-lang/zephir/issues/2374)

## [0.16.1] - 2022-08-21
### Changed
- Changed usage of `utf8_decode()` function in favour of `mb_convert_encoding()` [#2376](https://github.com/zephir-lang/zephir/issues/2376)
Expand Down Expand Up @@ -579,7 +583,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
[#1524](https://github.com/zephir-lang/zephir/issues/1524)


[Unreleased]: https://github.com/zephir-lang/zephir/compare/0.16.0...HEAD
[Unreleased]: https://github.com/zephir-lang/zephir/compare/0.16.2...HEAD
[0.16.2]: https://github.com/zephir-lang/zephir/compare/0.16.1...0.16.2
[0.16.1]: https://github.com/zephir-lang/zephir/compare/0.16.0...0.16.1
[0.16.0]: https://github.com/zephir-lang/zephir/compare/0.15.2...0.16.0
[0.15.2]: https://github.com/zephir-lang/zephir/compare/0.15.1...0.15.2
Expand Down
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
37 changes: 23 additions & 14 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 @@ -2224,24 +2229,17 @@ public function hasChildReturnStatementType(array $statement): bool
}

$statements = $statement['else_statements'];
foreach ($statements as $item) {
$type = $item['type'] ?? null;
if ('return' === $type || 'throw' === $type) {
return true;
}

return $this->hasChildReturnStatementType($item);
}
} else {
$statements = $statement['statements'];
foreach ($statements as $item) {
$type = $item['type'] ?? null;
if ('return' === $type || 'throw' === $type) {
return true;
}
}

return $this->hasChildReturnStatementType($item);
foreach ($statements as $item) {
$type = $item['type'] ?? null;
if ('return' === $type || 'throw' === $type) {
return true;
}

return $this->hasChildReturnStatementType($item);
}

return false;
Expand Down Expand Up @@ -2323,6 +2321,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 +2349,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
2 changes: 1 addition & 1 deletion Library/Zephir.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

final class Zephir
{
public const VERSION = '0.16.0-$Id$';
public const VERSION = '0.16.2-$Id$';

public const LOGO = <<<'ASCII'
_____ __ _
Expand Down
2 changes: 1 addition & 1 deletion ext/php_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#define PHP_STUB_VERSION "1.0.0"
#define PHP_STUB_EXTNAME "stub"
#define PHP_STUB_AUTHOR "Phalcon Team and contributors"
#define PHP_STUB_ZEPVERSION "0.16.0-$Id$"
#define PHP_STUB_ZEPVERSION "0.16.2-$Id$"
#define PHP_STUB_DESCRIPTION "Description <b>test</b> for<br/>Test Extension."

typedef struct _zephir_struct_db {
Expand Down
10 changes: 10 additions & 0 deletions ext/stub/types/obj.zep.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions ext/stub/types/obj.zep.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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();
}
}
1 change: 1 addition & 0 deletions tests/Extension/Types/ObjTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public function testIntFalse(): void

$this->assertNull($class->nullableObjectReturnNull());
$this->assertInstanceOf(stdClass::class, $class->nullableObjectReturnObj());
$this->assertInstanceOf(stdClass::class, $class->objectReturn());
}
}