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

@mixin does not support traits #6778

Open
Paril opened this issue Mar 9, 2022 · 6 comments
Open

@mixin does not support traits #6778

Paril opened this issue Mar 9, 2022 · 6 comments

Comments

@Paril
Copy link

Paril commented Mar 9, 2022

Bug report

The @mixin phpdoc does not recognize traits as an acceptable input type. It does recognize the members of traits declared via @mixin, but falsely reports that it is an invalid type.

Code snippet that reproduces the problem

https://phpstan.org/r/d0921109-874b-47e4-83b3-aa98060912e9

Expected output

It should act similarly to Deprecations being a class or an interface. That seems to be how Intelephense and phpstorm interpret it.

nfourtythree added a commit to craftcms/commerce that referenced this issue Jul 4, 2022
@phpstan-bot
Copy link
Contributor

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

@@ @@
+PHP 8.2 (3 errors)
+==========
+
+13: PHPDoc tag @mixin contains invalid type Deprecations.
+20: Access to an undefined property MyClass::$caseSensitive.
+21: Access to an undefined property MyClass::$nonExistant.
+
+PHP 7.1 – 8.1 (2 errors)
+==========
+
 13: PHPDoc tag @mixin contains invalid type Deprecations.
 21: Access to an undefined property MyClass::$nonExistant.
Full report

PHP 8.2 (3 errors)

Line Error
13 PHPDoc tag @mixin contains invalid type Deprecations.
20 Access to an undefined property MyClass::$caseSensitive.
21 Access to an undefined property MyClass::$nonExistant.

PHP 7.1 – 8.1 (2 errors)

Line Error
13 PHPDoc tag @mixin contains invalid type Deprecations.
21 Access to an undefined property MyClass::$nonExistant.

@timkelty
Copy link

timkelty commented Feb 7, 2023

Similar behavior if you have an @mixin on an interface: https://phpstan.org/r/4e8ecbaf-bb20-4ab6-9d65-328b2aa7d42a

@phpstan-bot
Copy link
Contributor

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

@@ @@
+PHP 8.2 – 8.3 (3 errors)
+==========
+
+13: PHPDoc tag @mixin contains invalid type Deprecations.
+20: Access to an undefined property MyClass::$caseSensitive.
+21: Access to an undefined property MyClass::$nonExistant.
+
+PHP 7.1 – 8.1 (2 errors)
+==========
+
 13: PHPDoc tag @mixin contains invalid type Deprecations.
 21: Access to an undefined property MyClass::$nonExistant.
Full report

PHP 8.2 – 8.3 (3 errors)

Line Error
13 PHPDoc tag @mixin contains invalid type Deprecations.
20 Access to an undefined property MyClass::$caseSensitive.
21 Access to an undefined property MyClass::$nonExistant.

PHP 7.1 – 8.1 (2 errors)

Line Error
13 PHPDoc tag @mixin contains invalid type Deprecations.
21 Access to an undefined property MyClass::$nonExistant.

@brandonkelly
Copy link

@ondrejmirtes We’re hitting this issue with Craft CMS when we switch the PHP requirement to 8.2. Apparently several trait-defined properties have been flying under the PHPStan radar only because PHPStan thought they were dynamic properties, which they’re not.

It would be great if PHPStan could be updated to respect trait-defined properties, including via interfaces, such as this one.

@ondrejmirtes
Copy link
Member

@brandonkelly This is a hack that doesn't have a way to be represented in the filesystem. You're using an interface and you're expecting the implementations of the interface to be using a specific trait. There's no way to enforce that in PHP or PHPStan, and @mixin is not that.

@mixin is used for redirecting unknown method calls and property fetches to another class. It assumes that __call/__get_/__set magic methods are implemented for these redirects.

I see a couple of ways out for you:

  • Switch to methods instead of properties so that you can cleanly describe and enforce the implementation to implement those methods.
  • Open a feature request for @phpstan-require-use that can be used on interfaces and abstract classes to force implementations/child classes to use certain traits.

@brandonkelly
Copy link

@ondrejmirtes Yeah, fully aware that we’re using interfaces wrong 🤦🏻 but we’re following Yii 2 conventions. We do plan on addressing that down the road, but that will be a widespread change on a large codebase with lots of downstream first/third party code that will need to adjust as well—so out of scope for now.

Went ahead and posted a FR for @phpstan-require-use (#9899). Thanks for considering!

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

No branches or pull requests

5 participants