phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

@mixin does not support `trait`s

Open Paril opened this issue 3 years ago • 7 comments
trafficstars

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.

Paril avatar Mar 09 '22 23:03 Paril

@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.

phpstan-bot avatar Sep 22 '22 14:09 phpstan-bot

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

timkelty avatar Feb 07 '23 03:02 timkelty

@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.

phpstan-bot avatar Jun 28 '23 20:06 phpstan-bot

@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.

brandonkelly avatar Sep 18 '23 17:09 brandonkelly

@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.

ondrejmirtes avatar Sep 18 '23 18:09 ondrejmirtes

@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!

brandonkelly avatar Sep 19 '23 04:09 brandonkelly

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

@@ @@
+PHP 8.2 – 8.4 (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.4 (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.

phpstan-bot avatar Aug 26 '24 21:08 phpstan-bot

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

@@ @@
+PHP 8.2 – 8.4 (4 errors)
+==========
+
+ 3: Tip: Trait Deprecations is used zero times and is not analysed.
+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 (3 errors)
+==========
+
+ 3: Tip: Trait Deprecations is used zero times and is not analysed.
 13: PHPDoc tag @mixin contains invalid type Deprecations.
 21: Access to an undefined property MyClass::$nonExistant.
Full report

PHP 8.2 – 8.4 (4 errors)

Line Error
3 Tip: Trait Deprecations is used zero times and is not analysed.
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 (3 errors)

Line Error
3 Tip: Trait Deprecations is used zero times and is not analysed.
13 PHPDoc tag @mixin contains invalid type Deprecations.
21 Access to an undefined property MyClass::$nonExistant.

phpstan-bot avatar Sep 23 '24 14:09 phpstan-bot

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

@@ @@
+PHP 8.2 – 8.5 (4 errors)
+==========
+
+ 3: Tip: Trait Deprecations is used zero times and is not analysed.
+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 (3 errors)
+==========
+
+ 3: Tip: Trait Deprecations is used zero times and is not analysed.
 13: PHPDoc tag @mixin contains invalid type Deprecations.
 21: Access to an undefined property MyClass::$nonExistant.
Full report

PHP 8.2 – 8.5 (4 errors)

Line Error
3 Tip: Trait Deprecations is used zero times and is not analysed.
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 (3 errors)

Line Error
3 Tip: Trait Deprecations is used zero times and is not analysed.
13 PHPDoc tag @mixin contains invalid type Deprecations.
21 Access to an undefined property MyClass::$nonExistant.

phpstan-bot avatar Nov 01 '25 13:11 phpstan-bot

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

@@ @@
+PHP 8.2 – 8.5 (4 errors)
+==========
+
+ 3: Tip: Trait Deprecations is used zero times and is not analysed.
 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 (4 errors)
+==========
+
+ 3: Tip: Trait Deprecations is used zero times and is not analysed.
+13: PHPDoc tag @mixin contains invalid type Deprecations.
+20: Access to deprecated property $caseSensitive of trait Deprecations.
 21: Access to an undefined property MyClass::$nonExistant.
Full report

PHP 8.2 – 8.5 (4 errors)

Line Error
3 Tip: Trait Deprecations is used zero times and is not analysed.
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 (4 errors)

Line Error
3 Tip: Trait Deprecations is used zero times and is not analysed.
13 PHPDoc tag @mixin contains invalid type Deprecations.
20 Access to deprecated property $caseSensitive of trait Deprecations.
21 Access to an undefined property MyClass::$nonExistant.

phpstan-bot avatar Nov 07 '25 10:11 phpstan-bot