Skip to content

Conversation

@peterfox
Copy link
Contributor

@peterfox peterfox commented Jan 12, 2025

Changes

  • Adds the assertions isStatic and isNotStatic.
  • Adds tests.
  • Updates the readme docs.

Why

This can be useful in some libraries where you want to make sure that the end user hasn't provided a static function as an argument.

Assert::isStatic($closure, $message = '');
Assert::isNotStatic($closure, $message = '');

@zerkms
Copy link
Contributor

zerkms commented Jan 12, 2025

you want to make sure that the end user hasn't provided a static function as an argument.

what would be a real life use case for this exactly? What property of a static function can break 3rd party code versus non-static function that simply does not use $this?

@peterfox
Copy link
Contributor Author

So if you're writing an API that accepts a Closure but contextually binds it later on it will stop the code from working.

$obj = new stdClass();
$function = static function () {
    return 'foo';
};
$function->bindTo($obj);

This would then throw an exception like 'Cannot bind an instance to a static closure'.

A good example of where I've seen this happen is with Pest where you provide closures where the Closure is then bound to a different object.

There's probably less of a need for isStatic than there is isNotStatic.

@shadowhand
Copy link
Collaborator

@peterfox sorry for the delay here. If you'd still like to see this merged, please rebase to latest master, which is staged for version 2.0.

@peterfox
Copy link
Contributor Author

peterfox commented Dec 2, 2025

@shadowhand thanks, having a look but might take a day or two to get myself back up to speed.

@peterfox peterfox force-pushed the feature/is-static-assertions branch from 81a2673 to 340e948 Compare December 2, 2025 01:25
@shadowhand
Copy link
Collaborator

shadowhand commented Dec 2, 2025

Tests are failing...

please regenerate Mixin with php bin/generate.php command in the project root

src/Assert.php Outdated
/**
* @param Closure $closure
* @param string $message
* @throws InvalidArgumentException|ReflectionException
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What scenario would cause ReflectionException to be thrown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually shouldn't happen. It would only occur if a string was passed to the ReflectionFunction constructor but that shouldn't happen as the assertions only accept callables.

@peterfox peterfox requested a review from shadowhand December 3, 2025 13:04
Copy link
Collaborator

@shadowhand shadowhand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peterfox changes look good. Assuming tests pass, I'll merge and clean up.

@shadowhand shadowhand merged commit 9b07f70 into webmozarts:master Dec 3, 2025
9 checks passed
shadowhand added a commit that referenced this pull request Dec 3, 2025
shadowhand added a commit that referenced this pull request Dec 3, 2025
shadowhand added a commit that referenced this pull request Dec 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants