2017 © Pedro Peláez
 

library traitor

Add a trait use statement to existing PHP class

image

kkszymanowski/traitor

Add a trait use statement to existing PHP class

  • Thursday, April 19, 2018
  • by KKSzymanowski
  • Repository
  • 1 Watchers
  • 43 Stars
  • 254,399 Installations
  • PHP
  • 8 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 19 % Grown

The README.md

Traitor

StyleCI Build Status Latest Stable Version License, (*1)

A PHP package for automatically adding a trait use statement to a given class., (*2)

Installation

Via composer:, (*3)

composer require kkszymanowski/traitor

Usage

  • Basic usage:
use Traitor\Traitor;

Traitor::addTrait(FooTrait::class)->toClass(FooClass:class);
  • Add multiple traits:
use Traitor\Traitor;

Traitor::addTraits([
    FooTrait::class,
    BarTrait::class,
    BazTrait::class
])->toClass(FooClass::class);

//or

Traitor::addTrait(FooTrait::class)
       ->addTrait(BarTrait::class)
       ->addTrait(BazTrait::class)
       ->toClass(FooClass::class);
  • 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., (*4)

Behavior

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

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

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:, (*7)

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

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

The Versions

19/04 2018

dev-master

9999999-dev

Add a trait use statement to existing PHP class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kuba Szymanowski

php trait add

19/04 2018

0.2.5

0.2.5.0

Add a trait use statement to existing PHP class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kuba Szymanowski

php trait add

28/08 2017

0.2.4

0.2.4.0

Add a trait use statement to existing PHP class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kuba Szymanowski

php trait add

18/12 2016

0.2.3

0.2.3.0

Add a trait use statement to existing PHP class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kuba Szymanowski

php trait add

09/07 2016

0.2.2

0.2.2.0

Add a trait use to existing PHP class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kuba Szymanowski

php trait add

20/06 2016

0.2.1

0.2.1.0

Add a trait use to existing PHP class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kuba Szymanowski

php trait add

15/06 2016

0.2.0

0.2.0.0

Add a trait use to existing PHP class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kuba Szymanowski

php trait add

13/06 2016

0.1.0

0.1.0.0

Add a trait use to existing PHP class

  Sources   Download

MIT

The Requires

 

The Development Requires

by Kuba Szymanowski

php trait