Skip to content

fix: Allow passing pure-closures to NormalizerBuilder::registerTransformer#795

Closed
thePanz wants to merge 1 commit intoCuyZ:masterfrom
thePanz:register-transformer-as-pure-closure
Closed

fix: Allow passing pure-closures to NormalizerBuilder::registerTransformer#795
thePanz wants to merge 1 commit intoCuyZ:masterfrom
thePanz:register-transformer-as-pure-closure

Conversation

@thePanz
Copy link
Copy Markdown
Contributor

@thePanz thePanz commented Mar 24, 2026

This will solve PHPStan errors when passing Closure to registerTransformer

Ref: https://phpstan.org/writing-php-code/phpdoc-types#callables

@romm
Copy link
Copy Markdown
Member

romm commented Mar 24, 2026

Hi @thePanz, I thought @pure-callable would include @pure-closure 🤔

Maybe I'm wrong, but do you have an example where it fails?

@thePanz
Copy link
Copy Markdown
Contributor Author

thePanz commented Mar 24, 2026

I get this error:
image

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: 

@romm
Copy link
Copy Markdown
Member

romm commented Mar 24, 2026

Oh, I see. Then I doubt that adding pure-closure will change anything to the issue.

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 😊

@romm romm closed this Mar 24, 2026
@thePanz thePanz deleted the register-transformer-as-pure-closure branch March 24, 2026 10:19
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