From c2012763edffd6d88684afc344067162bae2fa6c Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Mon, 22 Aug 2022 16:25:59 +0100 Subject: [PATCH 1/6] #2374 - Add support for `object` return type --- Library/ArgInfoDefinition.php | 27 +++++++++++++++++++++++++++ Library/ClassMethod.php | 16 ++++++++++++++++ stub/types/obj.zep | 5 +++++ 3 files changed, 48 insertions(+) diff --git a/Library/ArgInfoDefinition.php b/Library/ArgInfoDefinition.php index 8e55bc213..e51759766 100644 --- a/Library/ArgInfoDefinition.php +++ b/Library/ArgInfoDefinition.php @@ -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(); diff --git a/Library/ClassMethod.php b/Library/ClassMethod.php index 7802cf7ce..504c2fdd5 100644 --- a/Library/ClassMethod.php +++ b/Library/ClassMethod.php @@ -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. * @@ -2323,6 +2328,7 @@ public function isReturnTypesHintDetermined(): bool $this->areReturnTypesNullCompatible() || $this->areReturnTypesStringCompatible() || $this->areReturnTypesFalseCompatible() || + $this->areReturnTypesObjectCompatible() || \array_key_exists('array', $this->getReturnTypes()) ) { continue; @@ -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. * diff --git a/stub/types/obj.zep b/stub/types/obj.zep index 65134b3ee..6d7254c71 100644 --- a/stub/types/obj.zep +++ b/stub/types/obj.zep @@ -12,4 +12,9 @@ class Obj { return new \stdClass(); } + + public function objectReturn() -> object + { + return new \stdClass(); + } } From 1659e5071f228506a7dfa97f06a7375ffe76349e Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Mon, 22 Aug 2022 16:26:23 +0100 Subject: [PATCH 2/6] Bump version to `0.16.2` --- Library/Zephir.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Zephir.php b/Library/Zephir.php index 31a6a4b97..9c26c6048 100644 --- a/Library/Zephir.php +++ b/Library/Zephir.php @@ -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' _____ __ _ From 2a27b7338f381711719135bdef63c34a4e2ae847 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Mon, 22 Aug 2022 16:26:53 +0100 Subject: [PATCH 3/6] #2374 - Generate ext/ directory --- ext/php_stub.h | 2 +- ext/stub/types/obj.zep.c | 10 ++++++++++ ext/stub/types/obj.zep.h | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ext/php_stub.h b/ext/php_stub.h index 1b5391c9f..7b25d95b7 100644 --- a/ext/php_stub.h +++ b/ext/php_stub.h @@ -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 test for
Test Extension." typedef struct _zephir_struct_db { diff --git a/ext/stub/types/obj.zep.c b/ext/stub/types/obj.zep.c index d2b119f0c..c7c5aea67 100644 --- a/ext/stub/types/obj.zep.c +++ b/ext/stub/types/obj.zep.c @@ -37,6 +37,16 @@ PHP_METHOD(Stub_Types_Obj, nullableObjectReturnObj) + object_init(return_value); + return; +} + +PHP_METHOD(Stub_Types_Obj, objectReturn) +{ + zval *this_ptr = getThis(); + + + object_init(return_value); return; } diff --git a/ext/stub/types/obj.zep.h b/ext/stub/types/obj.zep.h index 6854113fa..a799f6ff6 100644 --- a/ext/stub/types/obj.zep.h +++ b/ext/stub/types/obj.zep.h @@ -5,6 +5,7 @@ ZEPHIR_INIT_CLASS(Stub_Types_Obj); PHP_METHOD(Stub_Types_Obj, nullableObjectReturnNull); PHP_METHOD(Stub_Types_Obj, nullableObjectReturnObj); +PHP_METHOD(Stub_Types_Obj, objectReturn); #if PHP_VERSION_ID >= 80000 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_stub_types_obj_nullableobjectreturnnull, 0, 0, MAY_BE_NULL|MAY_BE_OBJECT) @@ -20,8 +21,16 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stub_types_obj_nullableobjectret #endif ZEND_END_ARG_INFO() +#if PHP_VERSION_ID >= 80000 +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_stub_types_obj_objectreturn, 0, 0, MAY_BE_OBJECT) +#else +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_stub_types_obj_objectreturn, 0, 0, IS_OBJECT, 0) +#endif +ZEND_END_ARG_INFO() + ZEPHIR_INIT_FUNCS(stub_types_obj_method_entry) { PHP_ME(Stub_Types_Obj, nullableObjectReturnNull, arginfo_stub_types_obj_nullableobjectreturnnull, ZEND_ACC_PUBLIC) PHP_ME(Stub_Types_Obj, nullableObjectReturnObj, arginfo_stub_types_obj_nullableobjectreturnobj, ZEND_ACC_PUBLIC) + PHP_ME(Stub_Types_Obj, objectReturn, arginfo_stub_types_obj_objectreturn, ZEND_ACC_PUBLIC) PHP_FE_END }; From 27b663ced58a8ea2df07117610418ea403ad668e Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Mon, 22 Aug 2022 16:29:42 +0100 Subject: [PATCH 4/6] #2374 - Update CHANGELOG.md --- CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f720297df..3e64b7cdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) @@ -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 From e57e2f2260817b2f038341d7cd4cf98de84ee2f2 Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Mon, 22 Aug 2022 16:31:26 +0100 Subject: [PATCH 5/6] #2374 - Extract common parts from condition --- Library/ClassMethod.php | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/Library/ClassMethod.php b/Library/ClassMethod.php index 504c2fdd5..01e8354bf 100644 --- a/Library/ClassMethod.php +++ b/Library/ClassMethod.php @@ -2229,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; From 3cfa8a69e238c9c793eea41749a415a84f5dc6ee Mon Sep 17 00:00:00 2001 From: Anton Vasiliev Date: Mon, 22 Aug 2022 16:42:44 +0100 Subject: [PATCH 6/6] #2374 - Add test case --- tests/Extension/Types/ObjTypeTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Extension/Types/ObjTypeTest.php b/tests/Extension/Types/ObjTypeTest.php index 0cab73519..cf3539fdf 100644 --- a/tests/Extension/Types/ObjTypeTest.php +++ b/tests/Extension/Types/ObjTypeTest.php @@ -25,5 +25,6 @@ public function testIntFalse(): void $this->assertNull($class->nullableObjectReturnNull()); $this->assertInstanceOf(stdClass::class, $class->nullableObjectReturnObj()); + $this->assertInstanceOf(stdClass::class, $class->objectReturn()); } }