fix: Allow passing pure-closures to NormalizerBuilder::registerTransformer#795
Closed
thePanz wants to merge 1 commit intoCuyZ:masterfrom
Closed
fix: Allow passing pure-closures to NormalizerBuilder::registerTransformer#795thePanz wants to merge 1 commit intoCuyZ:masterfrom
thePanz wants to merge 1 commit intoCuyZ:masterfrom
Conversation
Member
|
Hi @thePanz, I thought Maybe I'm wrong, but do you have an example where it fails? |
Contributor
Author
|
And that's the class: final readonly class ConvertKeysToSnakeCase implements NormalizerBuilderConfigurator
{
public function configureNormalizerBuilder(NormalizerBuilder $builder): NormalizerBuilder
{
return $builder->registerTransformer(static function (object $object, callable $next): mixed {
$result = $next();
if (! is_array($result)) {
return $result;
}
$snakeCased = [];
foreach ($result as $key => $value) {
$lcFirstKey = preg_replace('/[A-Z]/', '_$0', lcfirst($key));
$newKey = strtolower($lcFirstKey ?? $key);
$snakeCased[$newKey] = $value;
}
return $snakeCased;
});
}
}```
Maybe the fix is not really working, as for some phpstan-cache on my current project? :thinking: |
Member
|
Oh, I see. Then I doubt that adding I gave some explanations in the documentation about purity — TL;DR it is quite hard to handle pure markers in a codebase, so I give access to a PHPStan plugin that will silent those errors for you (this is enabled by default in Valinor's own codebase BTW). I'm closing this, but thanks anyway 😊 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

This will solve PHPStan errors when passing Closure to registerTransformer
Ref: https://phpstan.org/writing-php-code/phpdoc-types#callables