Traitor
Add a Trait use statement to existing PHP class
Install / Use
/learn @KKSzymanowski/TraitorREADME
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:
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.
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;
}
Related Skills
node-connect
349.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.9kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
