|
Server : Apache/2.4.41 (Ubuntu) System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64 User : www-data ( 33) PHP Version : 8.2.12 Disable Function : NONE Directory : /var/www/account.appointkrypt.com/vendor/kkszymanowski/traitor/ |
Upload File : |
# Traitor
[](https://styleci.io/repos/60994435)
[](https://travis-ci.org/KKSzymanowski/Traitor)
[](https://packagist.org/packages/kkszymanowski/traitor)
[](https://packagist.org/packages/kkszymanowski/traitor)
A PHP package for automatically adding a `trait use statement` to a given class.
## Installation
Via composer:
```
composer require kkszymanowski/traitor
```
## Usage
- Basic usage:
```
use Traitor\Traitor;
Traitor::addTrait(FooTrait::class)->toClass(FooClass:class);
```
- Add multiple traits:
```php
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:
```php
use Traitor\Traitor;
$alreadyUses = Traitor::alreadyUses(FooClass::class, BarTrait::class);
```
- Only generate output without changing files:
```php
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.
## Behavior
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;
}
```