phpactor / code-builder
Generating and modifying source code
Installs: 56 196
Dependents: 2
Suggesters: 0
Security: 0
Stars: 8
Watchers: 4
Forks: 5
Open Issues: 0
Requires
- php: ^7.3 || ^8.0
- microsoft/tolerant-php-parser: ~0.1.0
- phpactor/text-document: ~1.2.3
- phpactor/worse-reflection: ~0.4.7
- twig/twig: ^2.4
Requires (Dev)
- ergebnis/composer-normalize: ^2.0
- friendsofphp/php-cs-fixer: ^2.17
- phpactor/test-utils: ~1.1.3
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ~0.12.0
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ^5.0
This package is auto-updated.
Last update: 2022-03-31 19:54:31 UTC
README
This library can be used to generate or idempotently add classes, methods, properties, use statements, etc. to existing source code using prototypes.
A prototype is an object which defines structural code elements.
Usage
The library provides a source code prototype builder:
$builder = SourceBuilder::create() ->namespace('Animals'); ->use('Measurements\\Height'); ->class('Rabbits') ->extends('Leopridae') ->property('force') ->visibility('private') ->type('int') ->defaultValue(5) ->end() ->method('jump') ->parameters() ->parameter('how') ->default('high') ->type('Height') ->end(); ->end() ->end() ->end(); $sourcePrototype = $builder->build();
the above prototype can either be used to generate a new class:
$renderer = new TwigRenderer(); $renderer->render($sourcePrototype);
Or it can be applied to an existing source code, given the following:
<?php class Rabbits { }
When we do:
$updater = new TolerantUpdater(); $updater->apply($sourcePrototype, Code::fromString(file_get_contents('Rabbits.php')));
Then we get:
<?php namespace Animals; use Measurements\Height; class Rabbits extends Leopridae { private $force = 5; public function jump(Height $how = 'high') { } }
About this project
This library is part of the phpactor project.
Contributing
This package is open source and welcomes contributions! Feel free to open a pull request on this repository.
Support
- Create an issue on the main Phpactor repository.
- Join the
#phpactor
channel on the Slack Symfony Devs channel.