Projectsphp-builderAttributes

PHP Builder

Package

A PHP code builder for programmatically generating classes, interfaces, traits, and enums.

Attributes & Constants

Attributes

PhpAttribute renders #[Name] / #[Name(…)] on classes, interfaces, traits, enums, enum cases, properties, methods, constants, and parameters.

use BradieTilley\Builder\PhpAttribute;

new PhpAttribute('AllowDynamicProperties');

new PhpAttribute(
    'App\\Attributes\\OwnedBy',
    ["team: 'platform'"],
);

new PhpAttribute('Deprecated', ["message: 'use NAME'"]);
ArgumentNotes
nameShort name or FQCN (FQCNs are imported)
argumentsList of raw PHP expression strings, joined with ,

Examples of argument strings:

["length: 255"]
["value: 'grey'"]
["group: 'content'"]
["'positional'", 'named: true']

The builder does not parse attribute arguments — whatever you pass is emitted verbatim inside the parentheses.

Where attributes attach

// Type-level
new PhpClass(attributes: [new PhpAttribute('…')]);

// Constant
new PhpClassConstant(name: 'TYPE', value: "'x'", attributes: […]);

// Property / method / parameter / enum case — same pattern

Class constants

PhpClassConstant works on classes, interfaces, traits, and enums.

use BradieTilley\Builder\PhpAttribute;
use BradieTilley\Builder\PhpClassConstant;

new PhpClassConstant(
    name: 'TYPE',
    value: "'kitchen'",
    type: 'string',
    attributes: [
        new PhpAttribute('Deprecated', ["message: 'use NAME'"]),
    ],
);

new PhpClassConstant(
    name: 'MAX',
    value: '100',
    visibility: PhpClassConstant::protected(),
    final: true,
    type: 'int',
);

new PhpClassConstant(
    name: 'DEFAULT',
    value: 'self::Draft',
    type: 'self',
    visibility: PhpClassConstant::private(),
);
ArgumentNotes
nameConstant name
valueRaw PHP expression ('100', "'kitchen'", 'self::Draft')
visibilityDefault public
finalfinal public const …
typeTyped class constants
attributes

Templates (generics)

PhpTemplate (or a plain string) adds @template tags to type or method docblocks.

use BradieTilley\Builder\PhpTemplate;

new PhpTemplate(name: 'TValue');
new PhpTemplate(name: 'TKey', of: 'array-key', covariant: true);
new PhpTemplate(name: 'TInput', contravariant: true);

// String shorthand on templates arrays:
templates: ['TExtra'] // → @template TExtra
ArgumentNotes
nameTemplate name (TValue)
ofOptional bound (of array-key)
covariant@template covariant T
contravariant@template contravariant T

covariant wins over contravariant if both were somehow set.

Continue to Formatting.