Projectsphp-builderMethods

PHP Builder

Package

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

Methods & Arguments

Methods are PhpMethod objects. Arguments are PhpArgument objects. Both accept native types as strings or richer PhpType instances.

use BradieTilley\Builder\PhpArgument;
use BradieTilley\Builder\PhpAttribute;
use BradieTilley\Builder\PhpMethod;
use BradieTilley\Builder\Types\PhpGeneric;

new PhpMethod(
    name: 'setTags',
    visibility: PhpMethod::public(),
    final: true,
    args: [
        new PhpArgument(
            type: PhpGeneric::array(value: 'string'),
            name: 'tags',
            defaultValue: '[]',
            description: 'Tag names',
        ),
    ],
    return: 'static',
    lines: [
        '$this->tags()->sync($tags);',
        'return $this;',
    ],
    description: 'Sync tags on the model',
    throws: ['RuntimeException'],
    attributes: [
        new PhpAttribute('App\\Attributes\\Internal'),
    ],
);

Method options

ArgumentTypeDefaultNotes
namestring
visibilityPhpVisibilityPublicUse PhpMethod::public() / protected() / private()
staticboolfalse
finalboolfalseIgnored when abstract is true
abstractboolfalseSignature ends with ;, no body
returnsReferenceboolfalsefunction &name()
argslist<PhpArgument>[]Multi-arg methods render multiline
returnPhpType|string|nullnull
lineslist<string>[]Raw body lines (no leading indent)
description?stringnullDocblock summary
throwslist<string>[]FQCNs become @throws (and are imported)
templateslist<PhpTemplate|string>[]Method-level @template tags
attributeslist<PhpAttribute>[]
signatureOnlyboolfalseForce ; with no body (interfaces set this for you)

Empty lines entries become blank lines in the method body.

Abstract methods

new PhpMethod(
    name: 'boot',
    visibility: PhpMethod::protected(),
    abstract: true,
    return: 'void',
);

Returning by reference

new PhpMethod(name: 'resolve', returnsReference: true, return: 'mixed', lines: ['return $x;']);

Generics on methods

use BradieTilley\Builder\PhpTemplate;
use BradieTilley\Builder\Types\PhpCallableType;
use BradieTilley\Builder\Types\PhpGeneric;

new PhpMethod(
    name: 'map',
    static: true,
    templates: [
        new PhpTemplate(name: 'TReturn', of: 'mixed'),
    ],
    args: [
        new PhpArgument(
            type: new PhpCallableType(parameters: ['mixed'], return: 'TReturn'),
            name: 'callback',
        ),
    ],
    return: PhpGeneric::array(value: 'TReturn'),
    lines: ['return [];'],
);

Arguments

new PhpArgument(
    name: 'tags',
    type: 'array',
    defaultValue: '[]',
    variadic: false,
    byRef: false,
    visibility: null,       // set → constructor promotion
    setVisibility: null,    // asymmetric set on promoted props
    readonly: false,
    final: false,           // promoted params only
    description: null,
    get: null,              // promoted property hooks
    set: null,
    attributes: [],
);
ArgumentNotes
defaultValueRaw PHP expression string ('[]', 'null', "'untitled'")
variadicEmits ...$name; default values are omitted
byRefEmits &$name
visibilityAny visibility implies constructor property promotion
promotedAlso available; setting visibility sets this automatically
setVisibilityAsymmetric visibility: public private(set)
finalOnly on promoted params
get / setProperty hooks on promoted parameters only
attributesParameter attributes (e.g. #[SensitiveParameter])

Constructor promotion

new PhpMethod(
    name: '__construct',
    args: [
        new PhpArgument(
            visibility: PhpArgument::public(),
            readonly: true,
            type: 'string',
            name: 'id',
        ),
        new PhpArgument(
            visibility: PhpArgument::public(),
            setVisibility: PhpArgument::private(),
            type: 'string',
            name: 'name',
            defaultValue: "'untitled'",
        ),
        new PhpArgument(
            type: 'int',
            name: 'count',
            byRef: true, // ordinary (non-promoted) by-ref param
        ),
        new PhpArgument(
            type: 'string',
            name: 'tags',
            variadic: true,
        ),
    ],
    lines: [
        '// …',
    ],
);

Hooks, asymmetric set visibility, and final are only valid on promoted parameters. Using them on a normal argument throws InvalidPhpDefinitionException.

use BradieTilley\Builder\PhpPropertyGetHook;
use BradieTilley\Builder\PhpPropertySetHook;

new PhpArgument(
    visibility: PhpArgument::protected(),
    type: 'string',
    name: 'nickname',
    get: new PhpPropertyGetHook(lines: ['return $this->nickname;']),
    set: new PhpPropertySetHook(
        type: 'string',
        lines: ['$this->nickname = trim($value);'],
    ),
);

See Properties & Hooks for expression vs block vs stub forms.

Visibility helpers

PhpMethod, PhpArgument, PhpProperty, PhpClassConstant, and PhpTraitAlias all expose:

PhpMethod::public();
PhpMethod::protected();
PhpMethod::private();

These return PhpVisibility enum cases.

Continue to Properties & Hooks.