Skip to content

Support passing Unit enums to sub method and update type-hints - #3250

Merged
kylekatarnls merged 2 commits into
briannesbitt:masterfrom
hackel:add-sub-subtract-typehints
Nov 3, 2025
Merged

kylekatarnls merged 2 commits into
briannesbitt:masterfrom
hackel:add-sub-subtract-typehints

Conversation

@hackel

@hackel hackel commented Oct 15, 2025

Copy link
Copy Markdown
Contributor

This adds support for passing Unit enums to the sub method, matching the existing behaviour of the add method. It also updates the phpdocs for the add, sub, and subtract methods to reflect the available usage patterns. Specifically, the documentation only includes examples of using these methods with the ($value, $unit) order of arguments. e.g.

$modifiedMutable = $mutable->add(1, 'day');
$modifiedImmutable = CarbonImmutable::now()->add(1, 'day');
echo $dt->add(61, 'seconds');                      // 2012-02-03 00:01:01
$interval->subtract(10, 'minutes');

However, the current type-hinting only supports the ($unit, $value) order, producing the following errors from PHPStan:

phpstan: Parameter #1 $unit of method Carbon\CarbonInterface::add() expects Carbon\CarbonConverterInterface|Carbon\Unit|Closure|DateInterval|string, 1 given.
phpstan: Parameter #2 $value of method Carbon\CarbonInterface::add() expects float|int, string given.

I think if you're wanting to deprecate the ($value, $unit) order (which also seems reasonable), then the documentation should be updated to reflect the preferred order going forward.

@kylekatarnls kylekatarnls added this to the 3.11.0 milestone Nov 3, 2025
@kylekatarnls
kylekatarnls merged commit d0346f9 into briannesbitt:master Nov 3, 2025
3 checks passed
@kylekatarnls

Copy link
Copy Markdown
Collaborator

Thanks 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants