Repository: briannesbitt/Carbon (PHP)

Issue:
CarbonPeriodImmutable creates mutable Carbon instanced when used with static creator methods
<!--
    🛑 DON'T REMOVE ME.
    This issue template applies to all
      - bug reports,
      - feature proposals,
      - and documentation requests

    Having all those informations will allow us to know exactly
    what you expect and answer you faster and precisely (answer
    that matches your Carbon version, PHP version and usage).
    
    Note: Comments between <!- - and - -> won't appear in the final
    issue (See [Preview] tab).
-->
Hello,

I encountered an issue with the following code:
<!--
    ⚠️ Your code below (not the code from inside Carbon library)
    Be sure it's dependency-free and enough to reproduce the issue
    when we run it on:
    https://play.phpsandbox.io/embed/nesbot/carbon
-->
```php
$period = CarbonPeriodImmutable::between(Carbon::today(), Carbon::today()->addDays(2));

foreach ($period as $date) {
   echo $date instanceof CarbonImmutable ? 'immutable' : 'mutable' . PHP_EOL;
}
```

Carbon version: 2.67.0
<!--
    ⚠️ Run the command `composer show nesbot/carbon` to get "versions"
-->

PHP version: 8.2.6

<!--
    Use `echo phpversion();`
    to get PHP version.

    Some issues can depends on your context, settings,
    macros, timezone, language. So to be sure the code
    you give is enough to reproduce your bug, try it
    first in:
    https://play.phpsandbox.io/embed/nesbot/carbon?input=%3C%3Fphp%0A%0Aecho%20Carbon%3A%3Aparse('2018-06-01')-%3EsubDay()-%3Emonth%3B

    You can use the [Options] button to change the version
    then when you get the bug with this editor, you can use
    the [Export] button, copy the link of the opened tab,
    then paste it in the issue. Then we can immediatly get
    your issue.
-->


I expected to get:

```
immutable
immutable
immutable
```
<!--
    Always give your expectations. Each use has their owns.
    You may want daylight saving time to be taken into account,
    someone else want it to be ignored. You may want timezone
    to be used in comparisons, someone else may not, etc.
-->

But I actually get:

```
mutable
mutable
mutable
```
<!--
    If you did not succeed to get the same result in
    https://play.phpsandbox.io/embed/nesbot/carbon?input=%3C%3Fphp%0A%0Aecho%20Carbon%3A%3Aparse('2018-06-01')%3B then precise the
    result you get.
-->



Discussion:

Patch:
diff --git a/src/Carbon/CarbonPeriod.php b/src/Carbon/CarbonPeriod.php
index 01679142ea..007a4cf829 100644
--- a/src/Carbon/CarbonPeriod.php
+++ b/src/Carbon/CarbonPeriod.php
@@ -632,6 +632,10 @@ public static function __callStatic($method, $parameters)
      */
     public function __construct(...$arguments)
     {
+        if (is_a($this->dateClass, DateTimeImmutable::class, true)) {
+            $this->options = static::IMMUTABLE;
+        }
+
         // Parse and assign arguments one by one. First argument may be an ISO 8601 spec,
         // which will be first parsed into parts and then processed the same way.
 


Test patch:
diff --git a/tests/CarbonPeriod/CreateTest.php b/tests/CarbonPeriod/CreateTest.php
index 4f104c3cf2..9d73dc36f8 100644
--- a/tests/CarbonPeriod/CreateTest.php
+++ b/tests/CarbonPeriod/CreateTest.php
@@ -779,4 +779,44 @@ public function testInstanceInvalidInstance()
         $periodClass = $this->periodClass;
         $periodClass::instance(Carbon::now());
     }
+
+    public function testMutability()
+    {
+        $this->assertSame(
+            [Carbon::class, Carbon::class, Carbon::class],
+            iterator_to_array(
+                CarbonPeriod::between(Carbon::today(), Carbon::today()->addDays(2))->map('get_class')
+            )
+        );
+        $this->assertSame(
+            [Carbon::class, Carbon::class, Carbon::class],
+            iterator_to_array(
+                CarbonPeriod::between(CarbonImmutable::today(), CarbonImmutable::today()->addDays(2))->map('get_class')
+            )
+        );
+        $this->assertSame(
+            [Carbon::class, Carbon::class, Carbon::class],
+            iterator_to_array(
+                CarbonPeriod::between('today', 'today + 2 days')->map('get_class')
+            )
+        );
+        $this->assertSame(
+            [CarbonImmutable::class, CarbonImmutable::class, CarbonImmutable::class],
+            iterator_to_array(
+                CarbonPeriodImmutable::between(Carbon::today(), Carbon::today()->addDays(2))->map('get_class')
+            )
+        );
+        $this->assertSame(
+            [CarbonImmutable::class, CarbonImmutable::class, CarbonImmutable::class],
+            iterator_to_array(
+                CarbonPeriodImmutable::between(CarbonImmutable::today(), CarbonImmutable::today()->addDays(2))->map('get_class')
+            )
+        );
+        $this->assertSame(
+            [CarbonImmutable::class, CarbonImmutable::class, CarbonImmutable::class],
+            iterator_to_array(
+                CarbonPeriodImmutable::between('today', 'today + 2 days')->map('get_class')
+            )
+        );
+    }
 }