PHP code example of rasuvaeff / rector-datetime-immutable

1. Go to this page and download the library: Download rasuvaeff/rector-datetime-immutable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rasuvaeff / rector-datetime-immutable example snippets


// before — mutable construction, in-place mutation
$deadline = new \DateTime('2026-01-01');
$deadline->modify('+1 month');

// after (both rules) — immutable, and the mutation result is kept
$deadline = new \DateTimeImmutable('2026-01-01');
$deadline = $deadline->modify('+1 month');

// @mutable-datetime-boundary: parameter $object 

// rector-preflight.php


declare(strict_types=1);

use Rasuvaeff\RectorDateTimeImmutable\MutableDateTimeBoundaryRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
    ->withRules([
        MutableDateTimeBoundaryRector::class,
    ])
    ->withSkip([
        MutableDateTimeBoundaryRector::class => [
            __DIR__ . '/src/Legacy/SdkAdapter.php',
        ],
    ]);

#[ORM\Column(type: 'datetime')]              // → type: 'datetime_immutable'
private \DateTime $expiresAt;                // → private \DateTimeImmutable $expiresAt;

#[ORM\Column(type: Types::DATETIME_MUTABLE)] // → Types::DATETIME_IMMUTABLE
#[ORM\Column]                                // no type: Doctrine infers it from the PHP type

// rector.php


declare(strict_types=1);

use Rasuvaeff\RectorDateTimeImmutable\DateTimeImmutableRector;
use Rasuvaeff\RectorDateTimeImmutable\LostDateTimeMutationRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
    ->withPaths([__DIR__ . '/src'])
    ->withRules([
        DateTimeImmutableRector::class,
        LostDateTimeMutationRector::class,
    ]);

->withConfiguredRule(DateTimeImmutableRector::class, [
    DateTimeImmutableRector::CONSTRUCTORS => true,
    DateTimeImmutableRector::TYPEHINTS => true,
    DateTimeImmutableRector::PROPERTIES => true,
    DateTimeImmutableRector::ALLOW_SUBCLASS => false,
])

->withConfiguredRule(LostDateTimeMutationRector::class, [
    LostDateTimeMutationRector::MODE => LostDateTimeMutationRector::MODE_REPORT,
])

/**
 * @mutable-datetime — third-party SDK mutates this in place
 */
private \DateTime $sdkClock;

// @mutable-datetime-boundary: parameter $object 
bash
vendor/bin/rector-datetime-immutable --preflight-config=rector-preflight.php src