Skip to content

[8.1] Support never return type #1184

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

Merged

Conversation

ghostwriter
Copy link
Member

This patch resolves #1167, via the following changes:

  • MethodDefinitionPass will not generate a return statement for a never return typehint.

A function/method that is declared with the never return type indicates that it will never return a value, and always throws an exception or terminates with a die/exit call.

Verified

This commit was signed with the committer’s verified signature.
ghostwriter Nathanael Esayeas
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@ghostwriter ghostwriter marked this pull request as draft June 7, 2022 23:03

Verified

This commit was signed with the committer’s verified signature.
ghostwriter Nathanael Esayeas
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@ghostwriter
Copy link
Member Author

I'm not sure how to go about testing the die/exit case.

Would love some feedback and or guidance.

@ghostwriter ghostwriter marked this pull request as ready for review June 8, 2022 00:28
@davedevelopment
Copy link
Collaborator

@ghostwriter this is one way, but might be too flaky to bother

diff --git tests/PHP81/Php81LanguageFeaturesTest.php tests/PHP81/Php81LanguageFeaturesTest.php
index 52b9933..59eadaf 100644
--- tests/PHP81/Php81LanguageFeaturesTest.php
+++ tests/PHP81/Php81LanguageFeaturesTest.php
@@ -92,6 +92,27 @@ class Php81LanguageFeaturesTest extends MockeryTestCase
         $this->expectException(RuntimeException::class);
         $mock->throws();
     }
+
+    /** @test */
+    public function it_can_mock_a_class_with_a_never_returning_type_hint_with_exit()
+    {
+        $mock = Mockery::mock(NeverReturningTypehintClass::class)->makePartial();
+
+        $pid = pcntl_fork();
+
+        if (-1 == $pid) {
+            $this->markTestSkipped("Couldn't fork for exit test");
+
+            return;
+        } else if ($pid) {
+            pcntl_waitpid($pid, $status);
+            $this->assertEquals(123, pcntl_wexitstatus($status));
+
+            return;
+        }
+
+        $mock->exits();
+    }
 }
 
 interface LoggerInterface
@@ -154,7 +175,7 @@ class NeverReturningTypehintClass
 
     public function exits(): never
     {
-        exit;
+        exit(123);
     }
 }
 class IntersectionTypeHelperClass

@ghostwriter ghostwriter force-pushed the bugfix/support-never-return-type branch from 26ece39 to 78e0fb7 Compare June 8, 2022 20:29
@ghostwriter
Copy link
Member Author

That's perfect @davedevelopment👌🏾, Thank you!

@ghostwriter ghostwriter force-pushed the bugfix/support-never-return-type branch from 78e0fb7 to 8a7b8fb Compare June 8, 2022 20:32

Verified

This commit was signed with the committer’s verified signature.
ghostwriter Nathanael Esayeas
Co-Authored-By: Dave Marshall <dave@atstsolutions.co.uk>
Signed-off-by: Nathanael Esayeas <nathanael.esayeas@protonmail.com>
@ghostwriter ghostwriter force-pushed the bugfix/support-never-return-type branch from 8a7b8fb to ec2443a Compare June 8, 2022 20:40
@tatupesonen
Copy link

Any chance this could be looked at? It would help us integrate tests that are currently failing as one of our projects has recently updated to PHP 8.1.

@davedevelopment davedevelopment merged commit 750ce17 into mockery:master Jul 21, 2022
@tatupesonen
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A never-returning function must not return in /vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php
3 participants