stefna / php-code-builder
Installs: 3 905
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 5
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
Requires (Dev)
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^10.5
- stefna/codestyle: ^1.2
README
Installation
$ composer require stefna/php-code-builder
API
PhpFile
The actual file to save.
Methods
setStrict()
Mark files with declare(strict_types=1);
setNamespace(string $ns)
Set file namespace
setSource(string $code)
Set raw php code to be included in file
Like
$file = new PhpFile(); $file->setSource("spl_autoload_register('$autoloaderName');");
addFunction(PhpFunction $func)
Add function to file. Files can contain multiple functions
addClass(PhpClass $class)
Add class to file. Files can contain multiple classes
addTrait(PhpTrait $trait)
Add trait to file. Files can contain multiple traits
addInterface(PhpInterface $interface)
Add interface to file. Files can contain multiple interfaces
PhpParam
Used to make complex parameter arguments
Usage
new PhpParam('string', 'test') => string $test new PhpParam('object', 'test', null) => object $test = null $param = new PhpParam('DateTimeInterface', 'date'); $param->allowNull(true); $param->getSource() => ?DateTimeInterface $date $param = new PhpParam('float', 'price', 1.5); $param->allowNull(true); $param->getSource() => ?float $price = 1.5
PhpFunction
Usage
__construct(string $identifier, array $params, string $source, ?PhpDocComment $comment = null, ?string $returnTypeHint = null)
Example
$func = new PhpFunction( 'testFunc', [ 'param1', // Simple parameter new PhpParam('int', 'param2', 1), ], "return $param1 + $param2", null, // no docblock 'int' ); echo $func->getSource();
Output:
function testFunc($param1, int $param2 = 1): int { return $param1 + $param2; }
PhpMethod
Method is an extension of PhpFunction
with support for accessors like public
, protected
, private
, final
, static
and abstract
Usage
With return typehint
$method = new PhpMethod('private', 'test', [], 'return 1', null, 'int'); echo $method->getSource(); ------- private function test(): int { return 1; }
With docblock
$method = new PhpMethod( 'private', 'test', [], 'return 1', new PhpDocComment('Test Description') ); echo $method->getSource(); ------- /** * Test Description */ private function test() { return 1; }
Static method
$method = new PhpMethod('protected', 'test', [], 'return 1'); $method->setStatic(); echo $method->getSource(); ------- protected static function test() { return 1; }
Final method
$method = new PhpMethod('public', 'test', [], 'return 1'); $method->setFinal(); echo $method->getSource(); ------- final public function test() { return 1; }
Abstract method
$method = new PhpMethod('public', 'test', [], 'return 1', null, 'int'); $method->setAbstract(); echo $method->getSource(); ------- abstract public function test(): int;