msstc4php / dto-collection-generator
Generates natively typed collection classes over msstc4php/dto-collection from #[TypedCollection] attributes and a config file.
Package info
github.com/msstc4php/dto-collection-generator
Type:composer-plugin
pkg:composer/msstc4php/dto-collection-generator
Requires
- php: >=8.1
- composer-plugin-api: ^2.0
- composer-runtime-api: ^2.0
- msstc4php/dto-collection: ^1.1
- nikic/php-parser: ^5.1
- symfony/console: ^5.4.47 || ^6.4 || ^7.0 || ^8.0
- symfony/yaml: ^5.4.54 || ^6.4.45 || ^7.4.18 || ^8.1.6
Requires (Dev)
- composer/composer: ^2.8
- doctrine/collections: ^2.2 || ^3.1
- doctrine/mongodb-odm: ^2.10
- phpunit/phpunit: ^10.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Writes natively typed collection classes over
msstc4php/dto-collection from #[TypedCollection] attributes on your DTOs
and from a config file. One attribute gives you a class like this, with the element type in
every signature:
final class OrderLineList implements ImmutableList, TypedList { use ImmutableListBehaviour; public static function of(OrderLine ...$elements): static { /* … */ } public function firstOrFail(): OrderLine { /* … */ } public function with(OrderLine ...$elements): self { /* … */ } // … about 30 one-line methods }
- Variants: immutable, mutable and Doctrine MongoDB ODM (
collectionClass). - Element types: classes, interfaces,
int,float,string,bool,?T, unions and intersections. - Code for the target PHP version (8.1–8.5):
#[\Override]from 8.3, intersection element types from 8.2. - Repeatable output, a
--checkmode for CI, and no hand-written file is ever overwritten.
Installation
composer require --dev msstc4php/dto-collection-generator composer require msstc4php/dto-collection
The generator runs on PHP 8.1+, inside your project: it loads your DTO classes through the project's autoloader.
Quick start
Mark the DTO:
use MSSTC4PHP\DtoCollection\Attribute\TypedCollection; use MSSTC4PHP\DtoCollection\Attribute\Variant; #[TypedCollection(class: 'App\Collection\OrderLineList', variant: Variant::Immutable)] #[TypedCollection(class: 'App\Collection\MutableOrderLineList', variant: Variant::Mutable)] final class OrderLine { // … }
Add dto-collection-generator.yaml next to composer.json:
version: 1 sources: - src/Dto
Run it:
vendor/bin/dto-collection-generator generate
The classes land where PSR-4 autoloading looks for them, here src/Collection/ if
App\ maps to src/.
In CI
vendor/bin/dto-collection-generator generate --check
exits with 1 when a generated file is missing, outdated or edited by hand.
Regenerating on composer dump-autoload
{
"extra": {
"dto-collection-generator": {"config": "dto-collection-generator.yaml"}
}
}
The plugin runs the generator after every autoload dump and only warns on failure; add
"failOnError": true to make the Composer command fail instead.
Documentation
- docs/configuration.md: every config key, the checks and the exit codes.
- The shape of the generated classes is dto-collection's writing-a-typed-collection.md.
Development
make install # package and tool dependencies make verify # PHPStan, CS-Fixer, Rector, deptrac, PHP 8.1 lint, tests on local PHP and 8.1 make test-targets # the golden output on PHP 8.1 … 8.5 in Docker make infection # mutation testing
Security
See SECURITY.md.
License
MIT, see LICENSE.