Add a trait use statement to existing PHP class

0.2.5 2018-04-19 12:24 UTC


StyleCI Build Status Latest Stable Version License

A PHP package for automatically adding a trait use statement to a given class.


Via composer:

composer require kkszymanowski/traitor


  • Basic usage:
use Traitor\Traitor;

  • Add multiple traits:
use Traitor\Traitor;



  • Check if class already uses trait:
use Traitor\Traitor;

$alreadyUses = Traitor::alreadyUses(FooClass::class, BarTrait::class);
  • Only generate output without changing files:
use Traitor\Handlers\AbstractTreeHandler;

$handler = new AbstractTreeHandler(file($originalFilePath), FooTrait::class, BarClass::class);

$newContent = $handler->handle()->toString();

Note, that AbstractTreeHandler accepts input file as an array of lines, such as one produced from file() call.


Adding a new trait use statement does not change in any way formatting of your file(or at least it shouldn't).

If the trait is not present in the use section below the namespace declaration, it will be also added there, below any existing imports.

If it's not present in the use section in the class body, it will be added there above first existing use statement, on it's own line:

use Bar\PreviouslyExistingTrait;
use Baz\NewlyAddedTrait; // Here

class Foo
    use NewlyAddedTrait; // And here
    use PreviouslyExistingTrait;

To do

  • Add trait correctly when the class is empty, for example:
class Foo {}
  • Add trait correctly when the class isn't namespaced.