leaditin/code

Provides an API to easy generate PHP code

0.1.5 2023-02-06 12:26 UTC

This package is auto-updated.

Last update: 2024-11-06 16:11:41 UTC


README

Provides an API to generate arbitrary code.

Build Status Code Quality Code Coverage Latest Version PDS Skeleton

Installation

The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json:

composer require leaditin/code

Usage

1. Create Class

<?php

use Leaditin\Code\DocBlock;
use Leaditin\Code\Flag;
use Leaditin\Code\Generator\Factory;
use Leaditin\Code\Import;
use Leaditin\Code\Member\Constant;
use Leaditin\Code\Member\Method;
use Leaditin\Code\Member\Property;
use Leaditin\Code\Tag;
use Leaditin\Code\Type;
use Leaditin\Code\Value;
use Leaditin\Code\Visibility;

$generator = (new Factory())->classGenerator();
$generator
    ->setName('MyClass')
    ->setNamespace('MyDummyNamespace')
    ->setExtends('MyDummyClass')
    ->implementInterface('MyDummyInterface')
    ->useTrait('MyDummyTrait')
    ->setDocBlock(
        new DocBlock(
            'Short description',
            'Long description',
            [
                new Tag('author', 'author@domain')
            ]
        )
    )
    ->addImport(new Import('MyNamespace\\MyDummyTrait'))
    ->addConstant(new Constant('CONST_A', 2, new Visibility(Visibility::VISIBILITY_PUBLIC)))
    ->addConstant(new Constant('CONST_B', 3, new Visibility(Visibility::VISIBILITY_PUBLIC)))
    ->addProperty(new Property('name', new Value('Some User'), new Type(Type::TYPE_STRING), new Flag(Flag::FLAG_PROTECTED)))
    ->addProperty(new Property('email', new Value('author@domain'), new Type(Type::TYPE_STRING), new Flag(Flag::FLAG_PROTECTED)))
    ->addMethod(new Method('name', new Flag(Flag::FLAG_PUBLIC), null, 'return $this->name;', null, new Type(Type::TYPE_STRING)))
    ->addMethod(new Method('email', new Flag(Flag::FLAG_PUBLIC), null, 'return $this->email;', null, new Type(Type::TYPE_STRING)));

echo $generator->generate();

Will output:

<?php

namespace MyDummyNamespace;

use MyNamespace\MyDummyTrait;

/**
 * Short description
 *
 * Long description
 *
 * @author author@domain
 */
class MyClass extends MyDummyClass implements MyDummyInterface
{
    use MyDummyTrait;

    public const CONST_A = 2;
    public const CONST_B = 3;

    /**
     * @var string
     */
    protected $name = 'Some User';

    /**
     * @var string
     */
    protected $email = 'author@domain';

    /**
     * @return string
     */
    public function name(): string
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function email(): string
    {
        return $this->email;
    }
}

2. Create Interface

<?php

use Leaditin\Code\DocBlock;
use Leaditin\Code\Flag;
use Leaditin\Code\Generator\Factory;
use Leaditin\Code\Member\Constant;
use Leaditin\Code\Member\Method;
use Leaditin\Code\Type;
use Leaditin\Code\Visibility;

$generator = (new Factory())->interfaceGenerator();
$generator
    ->setName('MyInterface')
    ->setNamespace('MyDummyNamespace')
    ->setExtends('MyDummyInterface')
    ->setDocBlock(
        new DocBlock(
            'Short description',
            'Long description'
        )
    )
    ->addConstant(new Constant('CONST_A', 2, new Visibility(Visibility::VISIBILITY_PUBLIC)))
    ->addConstant(new Constant('CONST_B', 3, new Visibility(Visibility::VISIBILITY_PUBLIC)))
    ->addMethod(new Method('name', new Flag(Flag::FLAG_PUBLIC), null, null, null, new Type(Type::TYPE_STRING)))
    ->addMethod(new Method('email', new Flag(Flag::FLAG_PUBLIC), null, null, null, new Type(Type::TYPE_STRING)));

echo $generator->generate();

Will output...

<?php

namespace MyDummyNamespace;

/**
 * Short description
 *
 * Long description
 */
interface MyInterface extends MyDummyInterface
{
    public const CONST_A = 2;
    public const CONST_B = 3;

    /**
     * @return string
     */
    public function name(): string;

    /**
     * @return string
     */
    public function email(): string;
}

3. Create Trait

<?php

use Leaditin\Code\DocBlock;
use Leaditin\Code\Flag;
use Leaditin\Code\Generator\Factory;
use Leaditin\Code\Member\Method;
use Leaditin\Code\Member\Property;
use Leaditin\Code\Type;
use Leaditin\Code\Value;

$generator = (new Factory())->traitGenerator();
$generator
    ->setName('MyTrait')
    ->setDocBlock(
        new DocBlock(
            'Short description',
            'Long description'
        )
    )
    ->addProperty(new Property('name', new Value('Some User'), new Type(Type::TYPE_STRING), new Flag(Flag::FLAG_PROTECTED)))
    ->addProperty(new Property('email', new Value('author@domain'), new Type(Type::TYPE_STRING), new Flag(Flag::FLAG_PROTECTED)))
    ->addMethod(new Method('name', new Flag(Flag::FLAG_PUBLIC), null, 'return $this->name;', null, new Type(Type::TYPE_STRING)))
    ->addMethod(new Method('email', new Flag(Flag::FLAG_PUBLIC), null, 'return $this->email;', null, new Type(Type::TYPE_STRING)));

echo $generator->generate();

Will output...

<?php

/**
 * Short description
 *
 * Long description
 */
trait MyTrait
{
    /**
     * @var string
     */
    protected $name = 'Some User';

    /**
     * @var string
     */
    protected $email = 'author@domain';

    /**
     * @return string
     */
    public function name(): string
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function email(): string
    {
        return $this->email;
    }
}

Credits

License

Released under MIT License - see the License File for details.