ronantakizawa/codereview-bench huggingface dataset:
Repository: symfony/symfony
File: src/Symfony/Component/Translation/TranslatableMessage.php (PHP)

Code before:
 * (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\Translation;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * @author Nate Wiebe <nate@northern.co>
 */
class TranslatableMessage implements TranslatableInterface
{
    public function __construct(
        private string $message,
        private array $parameters = [],
        private ?string $domain = null,
    ) {
    }

    public function __toString(): string
    {
        trigger_deprecation('symfony/translation', '7.4', 'Calling "%s()" is deprecated.', __METHOD__);

        return $this->getMessage();
    }

    public function getMessage(): string
    {
        return $this->message;
    }

    public function getParameters(): array
    {
        return $this->parameters;
    }

    public function getDomain(): ?string
    {
        return $this->domain;
    }

    public function trans(TranslatorInterface $translator, ?string $locale = null): string
    {
        return $translator->trans($this->getMessage(), array_map(
            static fn ($parameter) => $parameter instanceof TranslatableInterface ? $parameter->trans($translator, $locale) : $parameter,
            $this->getParameters()
        ), $this->getDomain(), $locale);

Review comment (suggestion):
missing composer.json entry for symfony/deprecation-contracts also
```suggestion
        trigger_deprecation('symfony/contracts', '3.6', 'Method "%s()" is deprecated.', __METHOD__);
```

Code after:
 * (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\Translation;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
 * @author Nate Wiebe <nate@northern.co>
 */
class TranslatableMessage implements TranslatableInterface
{
    public function __construct(
        private string $message,
        private array $parameters = [],
        private ?string $domain = null,
    ) {
    }

    /**
     * @deprecated since Symfony 7.4
     */
    public function __toString(): string
    {
        trigger_deprecation('symfony/translation', '7.4', 'Method "%s()" is deprecated.', __METHOD__);

        return $this->getMessage();
    }

    public function getMessage(): string
    {
        return $this->message;
    }

    public function getParameters(): array
    {
        return $this->parameters;
    }

    public function getDomain(): ?string
    {
        return $this->domain;
    }

    public function trans(TranslatorInterface $translator, ?string $locale = null): string
    {
        $parameters = $this->getParameters();
Repository: symfony/symfony
File: src/Symfony/Contracts/Translation/TranslatorTrait.php (PHP)

Code before:
 * @author Fabien Potencier <fabien@symfony.com>
 */
trait TranslatorTrait
{
    private ?string $locale = null;

    /**
     * @return void
     */
    public function setLocale(string $locale)
    {
        $this->locale = $locale;
    }

    public function getLocale(): string
    {
        return $this->locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
    }

    public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
    {
        if (null === $id || '' === $id) {
            return '';
        }

        $parameters = array_map(
            fn ($parameter) => $parameter instanceof TranslatableInterface ? $parameter->trans($this, $locale) : $parameter,
            $parameters,
        );

        if (!isset($parameters['%count%']) || !is_numeric($parameters['%count%'])) {
            return strtr($id, $parameters);
        }

        $number = (float) $parameters['%count%'];
        $locale = $locale ?: $this->getLocale();

        $parts = [];
        if (preg_match('/^\|++$/', $id)) {
            $parts = explode('|', $id);
        } elseif (preg_match_all('/(?:\|\||[^\|])++/', $id, $matches)) {
            $parts = $matches[0];
        }

        $intervalRegexp = <<<'EOF'
/^(?P<interval>
    ({\s*
        (\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
    \s*})

        |

Review comment (refactor):
We should remove this array_map call here and in Translator and TranslatableMessage: they create needless copies of the parameters array.

Code after:
 * @author Fabien Potencier <fabien@symfony.com>
 */
trait TranslatorTrait
{
    private ?string $locale = null;

    /**
     * @return void
     */
    public function setLocale(string $locale)
    {
        $this->locale = $locale;
    }

    public function getLocale(): string
    {
        return $this->locale ?: (class_exists(\Locale::class) ? \Locale::getDefault() : 'en');
    }

    public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
    {
        if (null === $id || '' === $id) {
            return '';
        }

        foreach ($parameters as $k => $v) {
            if ($v instanceof TranslatableInterface) {
                $parameters[$k] = $v->trans($this, $locale);
            }
        }

        if (!isset($parameters['%count%']) || !is_numeric($parameters['%count%'])) {
            return strtr($id, $parameters);
        }

        $number = (float) $parameters['%count%'];
        $locale = $locale ?: $this->getLocale();

        $parts = [];
        if (preg_match('/^\|++$/', $id)) {
            $parts = explode('|', $id);
        } elseif (preg_match_all('/(?:\|\||[^\|])++/', $id, $matches)) {
            $parts = $matches[0];
        }

        $intervalRegexp = <<<'EOF'
/^(?P<interval>
    ({\s*
        (\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
    \s*})
Repository: symfony/symfony
File: src/Symfony/Component/Console/Tests/Fixtures/AbstractLoopCommand.php (PHP)

Code before:
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

abstract class AbstractLoopCommand extends Command
{
    public function run(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $contexts = [1, 2, 3];
        $io->progressStart(count($contexts));
        $code = self::SUCCESS;

        foreach ($contexts as $ignored) {
            $io->progressAdvance();
            try {
                parent::run($input, $output);
            } catch (\Throwable) {
                $code = self::FAILURE;
            }
        }
        $io->progressFinish();
        $output->writeln("\nLoop finished.");
        return $code;
    }
}


Review comment (suggestion):
```suggestion
        $output->writeln("\nLoop finished.");

        return $code;
```

Code after:
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

abstract class AbstractLoopCommand extends Command
{
    public function run(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $contexts = [1, 2, 3];
        $io->progressStart(count($contexts));
        $code = self::SUCCESS;

        foreach ($contexts as $ignored) {
            $io->progressAdvance();
            try {
                parent::run($input, $output);
            } catch (\Throwable) {
                $code = self::FAILURE;
            }
        }
        $io->progressFinish();
        $output->writeln("\nLoop finished.");

        return $code;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Console/Tests/Fixtures/LoopExampleCommand.php (PHP)

Code before:
<?php

/*
 * 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\Console\Tests\Fixtures;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand('app:loop:example')]
class LoopExampleCommand extends AbstractLoopCommand
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln(' Hello world');
        return Command::SUCCESS;
    }
}


Review comment (suggestion):
```suggestion
        $output->writeln(' Hello world');

        return Command::SUCCESS;
```

Code after:
<?php

/*
 * 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\Console\Tests\Fixtures;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand('app:loop:example')]
class LoopExampleCommand extends AbstractLoopCommand
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln(' Hello world');

        return Command::SUCCESS;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/ObjectMapper/ObjectMapper.php (PHP)

Code before:
     *
     * @return \ReflectionClass<object|T>
     */
    private function getSourceReflectionClass(object $source, \ReflectionClass $targetRefl): \ReflectionClass
    {
        $metadata = $this->metadataFactory->create($source);
        try {
            $refl = new \ReflectionClass($source);
        } catch (\ReflectionException $e) {
            throw new MappingException($e->getMessage(), $e->getCode(), $e);
        }

        if ($metadata) {
            return $refl;
        }

        foreach ($refl->getProperties() as $property) {
            if ($this->metadataFactory->create($source, $property->getName())) {
                return $refl;
            }
        }

        return $targetRefl;
    }

    #[Required]
    public function withObjectMapper(ObjectMapperInterface $objectMapper): static
    {
        $clone = clone $this;
        $clone->objectMapper = $objectMapper;

        return $clone;
    }
}


Review comment (suggestion):
I'm wondering if this is allowed or if I should add something to the framework bundle, I could not find any other occurence of something like this in the current code base.

Code after:
        }

        return null;
    }

    /**
     * @param \ReflectionClass<object> $targetRefl
     *
     * @return \ReflectionClass<object|T>
     */
    private function getSourceReflectionClass(object $source, \ReflectionClass $targetRefl): \ReflectionClass
    {
        $metadata = $this->metadataFactory->create($source);
        try {
            $refl = new \ReflectionClass($source);
        } catch (\ReflectionException $e) {
            throw new MappingException($e->getMessage(), $e->getCode(), $e);
        }

        if ($metadata) {
            return $refl;
        }

        foreach ($refl->getProperties() as $property) {
            if ($this->metadataFactory->create($source, $property->getName())) {
                return $refl;
            }
        }

        return $targetRefl;
    }

    public function withObjectMapper(ObjectMapperInterface $objectMapper): static
    {
        $clone = clone $this;
        $clone->objectMapper = $objectMapper;

        return $clone;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/ObjectMapper/ObjectMapper.php (PHP)

Code before:
    {
        if (\is_callable($fn)) {
            return $fn;
        }

        if ($locator?->has($fn)) {
            return $locator->get($fn);
        }

        return null;
    }

    /**
     * @param \ReflectionClass<object> $targetRefl
     *
     * @return \ReflectionClass<object|T>
     */
    private function getSourceReflectionClass(object $source, \ReflectionClass $targetRefl): \ReflectionClass
    {
        if ($source instanceof \Symfony\Component\VarExporter\LazyObjectInterface) {
            $source->initializeLazyObject();
        } elseif (\PHP_VERSION_ID >= 80400) {
            $refl = new \ReflectionClass($source);
            if ($refl->isUninitializedLazyObject($source)) {
                $refl->initializeLazyObject($source);
            }
        }

        $metadata = $this->metadataFactory->create($source);
        try {
            $refl = new \ReflectionClass($source);
        } catch (\ReflectionException $e) {
            throw new MappingException($e->getMessage(), $e->getCode(), $e);
        }

        if ($metadata) {
            return $refl;
        }

        foreach ($refl->getProperties() as $property) {
            if ($this->metadataFactory->create($source, $property->getName())) {
                return $refl;
            }
        }

        return $targetRefl;
    }
}


Review comment (suggestion):
```suggestion
            (new \ReflectionClass($source))->initializeLazyObject($source);
```

Code after:
        }

        return $value;
    }

    /**
     * @param (string|callable(mixed $value, object $object): mixed) $fn
     */
    private function getCallable(string|callable $fn, ?ContainerInterface $locator = null): ?callable
    {
        if (\is_callable($fn)) {
            return $fn;
        }

        if ($locator?->has($fn)) {
            return $locator->get($fn);
        }

        return null;
    }

    /**
     * @param \ReflectionClass<object> $targetRefl
     *
     * @return \ReflectionClass<object|T>
     */
    private function getSourceReflectionClass(object $source, \ReflectionClass $targetRefl): \ReflectionClass
    {
        $metadata = $this->metadataFactory->create($source);
        try {
            $refl = new \ReflectionClass($source);
        } catch (\ReflectionException $e) {
            throw new MappingException($e->getMessage(), $e->getCode(), $e);
        }

        if ($source instanceof LazyObjectInterface) {
            $source->initializeLazyObject();
        } elseif (\PHP_VERSION_ID >= 80400 && $refl->isUninitializedLazyObject($source)) {
            $refl->initializeLazyObject($source);
        }

        if ($metadata) {
            return $refl;
        }

        foreach ($refl->getProperties() as $property) {
            if ($this->metadataFactory->create($source, $property->getName())) {
                return $refl;
            }
        }
Repository: symfony/symfony
File: src/Symfony/Component/ObjectMapper/ObjectMapper.php (PHP)

Code before:
    }

    /**
     * @param (string|callable(mixed $value, object $object): mixed) $fn
     */
    private function getCallable(string|callable $fn, ?ContainerInterface $locator = null): ?callable
    {
        if (\is_callable($fn)) {
            return $fn;
        }

        if ($locator?->has($fn)) {
            return $locator->get($fn);
        }

        return null;
    }

    /**
     * @param \ReflectionClass<object> $targetRefl
     *
     * @return \ReflectionClass<object|T>
     */
    private function getSourceReflectionClass(object $source, \ReflectionClass $targetRefl): \ReflectionClass
    {
        if ($source instanceof \Symfony\Component\VarExporter\LazyObjectInterface) {
            $source->initializeLazyObject();
        } elseif (\PHP_VERSION_ID >= 80400) {
            $refl = new \ReflectionClass($source);
            if ($refl->isUninitializedLazyObject($source)) {
                $refl->initializeLazyObject($source);
            }
        }

        $metadata = $this->metadataFactory->create($source);
        try {
            $refl = new \ReflectionClass($source);
        } catch (\ReflectionException $e) {
            throw new MappingException($e->getMessage(), $e->getCode(), $e);
        }

        if ($metadata) {
            return $refl;
        }

        foreach ($refl->getProperties() as $property) {
            if ($this->metadataFactory->create($source, $property->getName())) {
                return $refl;
            }
        }


Review comment (suggestion):
with proper `use`:
```suggestion
        if ($source instanceof LazyObjectInterface) {
```

Code after:
        }

        foreach ($transforms as $transform) {
            if ($fn = $this->getCallable($transform, $this->transformCallableLocator)) {
                $value = $this->call($fn, $value, $source, $target);
            }
        }

        return $value;
    }

    /**
     * @param (string|callable(mixed $value, object $object): mixed) $fn
     */
    private function getCallable(string|callable $fn, ?ContainerInterface $locator = null): ?callable
    {
        if (\is_callable($fn)) {
            return $fn;
        }

        if ($locator?->has($fn)) {
            return $locator->get($fn);
        }

        return null;
    }

    /**
     * @param \ReflectionClass<object> $targetRefl
     *
     * @return \ReflectionClass<object|T>
     */
    private function getSourceReflectionClass(object $source, \ReflectionClass $targetRefl): \ReflectionClass
    {
        $metadata = $this->metadataFactory->create($source);
        try {
            $refl = new \ReflectionClass($source);
        } catch (\ReflectionException $e) {
            throw new MappingException($e->getMessage(), $e->getCode(), $e);
        }

        if ($source instanceof LazyObjectInterface) {
            $source->initializeLazyObject();
        } elseif (\PHP_VERSION_ID >= 80400 && $refl->isUninitializedLazyObject($source)) {
            $refl->initializeLazyObject($source);
        }

        if ($metadata) {
            return $refl;
        }
Repository: symfony/symfony
File: src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/fixture_app_services.php (PHP)

Code before:
class BarService
{
}


class Db
{
    public Schema $schema;
}

class Bus
{
    public Handler1 $handler1;
    public Handler2 $handler2;

    public function __construct(public Db $db)
    {
    }
}

class Handler1
{
    public function __construct(
        public Db $db,
        public Schema $schema,
        public Processor $processor
    ) {
    }
}

class Handler2
{
    public function __construct(
        public Db $db,
        public Schema $schema,
        public Processor $processor
    ) {
    }
}

class Processor
{
    public function __construct(
        public Registry $registry,
        public Db $db
    ) {
    }
}

class Registry
{

Review comment (suggestion):
```suggestion
        public Processor $processor,
```

Code after:
class BarService
{
}

class Db
{
    public Schema $schema;
}

class Bus
{
    public Handler1 $handler1;
    public Handler2 $handler2;

    public function __construct(public Db $db)
    {
    }
}

class Handler1
{
    public function __construct(
        public Db $db,
        public Schema $schema,
        public Processor $processor,
    ) {
    }
}

class Handler2
{
    public function __construct(
        public Db $db,
        public Schema $schema,
        public Processor $processor,
    ) {
    }
}

class Processor
{
    public function __construct(
        public Registry $registry,
        public Db $db,
    ) {
    }
}

class Registry
{
    public array $processor;
Repository: symfony/symfony
File: src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/fixture_app_services.php (PHP)

Code before:
class Bus
{
    public Handler1 $handler1;
    public Handler2 $handler2;

    public function __construct(public Db $db)
    {
    }
}

class Handler1
{
    public function __construct(
        public Db $db,
        public Schema $schema,
        public Processor $processor
    ) {
    }
}

class Handler2
{
    public function __construct(
        public Db $db,
        public Schema $schema,
        public Processor $processor
    ) {
    }
}

class Processor
{
    public function __construct(
        public Registry $registry,
        public Db $db
    ) {
    }
}

class Registry
{
    public array $processor;
}

class Schema
{
    public function __construct(public Db $db)
    {
    }
}


Review comment (suggestion):
```suggestion
        public Processor $processor,
```

Code after:
{
    public Handler1 $handler1;
    public Handler2 $handler2;

    public function __construct(public Db $db)
    {
    }
}

class Handler1
{
    public function __construct(
        public Db $db,
        public Schema $schema,
        public Processor $processor,
    ) {
    }
}

class Handler2
{
    public function __construct(
        public Db $db,
        public Schema $schema,
        public Processor $processor,
    ) {
    }
}

class Processor
{
    public function __construct(
        public Registry $registry,
        public Db $db,
    ) {
    }
}

class Registry
{
    public array $processor;
}

class Schema
{
    public function __construct(public Db $db)
    {
    }
}