sourecode / php-object-model
dev-master
2022-12-25 00:28 UTC
Requires
- php: >=8.2
- nikic/php-parser: ^4.15
Requires (Dev)
- consolidation/robo: 4.x-dev
- php-cs-fixer/shim: ^3.13
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^5.1
This package is auto-updated.
Last update: 2024-10-25 04:42:58 UTC
README
A superset to the PHP Parser library that allows you to parse and manipulate PHP code. The idea is to create a model to manipulate PHP code like in javascript.
Installation
composer require sourecode/php-object-model
Usage
<?php use SoureCode\PhpObjectModel\File\ClassFile; use SoureCode\PhpObjectModel\Model\ClassModel; use SoureCode\PhpObjectModel\Model\PropertyModel; use SoureCode\PhpObjectModel\Type\StringType; use SoureCode\PhpObjectModel\ValueObject\NamespaceName; $classFile = new ClassFile('<?php'); $classFile ->setDeclare((new DeclareModel())->setStrictTypes(true)) ->setNamespace(new NamespaceModel(NamespaceName::fromString('App\\Foo'))) ->setClass( (new ClassModel('Foo')) ->addProperty( (new PropertyModel('foo')) ->setType(new StringType()) ->setPublic(true) ) ); echo $classFile->getSourceCode();
Generates something like this:
<?php declare(strict_types=1); namespace App\Foo; class Foo { public string $foo; }
For more examples see the tests.