Menu

← Back to return.*

Error Identifier: return.trait

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

trait MyTrait
{
}

function createTrait(): MyTrait
{
	// ...
}

Why is it reported? #

A trait cannot be used as a type in PHP. Traits are not types – they are a mechanism for code reuse. Using a trait name as a return type declaration is not valid and causes a fatal error at runtime.

How to fix it #

Use an interface or class instead of the trait:

+interface HasTrait
+{
+}

-function createTrait(): MyTrait
+function createTrait(): HasTrait
 {
 	// ...
 }

Or use a class that uses the trait:

 class MyClass
 {
 	use MyTrait;
 }

-function createTrait(): MyTrait
+function createTrait(): MyClass
 {
 	// ...
 }

How to ignore this error #

You can use the identifier return.trait to ignore this error using a comment:

// @phpstan-ignore return.trait
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: return.trait

Rules that report this error #

  • PHPStan\Rules\Functions\ExistingClassesInArrowFunctionTypehintsRule [1] [2]
  • PHPStan\Rules\Functions\ExistingClassesInClosureTypehintsRule [1] [2]
  • PHPStan\Rules\Functions\ExistingClassesInTypehintsRule [1] [2]
  • PHPStan\Rules\Methods\ExistingClassesInTypehintsRule [1] [2]
  • PHPStan\Rules\Properties\ExistingClassesInPropertyHookTypehintsRule [1] [2]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.