From 72423b821fd08baccf21aa803908c86d22c336f8 Mon Sep 17 00:00:00 2001 From: Ignas Rudaitis Date: Mon, 27 Jul 2026 11:48:23 +0300 Subject: [PATCH 1/5] Add test case --- tests/return-from-constructor.phpt | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/return-from-constructor.phpt diff --git a/tests/return-from-constructor.phpt b/tests/return-from-constructor.phpt new file mode 100644 index 0000000..fe8e291 --- /dev/null +++ b/tests/return-from-constructor.phpt @@ -0,0 +1,32 @@ +--TEST-- +https://github.com/antecedent/patchwork/issues/213 + +--FILE-- + +===DONE=== + +--EXPECT-- +Patchwork\Exceptions\NonNullToVoid +===DONE=== From f9be20cd5bc0f91f8064039cbff119d03451f874 Mon Sep 17 00:00:00 2001 From: Ignas Rudaitis Date: Mon, 27 Jul 2026 11:49:00 +0300 Subject: [PATCH 2/5] Treat constructors as void-typed --- src/CodeManipulation/Actions/Generic.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/CodeManipulation/Actions/Generic.php b/src/CodeManipulation/Actions/Generic.php index fb0ea54..f95a598 100644 --- a/src/CodeManipulation/Actions/Generic.php +++ b/src/CodeManipulation/Actions/Generic.php @@ -78,6 +78,10 @@ function prependCodeToFunctions($code, $typedVariants = array(), $fillArgRefs = function getDeclaredReturnType(Source $s, $function) { $parenthesis = $s->next(LEFT_ROUND, $function); + $name = $s->next(T_STRING, $function); + if ($name < $parenthesis && $s->read($name) === '__construct') { + return 'void'; + } $next = $s->skip(Source::junk(), $s->match($parenthesis)); if ($s->is(T_USE, $next)) { $next = $s->skip(Source::junk(), $s->match($s->next(LEFT_ROUND, $next))); From b2bdccbb3b3b6ecbd1f284bfe6d7371c42cc3960 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 16 Aug 2026 00:00:21 +0200 Subject: [PATCH 3/5] Add test case for destruct --- tests/return-from-constructor.phpt | 2 +- tests/return-from-destructor.phpt | 32 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/return-from-destructor.phpt diff --git a/tests/return-from-constructor.phpt b/tests/return-from-constructor.phpt index fe8e291..e87e720 100644 --- a/tests/return-from-constructor.phpt +++ b/tests/return-from-constructor.phpt @@ -1,5 +1,5 @@ --TEST-- -https://github.com/antecedent/patchwork/issues/213 +Return from constructor / antecedent/patchwork#213 --FILE-- +===DONE=== + +--EXPECT-- +Patchwork\Exceptions\NonNullToVoid +===DONE=== From 6aee010b57ed3b27ea0e9583d277176ee7863d85 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 16 Aug 2026 00:07:36 +0200 Subject: [PATCH 4/5] Maybe fix ? --- src/CodeManipulation/Actions/Generic.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CodeManipulation/Actions/Generic.php b/src/CodeManipulation/Actions/Generic.php index f95a598..2876f09 100644 --- a/src/CodeManipulation/Actions/Generic.php +++ b/src/CodeManipulation/Actions/Generic.php @@ -79,7 +79,7 @@ function getDeclaredReturnType(Source $s, $function) { $parenthesis = $s->next(LEFT_ROUND, $function); $name = $s->next(T_STRING, $function); - if ($name < $parenthesis && $s->read($name) === '__construct') { + if ($name < $parenthesis && ($s->read($name) === '__construct' || $s->read($name) === '__destruct')) { return 'void'; } $next = $s->skip(Source::junk(), $s->match($parenthesis)); From a1c3f95108f74fb940cea722ef956501b877d995 Mon Sep 17 00:00:00 2001 From: Ignas Rudaitis Date: Sun, 16 Aug 2026 10:32:03 +0300 Subject: [PATCH 5/5] Add a __destruct method to the NamedObject class used in tests --- tests/includes/NamedObject.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/includes/NamedObject.php b/tests/includes/NamedObject.php index ed075b2..d6f92a4 100644 --- a/tests/includes/NamedObject.php +++ b/tests/includes/NamedObject.php @@ -4,13 +4,17 @@ class NamedObject { private $name; - function __construct($name) + public function __construct($name) { $this->name = $name; } - function getName() + public function getName() { return $this->name; } + + public function __destruct() + { + } }