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

Excluding all enum cases one by one does not simplify a return type when using getAllowedSubTypes involving an enum #10148

Open
stof opened this issue Nov 17, 2023 · 8 comments
Labels
Milestone

Comments

@stof
Copy link
Contributor

stof commented Nov 17, 2023

Bug report

When an extension implements getAllowedSubTypes to define that the only allowed subtypes of an interface are a given class and an enum, I would except that handling each case of the enum would leave us with a type refined to the other class, just like it does for union types.

Code snippet that reproduces the problem

https://phpstan.org/r/deeced13-4be2-4ce2-86d8-ee547748f18a

Expected output

When running that with the jiripudil/phpstan-sealed-classes extension installed, I would expect no error.
This extension defines an AllowedSubTypesClassReflectionExtension reading the Sealed attribute to provide the allowed subtypes.

Replacing the sealed interface (for which the extension defines the 2 allowed subtypes) with a union type of the 2 allowed subtypes does not report the error: https://phpstan.org/r/0c528e9b-ab4d-4811-ba45-dbe16b42ffac

Did PHPStan help you today? Did it make you happy in any way?

No response

@ondrejmirtes
Copy link
Member

Hi, could you please prove the point of this bug without any extension involved on phpstan.org/try? For example with \PHPStan\dumpType calls.

@ondrejmirtes
Copy link
Member

Oh I get it. We need to update ObjectType::getEnumCases() so that it reads ClassReflection::getAllowedSubTypes().

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Nov 29, 2023
@stof
Copy link
Contributor Author

stof commented Nov 29, 2023

I don't see how to reproduce it on the payground as it involves ClassReflection::getAllowedSubTypes()

@ondrejmirtes
Copy link
Member

Yeah sure, I realized you can't.

@phpstan-bot
Copy link
Contributor

@stof After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.1 – 8.3 (3 errors)
+PHP 8.1 – 8.3 (6 errors)
 ==========
 
  5: Attribute class JiriPudil\SealedClasses\Sealed does not exist.
 48: Call to an undefined method ColorFormat::getOriginal().
 61: Call to an undefined method ColorFormat::getOriginal().
+65: Function writeRgb() returns void but does not have any side effects.
+68: Function writeHsl() returns void but does not have any side effects.
+72: Function write() returns void but does not have any side effects.
 
 PHP 7.2 – 8.0 (1 error)
 ==========
 
 11: Syntax error, unexpected T_STRING on line 11
Full report

PHP 8.1 – 8.3 (6 errors)

Line Error
5 Attribute class JiriPudil\SealedClasses\Sealed does not exist.
48 Call to an undefined method ColorFormat::getOriginal().
61 Call to an undefined method ColorFormat::getOriginal().
65 Function writeRgb() returns void but does not have any side effects.
68 Function writeHsl() returns void but does not have any side effects.
72 Function write() returns void but does not have any side effects.

PHP 7.2 – 8.0 (1 error)

Line Error
11 Syntax error, unexpected T_STRING on line 11

@phpstan-bot
Copy link
Contributor

@stof After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-PHP 8.1 – 8.3
+PHP 8.1 – 8.3 (3 errors)
 ==========
 
-No errors
+62: Function writeRgb() returns void but does not have any side effects.
+65: Function writeHsl() returns void but does not have any side effects.
+69: Function write() returns void but does not have any side effects.
 
 PHP 7.2 – 8.0 (1 error)
 ==========
 
  8: Syntax error, unexpected T_STRING on line 8
Full report

PHP 8.1 – 8.3 (3 errors)

Line Error
62 Function writeRgb() returns void but does not have any side effects.
65 Function writeHsl() returns void but does not have any side effects.
69 Function write() returns void but does not have any side effects.

PHP 7.2 – 8.0 (1 error)

Line Error
8 Syntax error, unexpected T_STRING on line 8

@phpstan-bot
Copy link
Contributor

@stof After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
 48: Call to an undefined method ColorFormat::getOriginal().
 61: Call to an undefined method ColorFormat::getOriginal().
 
-PHP 7.2 – 8.0 (1 error)
+PHP 8.0 (2 errors)
+==========
+
+11: Syntax error, unexpected T_STRING on line 11
+25: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 25
+
+PHP 7.2 – 7.4 (1 error)
 ==========
 
 11: Syntax error, unexpected T_STRING on line 11
Full report

PHP 8.1 – 8.3 (3 errors)

Line Error
5 Attribute class JiriPudil\SealedClasses\Sealed does not exist.
48 Call to an undefined method ColorFormat::getOriginal().
61 Call to an undefined method ColorFormat::getOriginal().

PHP 8.0 (2 errors)

Line Error
11 Syntax error, unexpected T_STRING on line 11
25 Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 25

PHP 7.2 – 7.4 (1 error)

Line Error
11 Syntax error, unexpected T_STRING on line 11

@phpstan-bot
Copy link
Contributor

@stof After the latest push in 2.0.x, PHPStan now reports different result with your code snippet:

@@ @@
 
 No errors
 
-PHP 7.2 – 8.0 (1 error)
+PHP 8.0 (2 errors)
+==========
+
+ 8: Syntax error, unexpected T_STRING on line 8
+22: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 22
+
+PHP 7.2 – 7.4 (1 error)
 ==========
 
  8: Syntax error, unexpected T_STRING on line 8
Full report

PHP 8.1 – 8.3

No errors

PHP 8.0 (2 errors)

Line Error
8 Syntax error, unexpected T_STRING on line 8
22 Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 22

PHP 7.2 – 7.4 (1 error)

Line Error
8 Syntax error, unexpected T_STRING on line 8

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

No branches or pull requests

3 participants