vlx/php-immutable-tools

Tools for work with immutable data objects

0.2.0 2024-09-17 14:24 UTC

This package is auto-updated.

Last update: 2025-04-17 15:45:51 UTC


README

To make it possible to create a modified copy of an immutable object use trait, and call method with.

readonly class SomeData
{
    use ImmutableData;

    public function __construct(
        public int $field,
        public AnotherData $objectField,
        public bool $flag = false,
    ) {
    }
}

$object = new SomeData(field: 1, objectField: new AnotherData(), flag: true);
$newObject = $object->with(field: 2, flag: false);

You can also use rector for automatic generation phpDoc for with method in each class which use ImmutableData

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Vlx\Immutable\ImmutableModelsRector;

return RectorConfig::configure()
    ...
    ->withRules([ImmutableModelsRector::class]);