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.
-->


Pull request:
Use CarbonImmutable when creating CarbonPeriodImmutable
Fix #2800

Patch:
diff --git a/readme.md b/readme.md
index 49e0e9e7..efc5811f 100644
--- a/readme.md
+++ b/readme.md
@@ -123,8 +123,8 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
 <a title="#1 Guide To Online Gambling In Canada" href="https://casinohex.org/canada/?utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon" target="_blank" rel="sponsored"><img alt="CasinoHex Canada" src="https://opencollective-production.s3.us-west-1.amazonaws.com/79fdbcc0-a997-11eb-abbc-25e48b63c6dc.jpg" width="85" height="64"></a>
 <a title="Casino-portugal.pt" href="https://casino-portugal.pt/?utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon" target="_blank" rel="sponsored"><img alt="Casino-portugal.pt" src="https://logo.clearbit.com/casino-portugal.pt" width="64" height="64"></a>
 <a title="Slots City® ➢ Лучшее лицензионно казино онлайн и оффлайн на гривны в Украине. 【 Более1500 игровых автоматов и слотов】✅ Официально и Безопасно" href="https://slotscity.ua/?utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon" target="_blank" rel="sponsored"><img alt="Slots City" src="https://opencollective-production.s3.us-west-1.amazonaws.com/d7e298c0-7abe-11ed-8553-230872f5e54d.png" width="90" height="64"></a>
-<a title="Znajdź najlepsze zakłady bukmacherskie w Polsce w 2023 roku. Probukmacher.pl to Twoje kompendium wiedzy na temat bukmacherów!" href="https://www.probukmacher.pl?utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon" target="_blank" rel="sponsored"><img alt="Probukmacher" src="https://opencollective-production.s3.us-west-1.amazonaws.com/account-avatar/caf50271-4560-4ffe-a434-ea15239168db/Screenshot_1.png" width="89" height="64"></a>
 <a title="inkedin" href="https://inkedin.com?utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon" target="_blank" rel="sponsored"><img alt="inkedin" src="https://logo.clearbit.com/inkedin.com" width="64" height="64"></a>
+<a title="Znajdź najlepsze zakłady bukmacherskie w Polsce w 2023 roku. Probukmacher.pl to Twoje kompendium wiedzy na temat bukmacherów!" href="https://www.probukmacher.pl?utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon" target="_blank" rel="sponsored"><img alt="Probukmacher" src="https://opencollective-production.s3.us-west-1.amazonaws.com/account-avatar/caf50271-4560-4ffe-a434-ea15239168db/Screenshot_1.png" width="89" height="64"></a>
 <a title="A self-hosted web radio management suite, including turnkey installer tools and an easy-to-use web app to manage your stations. " href="https://azuracast.com?utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon" target="_blank" rel="sponsored"><img alt="AzuraCast" src="https://opencollective-production.s3.us-west-1.amazonaws.com/3c12ea10-cdfb-11eb-9cf4-3760b386b76d.png" width="64" height="64"></a>
 <a title="Triplebyte is the first software engineering job platform that is on the developer&#039;s side. Take our coding quiz!" href="https://triplebyte.com/os/opencollective?utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon" target="_blank" rel="sponsored"><img alt="Triplebyte" src="https://opencollective-production.s3.us-west-1.amazonaws.com/43e4f9d0-30cd-11ea-9c6b-e1142996e8b2.png" width="64" height="64"></a>
 <a title="Connect your Collective to GitHub Sponsors: https://docs.opencollective.com/help/collectives/github-sponsors" href="https://github.com/sponsors/?utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon" target="_blank" rel="sponsored"><img alt="GitHub Sponsors" src="https://opencollective-production.s3.us-west-1.amazonaws.com/87b1d240-f617-11ea-9960-fd7e8ab20fe4.png" width="73" height="64"></a>
@@ -142,7 +142,7 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
 
 Thank you to all our backers! 🙏
 
-<a href="https://opencollective.com/Carbon#backers" target="_blank"><img src="https://opencollective.com/Carbon/backers.svg?width=890&version=2023-06-08-07-12"></a>
+<a href="https://opencollective.com/Carbon#backers" target="_blank"><img src="https://opencollective.com/Carbon/backers.svg?width=890"></a>
 
 [[Become a backer](https://opencollective.com/Carbon#backer)]
 
diff --git a/sponsors.php b/sponsors.php
index 4f6d994e..80128161 100644
--- a/sponsors.php
+++ b/sponsors.php
@@ -27,18 +27,10 @@ function getOpenCollectiveSponsors(): string
 
     $list = array_map(static function (array $member) {
         $createdAt = CarbonImmutable::parse($member['createdAt']);
-        $lastTransactionAt = CarbonImmutable::parse($member['lastTransactionAt']);
-
-        if ($createdAt->format('d H:i:s.u') > $lastTransactionAt->format('d H:i:s.u')) {
-            $createdAt = $createdAt
-                ->setDay($lastTransactionAt->day)
-                ->modify($lastTransactionAt->format('H:i:s.u'));
-        }
-
-        $monthlyContribution = (float) ($member['totalAmountDonated'] / ceil($createdAt->floatDiffInMonths()));
+        $monthlyContribution = (float) ($member['totalAmountDonated'] / (1 + $createdAt->diffInMonths()));
 
         if (
-            $lastTransactionAt->isAfter('last month') &&
+            CarbonImmutable::parse($member['lastTransactionAt'])->isAfter('last month') &&
             $member['lastTransactionAmount'] > $monthlyContribution
         ) {
             $monthlyContribution = (float) $member['lastTransactionAmount'];
diff --git a/src/Carbon/CarbonPeriod.php b/src/Carbon/CarbonPeriod.php
index 01679142..007a4cf8 100644
--- a/src/Carbon/CarbonPeriod.php
+++ b/src/Carbon/CarbonPeriod.php
@@ -632,6 +632,10 @@ class CarbonPeriod implements Iterator, Countable, JsonSerializable
      */
     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 4f104c3c..9d73dc36 100644
--- a/tests/CarbonPeriod/CreateTest.php
+++ b/tests/CarbonPeriod/CreateTest.php
@@ -779,4 +779,44 @@ class CreateTest extends AbstractTestCase
         $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')
+            )
+        );
+    }
 }