ronantakizawa/codereview-bench huggingface dataset:
Repository: symfony/symfony
File: src/Symfony/Component/DependencyInjection/Compiler/AutowireAsDecoratorPass.php (PHP)

Code before:
    private function accept(Definition $definition): bool
    {
        return !$definition->hasTag('container.ignore_attributes') && $definition->isAutowired();
    }

    private function processClass(string $id, ContainerBuilder $container, Definition $definition, \ReflectionClass $reflectionClass): void
    {
        $attributes = $reflectionClass->getAttributes(AsDecorator::class, \ReflectionAttribute::IS_INSTANCEOF);

        if ([] === $attributes) {
            return;
        }

        if (1 === \count($attributes)) {
            $attribute = $attributes[0]->newInstance();
            $definition->setDecoratedService($attribute->decorates, null, $attribute->priority, $attribute->onInvalid);

            return;
        }

        foreach ($attributes as $attribute) {
            $attribute = $attribute->newInstance();

            $definition = clone $definition;
            $definition->setDecoratedService($attribute->decorates, null, $attribute->priority, $attribute->onInvalid);
            $container->setDefinition($id.'.'.ContainerBuilder::hash($attribute->decorates), $definition);
        }

        $container->removeDefinition($id);
    }
}


Review comment (suggestion):
what about:
```suggestion
            $container->setDefinition(\sprintf('.decorator.%s.%s', $attribute->decorates, $id), $definition);
```

Code after:
    private function accept(Definition $definition): bool
    {
        return !$definition->hasTag('container.ignore_attributes') && $definition->isAutowired();
    }

    private function processClass(string $id, ContainerBuilder $container, Definition $definition, \ReflectionClass $reflectionClass): void
    {
        if (!$attributes = $reflectionClass->getAttributes(AsDecorator::class, \ReflectionAttribute::IS_INSTANCEOF)) {
            return;
        }

        if (1 === \count($attributes)) {
            $attribute = $attributes[0]->newInstance();
            $definition->setDecoratedService($attribute->decorates, null, $attribute->priority, $attribute->onInvalid);

            return;
        }

        foreach ($attributes as $attribute) {
            $attribute = $attribute->newInstance();

            $definition = clone $definition;
            $definition->setDecoratedService($attribute->decorates, null, $attribute->priority, $attribute->onInvalid);
            $container->setDefinition(\sprintf('.decorator.%s.%s', $attribute->decorates, $id), $definition);
        }

        $container->removeDefinition($id);
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php (PHP)

Code before:
    public function __construct(string $arg1, #[AutowireDecorated] AsDecoratorInterface $inner)
    {
    }
}

#[AsDecorator(decorates: AsDecoratorFoo::class, priority: 20)]
class AsDecoratorBar20 implements AsDecoratorInterface
{
    public function __construct(string $arg1, #[AutowireDecorated] AsDecoratorInterface $inner)
    {
    }
}

#[AsDecorator(decorates: \NonExistent::class, onInvalid: ContainerInterface::NULL_ON_INVALID_REFERENCE)]
class AsDecoratorBaz implements AsDecoratorInterface
{
    public function __construct(#[AutowireDecorated] ?AsDecoratorInterface $inner = null)
    {
    }
}

class AsDecoratorMultipleFoo implements AsDecoratorInterface
{
}
class AsDecoratorMultipleBar implements AsDecoratorInterface
{
}

#[AsDecorator(decorates: AsDecoratorMultipleFoo::class)]
#[AsDecorator(decorates: AsDecoratorMultipleBar::class)]
class AsDecoratorMultiple implements AsDecoratorInterface
{
    public function __construct(string $arg1, #[AutowireDecorated] AsDecoratorInterface $inner)
    {
    }
}

#[AsDecorator(decorates: AsDecoratorFoo::class)]
class AutowireNestedAttributes implements AsDecoratorInterface
{
    public function __construct(
        #[Autowire([
            'decorated' => new AutowireDecorated(),
            'iterator' => new AutowireIterator('foo'),
            'locator' => new AutowireLocator('foo'),
            'service' => new Autowire(service: 'bar'),
        ])] array $options)
    {
    }
}


Review comment (suggestion):
```suggestion

class AsDecoratorMultipleBar implements AsDecoratorInterface
```

Code after:
    public function __construct(string $arg1, #[AutowireDecorated] AsDecoratorInterface $inner)
    {
    }
}

#[AsDecorator(decorates: AsDecoratorFoo::class, priority: 20)]
class AsDecoratorBar20 implements AsDecoratorInterface
{
    public function __construct(string $arg1, #[AutowireDecorated] AsDecoratorInterface $inner)
    {
    }
}

#[AsDecorator(decorates: \NonExistent::class, onInvalid: ContainerInterface::NULL_ON_INVALID_REFERENCE)]
class AsDecoratorBaz implements AsDecoratorInterface
{
    public function __construct(#[AutowireDecorated] ?AsDecoratorInterface $inner = null)
    {
    }
}

class AsDecoratorMultipleFoo implements AsDecoratorInterface
{
}

class AsDecoratorMultipleBar implements AsDecoratorInterface
{
}

#[AsDecorator(decorates: AsDecoratorMultipleFoo::class)]
#[AsDecorator(decorates: AsDecoratorMultipleBar::class)]
class AsDecoratorMultiple implements AsDecoratorInterface
{
    public function __construct(string $arg1, #[AutowireDecorated] AsDecoratorInterface $inner)
    {
    }
}

#[AsDecorator(decorates: AsDecoratorFoo::class)]
class AutowireNestedAttributes implements AsDecoratorInterface
{
    public function __construct(
        #[Autowire([
            'decorated' => new AutowireDecorated(),
            'iterator' => new AutowireIterator('foo'),
            'locator' => new AutowireLocator('foo'),
            'service' => new Autowire(service: 'bar'),
        ])] array $options)
    {
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php (PHP)

Code before:

trait FactoryTrait
{
    /**
     * Sets a factory.
     *
     * @return $this
     */
    final public function factory(string|array|ReferenceConfigurator|Expression|\Closure $factory): static
    {
        if ($factory instanceof \Closure) {
            $function = new \ReflectionFunction($factory);
            if ($function->isAnonymous()) {
                throw new InvalidArgumentException(\sprintf('The closure "%s" passed as factory cannot be an anonymous function.', $function->getName()));
            }

            if ($function->getClosureThis()) {
                throw new InvalidArgumentException(\sprintf('The closure "%s" passed as factory cannot be a bound closure.', $function->getName()));
            }

            if (null === $class = $function->getClosureScopeClass()) {
                // Convert global_function(...) closure into 'global_function'
                $this->definition->setFactory($function->getName());
            } else {
                // Convert Class::method(...) closure into [Class::class, 'method']
                $this->definition->setFactory([$class->getName(), $function->getName()]);
            }

            return $this;
        }

        if (\is_string($factory) && 1 === substr_count($factory, ':')) {
            $factoryParts = explode(':', $factory);

            throw new InvalidArgumentException(\sprintf('Invalid factory "%s": the "service:method" notation is not available when using PHP-based DI configuration. Use "[service(\'%s\'), \'%s\']" instead.', $factory, $factoryParts[0], $factoryParts[1]));
        }

        if ($factory instanceof Expression) {
            $factory = '@='.$factory;
        }

        $this->definition->setFactory(static::processValue($factory, true));

        return $this;
    }
}


Review comment (suggestion):
```suggestion
                $this->definition->setFactory([$class, $function->name]);
```

Code after:

trait FactoryTrait
{
    /**
     * Sets a factory.
     *
     * @return $this
     */
    final public function factory(string|array|\Closure|ReferenceConfigurator|Expression $factory): static
    {
        if ($factory instanceof \Closure) {
            $this->definition->setFactory(static::processClosure($factory));

            return $this;
        }

        if (\is_string($factory) && 1 === substr_count($factory, ':')) {
            $factoryParts = explode(':', $factory);

            throw new InvalidArgumentException(\sprintf('Invalid factory "%s": the "service:method" notation is not available when using PHP-based DI configuration. Use "[service(\'%s\'), \'%s\']" instead.', $factory, $factoryParts[0], $factoryParts[1]));
        }

        if ($factory instanceof Expression) {
            $factory = '@='.$factory;
        }

        $this->definition->setFactory(static::processValue($factory, true));

        return $this;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Security/Http/EventListener/IsCsrfTokenValidAttributeListener.php (PHP)

Code before:

    public static function getSubscribedEvents(): array
    {
        return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 25]];
    }

    private function getTokenId(string|Expression $id, Request $request, array $arguments): string
    {
        if (!$id instanceof Expression) {
            return $id;
        }

        $this->expressionLanguage ??= new ExpressionLanguage();

        return (string) $this->expressionLanguage->evaluate($id, [
            'request' => $request,
            'args' => $arguments,
        ]);
    }

    private function getTokenValue(Request $request, int $tokenSource, string $tokenKey): ?string
    {
        $sources = [
            IsCsrfTokenValid::SOURCE_PAYLOAD => fn() => $request->getPayload()->get($tokenKey),
            IsCsrfTokenValid::SOURCE_QUERY => fn() => $request->query->get($tokenKey),
            IsCsrfTokenValid::SOURCE_HEADER => fn() => $request->headers->get($tokenKey),
        ];

        foreach ($sources as $source => $getter) {
            if (!($tokenSource & $source)) {
                continue;
            }

            if (null !== $token = $getter()) {
                return $token;
            }
        }

        return null;
    }
}


Review comment (security):
```suggestion
            IsCsrfTokenValid::SOURCE_PAYLOAD => static fn () => $request->getPayload()->get($tokenKey),
            IsCsrfTokenValid::SOURCE_QUERY => static fn () => $request->query->get($tokenKey),
            IsCsrfTokenValid::SOURCE_HEADER => static fn () => $request->headers->get($tokenKey),
```

Code after:
            }
        }
    }

    public static function getSubscribedEvents(): array
    {
        return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 25]];
    }

    private function getTokenId(string|Expression $id, Request $request, array $arguments): string
    {
        if (!$id instanceof Expression) {
            return $id;
        }

        $this->expressionLanguage ??= new ExpressionLanguage();

        return (string) $this->expressionLanguage->evaluate($id, [
            'request' => $request,
            'args' => $arguments,
        ]);
    }

    private function getTokenValue(Request $request, int $tokenSource, string $tokenKey): ?string
    {
        $sources = [
            IsCsrfTokenValid::SOURCE_PAYLOAD => static fn () => $request->getPayload()->get($tokenKey),
            IsCsrfTokenValid::SOURCE_QUERY => static fn () => $request->query->get($tokenKey),
            IsCsrfTokenValid::SOURCE_HEADER => static fn () => $request->headers->get($tokenKey),
        ];

        foreach ($sources as $source => $getter) {
            if (!($tokenSource & $source)) {
                continue;
            }

            if (null !== $token = $getter()) {
                return $token;
            }
        }

        return null;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Security/Http/Attribute/IsCsrfTokenValid.php (PHP)

Code before:

namespace Symfony\Component\Security\Http\Attribute;

use Symfony\Component\ExpressionLanguage\Expression;

#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
final class IsCsrfTokenValid
{
    public const SOURCE_PAYLOAD = 0b0001;
    public const SOURCE_QUERY = 0b0010;
    public const SOURCE_HEADER = 0b0100;

    public function __construct(
        /**
         * Sets the id, or an Expression evaluated to the id, used when generating the token.
         */
        public string|Expression $id,

        /**
         * Sets the key of the request that contains the actual token value that should be validated.
         */
        public string $tokenKey = '_token',

        /**
         * Sets the source targeted to read the tokenKey.
         */
        public int $tokenSource = self::SOURCE_PAYLOAD,

        /**
         * Sets the available http methods that can be used to validate the token.
         * If not set, the token will be validated for all methods.
         */
        public array|string $methods = [],
    ) {
    }
}


Review comment (suggestion):
```suggestion
         * @var int-mask-of<self::SOURCE_*>
         */
```

Code after:

namespace Symfony\Component\Security\Http\Attribute;

use Symfony\Component\ExpressionLanguage\Expression;

#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
final class IsCsrfTokenValid
{
    public const SOURCE_PAYLOAD = 0b0001;
    public const SOURCE_QUERY = 0b0010;
    public const SOURCE_HEADER = 0b0100;

    public function __construct(
        /**
         * Sets the id, or an Expression evaluated to the id, used when generating the token.
         */
        public string|Expression $id,

        /**
         * Sets the key of the request that contains the actual token value that should be validated.
         */
        public ?string $tokenKey = '_token',

        /**
         * Sets the available http methods that can be used to validate the token.
         * If not set, the token will be validated for all methods.
         */
        public array|string $methods = [],

        /**
         * Sets the source targeted to read the tokenKey.
         *
         * @var int-mask-of<self::SOURCE_*>
         */
        public int $tokenSource = self::SOURCE_PAYLOAD,
    ) {
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Security/Http/EventListener/IsCsrfTokenValidAttributeListener.php (PHP)

Code before:
        return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 25]];
    }

    private function getTokenId(string|Expression $id, Request $request, array $arguments): string
    {
        if (!$id instanceof Expression) {
            return $id;
        }

        $this->expressionLanguage ??= new ExpressionLanguage();

        return (string) $this->expressionLanguage->evaluate($id, [
            'request' => $request,
            'args' => $arguments,
        ]);
    }

    private function getTokenValue(Request $request, int $tokenSource, string $tokenKey): ?string
    {
        $sources = [
            IsCsrfTokenValid::SOURCE_PAYLOAD => static fn () => $request->getPayload()->get($tokenKey),
            IsCsrfTokenValid::SOURCE_QUERY => static fn () => $request->query->get($tokenKey),
            IsCsrfTokenValid::SOURCE_HEADER => static fn () => $request->headers->get($tokenKey),
        ];

        foreach ($sources as $source => $getter) {
            if (!($tokenSource & $source)) {
                continue;
            }

            if (null !== $token = $getter()) {
                return $token;
            }
        }

        return null;
    }
}


Review comment (suggestion):
Would we really expect the header name to be `_token` like payload fields ?

If the name cannot be common, we cannot really support a bitfield IMO.

Code after:

    public static function getSubscribedEvents(): array
    {
        return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 25]];
    }

    private function getTokenId(string|Expression $id, Request $request, array $arguments): string
    {
        if (!$id instanceof Expression) {
            return $id;
        }

        $this->expressionLanguage ??= new ExpressionLanguage();

        return (string) $this->expressionLanguage->evaluate($id, [
            'request' => $request,
            'args' => $arguments,
        ]);
    }

    private function getTokenValue(Request $request, int $tokenSource, string $tokenKey): ?string
    {
        $sources = [
            IsCsrfTokenValid::SOURCE_PAYLOAD => static fn () => $request->getPayload()->get($tokenKey),
            IsCsrfTokenValid::SOURCE_QUERY => static fn () => $request->query->get($tokenKey),
            IsCsrfTokenValid::SOURCE_HEADER => static fn () => $request->headers->get($tokenKey),
        ];

        foreach ($sources as $source => $getter) {
            if (!($tokenSource & $source)) {
                continue;
            }

            if (null !== $token = $getter()) {
                return $token;
            }
        }

        return null;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Form/FormConfigBuilderInterface.php (PHP)

Code before:

    /**
     * Sets the request handler used by the form.
     *
     * @return $this
     */
    public function setRequestHandler(RequestHandlerInterface $requestHandler): static;

    /**
     * Sets whether the form should be initialized automatically.
     *
     * Should be set to true only for root forms.
     *
     * @param bool $initialize True to initialize the form automatically,
     *                         false to suppress automatic initialization.
     *                         In the second case, you need to call
     *                         {@link FormInterface::initialize()} manually.
     *
     * @return $this
     */
    public function setAutoInitialize(bool $initialize): static;

    /**
     * Builds and returns the form configuration.
     *
     * @RETURN FormConfigInterface<T>
     */
    public function getFormConfig(): FormConfigInterface;

    /**
     * Sets the callback that will be called to determine if the model
     * data of the form is empty or not.
     *
     * @return $this
     */
    public function setIsEmptyCallback(?callable $isEmptyCallback): static;
}


Review comment (suggestion):
```suggestion
     * @return FormConfigInterface<T>
```

Code after:

    /**
     * Sets the request handler used by the form.
     *
     * @return $this
     */
    public function setRequestHandler(RequestHandlerInterface $requestHandler): static;

    /**
     * Sets whether the form should be initialized automatically.
     *
     * Should be set to true only for root forms.
     *
     * @param bool $initialize True to initialize the form automatically,
     *                         false to suppress automatic initialization.
     *                         In the second case, you need to call
     *                         {@link FormInterface::initialize()} manually.
     *
     * @return $this
     */
    public function setAutoInitialize(bool $initialize): static;

    /**
     * Builds and returns the form configuration.
     *
     * @return FormConfigInterface<T>
     */
    public function getFormConfig(): FormConfigInterface;

    /**
     * Sets the callback that will be called to determine if the model
     * data of the form is empty or not.
     *
     * @return $this
     */
    public function setIsEmptyCallback(?callable $isEmptyCallback): static;
}
Repository: symfony/symfony
File: src/Symfony/Component/Form/FormView.php (PHP)

Code before:
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Form;

use Symfony\Component\Form\Exception\BadMethodCallException;

/**
 * @author Bernhard Schussek <bschussek@gmail.com>
 *
 * @template T
 *
 * @implements \ArrayAccess<int|string, FormView>
 * @implements \IteratorAggregate<int|string, FormView>
 */
class FormView implements \ArrayAccess, \IteratorAggregate, \Countable
{
    /**
     * The variables assigned to this view.
     *
     * @var array{value: null|T, attr: array<array-key, mixed>, ...<string, mixed>}
     */
    public array $vars = [
        'value' => null,
        'attr' => [],
    ];

    /**
     * The child views.
     *
     * @var array<int|string, FormView>
     */
    public array $children = [];

    /**
     * Is the form attached to this renderer rendered?
     *
     * Rendering happens when either the widget or the row method was called.
     * Row implicitly includes widget, however certain rendering mechanisms
     * have to skip widget rendering when a row is rendered.
     */
    private bool $rendered = false;

    private bool $methodRendered = false;

    /**

Review comment (suggestion):
```suggestion
     * @var array{value: T|null, attr: array<array-key, mixed>, ...<string, mixed>}
```

Code after:
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Form;

use Symfony\Component\Form\Exception\BadMethodCallException;

/**
 * @author Bernhard Schussek <bschussek@gmail.com>
 *
 * @template T
 *
 * @implements \ArrayAccess<int|string, FormView>
 * @implements \IteratorAggregate<int|string, FormView>
 */
class FormView implements \ArrayAccess, \IteratorAggregate, \Countable
{
    /**
     * The variables assigned to this view.
     *
     * @var array{value: T|null, attr: array<array-key, mixed>, ...<string, mixed>}
     */
    public array $vars = [
        'value' => null,
        'attr' => [],
    ];

    /**
     * The child views.
     *
     * @var array<int|string, FormView>
     */
    public array $children = [];

    /**
     * Is the form attached to this renderer rendered?
     *
     * Rendering happens when either the widget or the row method was called.
     * Row implicitly includes widget, however certain rendering mechanisms
     * have to skip widget rendering when a row is rendered.
     */
    private bool $rendered = false;

    private bool $methodRendered = false;

    /**