Skip to content

Commit

Permalink
feat: add phpstan @param-closure-this tags to macro methods
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jan 31, 2025
1 parent ea38ddc commit 223a372
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 15 deletions.
13 changes: 0 additions & 13 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ parameters:
- '#^Call to an undefined method DateInterval::(spec|optimize)\(\)\.$#'
- '#^Property Carbon\\Carbon::\$timezone \(Carbon\\CarbonTimeZone\) does not accept string\.$#'
- '#^Method class@anonymous/tests/Carbon/TestingAidsTest\.php:\d+::modify\(\) should return class@anonymous/tests/Carbon/TestingAidsTest\.php:\d+ but returns \(?DateTimeImmutable#'
-
message: '#^Undefined variable: \$this$#'
paths:
- src/Carbon/Traits/Mixin.php
-
message: '#^Variable \$this in isset\(\) is never defined\.$#'
paths:
- src/Carbon/Traits/Mixin.php
-
message: '#^Call to an undefined method Carbon\\Carbon::[a-zA-Z]+Of[a-zA-Z]+\(\)\.$#'
paths:
Expand Down Expand Up @@ -83,11 +75,6 @@ parameters:
message: '#^Parameter \$foo of anonymous function has invalid type Tests\\Factory\\FooBar\.#'
paths:
- tests/Factory/CallbackTest.php
-
message: '#^Call to an undefined method Tests\\Carbon(Immutable)?\\MacroTest::diffInYears\(\)\.#'
paths:
- tests/Carbon/MacroTest.php
- tests/CarbonImmutable/MacroTest.php
-
message: '#^Call to an undefined method SubCarbon(Immutable)?::diffInDecades\(\)\.#'
paths:
Expand Down
2 changes: 2 additions & 0 deletions src/Carbon/CarbonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3351,6 +3351,8 @@ public function lte(DateTimeInterface|string $date): bool;
* });
* echo Carbon::yesterday()->hours(11)->userFormat();
* ```
*
* @param-closure-this static $macro
*/
public static function macro(string $name, ?callable $macro): void;

Expand Down
2 changes: 2 additions & 0 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,8 @@ public function isEmpty(): bool
* });
* echo CarbonInterval::hours(2)->twice();
* ```
*
* @param-closure-this static $macro
*/
public static function macro(string $name, ?callable $macro): void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ private static function makeTimezone(mixed $input): ?CarbonTimeZone
* });
* echo CarbonPeriod::since('2011-05-12')->until('2011-06-03')->middle();
* ```
*
* @param-closure-this static $macro
*/
public static function macro(string $name, ?callable $macro): void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Carbon/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ public function getHumanDiffOptions(): int
* });
* echo $factory->yesterday()->hours(11)->userFormat();
* ```
*
* @param-closure-this static $macro
*/
public function macro(string $name, ?callable $macro): void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Carbon/Traits/Macro.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ trait Macro
* });
* echo Carbon::yesterday()->hours(11)->userFormat();
* ```
*
* @param-closure-this static $macro
*/
public static function macro(string $name, ?callable $macro): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Carbon/Traits/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ private static function loadMixinTrait(string $trait): void
$closureBase = Closure::fromCallable([$context, $name]);

static::macro($name, function (...$parameters) use ($closureBase, $className, $baseClass) {
$downContext = isset($this) ? ($this) : new $baseClass();
$context = isset($this) ? $this->cast($className) : new $className();
$downContext = isset(${'this'}) ? $this : new $baseClass();
$context = isset(${'this'}) ? $this->cast($className) : new $className();

try {
// @ is required to handle error if not converted into exceptions
Expand Down

0 comments on commit 223a372

Please sign in to comment.