ronantakizawa/codereview-bench huggingface dataset:
Repository: symfony/symfony
File: src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php (PHP)

Code before:

            $p = new MyProxy();
            $p->name = 'test';

            return $p;
        };

        $r = new \ReflectionClass(MyProxy::class);
        $lazyObj = $r->newLazyProxy($initializer);
        $this->assertFalse($initialized);
        $mapper = new ObjectMapper();
        $d = $mapper->map($lazyObj, MyProxy::class);
        $this->assertSame('test', $d->name);
        $this->assertTrue($initialized);
    }

    /**
     * @dataProvider validPartialInputProvider
     */
    public function testMapPartially(PartialInput $actual, FinalInput $expected)
    {
        $mapper = new ObjectMapper();
        $this->assertEquals($expected, $mapper->map($actual));
    }

    public static function validPartialInputProvider(): iterable {
        $p = new PartialInput;
        $p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
        $p->website = 'https://updated.website.com';

        $f = new FinalInput();
        $f->uuid = $p->uuid;
        $f->website = $p->website;

        yield [$p, $f];

        $p = new PartialInput;
        $p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
        $p->website = null;

        $f = new FinalInput();
        $f->uuid = $p->uuid;

        yield [$p, $f];

        $p = new PartialInput;
        $p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
        $p->website = 'https://updated.website.com';
        $p->email = 'updated@email.com';

        $f = new FinalInput();

Review comment (suggestion):
```suggestion
    public static function validPartialInputProvider(): iterable
    {
```

Code after:

            $p = new MyProxy();
            $p->name = 'test';

            return $p;
        };

        $r = new \ReflectionClass(MyProxy::class);
        $lazyObj = $r->newLazyProxy($initializer);
        $this->assertFalse($initialized);
        $mapper = new ObjectMapper();
        $d = $mapper->map($lazyObj, MyProxy::class);
        $this->assertSame('test', $d->name);
        $this->assertTrue($initialized);
    }

    /**
     * @dataProvider validPartialInputProvider
     */
    public function testMapPartially(PartialInput $actual, FinalInput $expected)
    {
        $mapper = new ObjectMapper();
        $this->assertEquals($expected, $mapper->map($actual));
    }

    public static function validPartialInputProvider(): iterable
    {
        $p = new PartialInput();
        $p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
        $p->website = 'https://updated.website.com';

        $f = new FinalInput();
        $f->uuid = $p->uuid;
        $f->website = $p->website;

        yield [$p, $f];

        $p = new PartialInput();
        $p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
        $p->website = null;

        $f = new FinalInput();
        $f->uuid = $p->uuid;

        yield [$p, $f];

        $p = new PartialInput();
        $p->uuid = '6a9eb6dd-c4dc-4746-bb99-f6bad716acb2';
        $p->website = 'https://updated.website.com';
        $p->email = 'updated@email.com';
Repository: symfony/symfony
File: src/Symfony/Component/Console/Helper/Helper.php (PHP)

Code before:
        $string ??= '';

        if (preg_match('//u', $string)) {
            return (new UnicodeString($string))->length();
        }

        if (false === $encoding = mb_detect_encoding($string, null, true)) {
            return \strlen($string);
        }

        return mb_strlen($string, $encoding);
    }

    /**
     * Returns the subset of a string, using mb_substr if it is available.
     */
    public static function substr(?string $string, int $from, ?int $length = null): string
    {
        $string ??= '';

        if (preg_match('//u', $string)) {
            $result = grapheme_substr((new UnicodeString($string))->toString(), $from, $length);

            return false === $result
                ? ''
                : $result;
        }

        if (false === $encoding = mb_detect_encoding($string, null, true)) {
            return substr($string, $from, $length);
        }

        return mb_substr($string, $from, $length, $encoding);
    }

    /**
     * @return string
     */
    public static function formatTime(int|float $secs, int $precision = 1)
    {
        $secs = (int) floor($secs);

        if (0 === $secs) {
            return '< 1 sec';
        }

        static $timeFormats = [
            [1, '1 sec', 'secs'],
            [60, '1 min', 'mins'],
            [3600, '1 hr', 'hrs'],
            [86400, '1 day', 'days'],

Review comment (suggestion):
```suggestion
            return (new UnicodeString($string))->slice($from, $length);
```

Code after:
        $string ??= '';

        if (preg_match('//u', $string)) {
            return (new UnicodeString($string))->length();
        }

        if (false === $encoding = mb_detect_encoding($string, null, true)) {
            return \strlen($string);
        }

        return mb_strlen($string, $encoding);
    }

    /**
     * Returns the subset of a string, using mb_substr if it is available.
     */
    public static function substr(?string $string, int $from, ?int $length = null): string
    {
        $string ??= '';

        if (preg_match('//u', $string)) {
            return (new UnicodeString($string))->slice($from, $length);
        }

        if (false === $encoding = mb_detect_encoding($string, null, true)) {
            return substr($string, $from, $length);
        }

        return mb_substr($string, $from, $length, $encoding);
    }

    /**
     * @return string
     */
    public static function formatTime(int|float $secs, int $precision = 1)
    {
        $secs = (int) floor($secs);

        if (0 === $secs) {
            return '< 1 sec';
        }

        static $timeFormats = [
            [1, '1 sec', 'secs'],
            [60, '1 min', 'mins'],
            [3600, '1 hr', 'hrs'],
            [86400, '1 day', 'days'],
        ];

        $times = [];
        foreach ($timeFormats as $index => $format) {
Repository: symfony/symfony
File: src/Symfony/Component/Form/EnumFormTypeGuesser.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\Form;

use Symfony\Component\Form\Extension\Core\Type\EnumType as FormEnumType;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;

final class EnumFormTypeGuesser implements FormTypeGuesserInterface
{
    private array $cache = [];

    public function guessType(string $class, string $property): ?TypeGuess
    {
        if (null === ($propertyType = $this->getPropertyType($class, $property))) {
            return null;
        }

        return new TypeGuess(FormEnumType::class, ['class' => $propertyType->getName()], Guess::HIGH_CONFIDENCE);
    }

    public function guessRequired(string $class, string $property): ?ValueGuess
    {
        if (null === $propertyType = $this->getPropertyType($class, $property)) {
            return null;
        }

        return new ValueGuess(!$propertyType->allowsNull(), Guess::HIGH_CONFIDENCE);
    }

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    public function guessPattern(string $class, string $property): ?ValueGuess
    {
        return null;
    }

Review comment (performance):
```suggestion
    /**
     * @var (\ReflectionNamedType|false)[]
     */
    private array $cache = [];
```

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\Form;

use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;

final class EnumFormTypeGuesser implements FormTypeGuesserInterface
{
    /**
     * @var array<string, array<string, string|false>>
     */
    private array $cache = [];

    public function guessType(string $class, string $property): ?TypeGuess
    {
        if (!($enum = $this->getPropertyType($class, $property))) {
            return null;
        }

        return new TypeGuess(EnumType::class, ['class' => ltrim($enum, '?')], Guess::HIGH_CONFIDENCE);
    }

    public function guessRequired(string $class, string $property): ?ValueGuess
    {
        if (!($enum = $this->getPropertyType($class, $property))) {
            return null;
        }

        return new ValueGuess('?' !== $enum[0], Guess::HIGH_CONFIDENCE);
    }

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    public function guessPattern(string $class, string $property): ?ValueGuess
Repository: symfony/symfony
File: src/Symfony/Component/Form/EnumFormTypeGuesser.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\Form;

use Symfony\Component\Form\Extension\Core\Type\EnumType as FormEnumType;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;

final class EnumFormTypeGuesser implements FormTypeGuesserInterface
{
    private array $cache = [];

    public function guessType(string $class, string $property): ?TypeGuess
    {
        if (null === ($propertyType = $this->getPropertyType($class, $property))) {
            return null;
        }

        return new TypeGuess(FormEnumType::class, ['class' => $propertyType->getName()], Guess::HIGH_CONFIDENCE);
    }

    public function guessRequired(string $class, string $property): ?ValueGuess
    {
        if (null === $propertyType = $this->getPropertyType($class, $property)) {
            return null;
        }

        return new ValueGuess(!$propertyType->allowsNull(), Guess::HIGH_CONFIDENCE);
    }

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    public function guessPattern(string $class, string $property): ?ValueGuess
    {
        return null;
    }


Review comment (suggestion):
```suggestion
        if (!$propertyType = $this->getPropertyType($class, $property)) {
```

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\Form;

use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;

final class EnumFormTypeGuesser implements FormTypeGuesserInterface
{
    /**
     * @var array<string, array<string, string|false>>
     */
    private array $cache = [];

    public function guessType(string $class, string $property): ?TypeGuess
    {
        if (!($enum = $this->getPropertyType($class, $property))) {
            return null;
        }

        return new TypeGuess(EnumType::class, ['class' => ltrim($enum, '?')], Guess::HIGH_CONFIDENCE);
    }

    public function guessRequired(string $class, string $property): ?ValueGuess
    {
        if (!($enum = $this->getPropertyType($class, $property))) {
            return null;
        }

        return new ValueGuess('?' !== $enum[0], Guess::HIGH_CONFIDENCE);
    }

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    public function guessPattern(string $class, string $property): ?ValueGuess
    {
Repository: symfony/symfony
File: src/Symfony/Component/Form/EnumFormTypeGuesser.php (PHP)

Code before:

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    public function guessPattern(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    private function getPropertyType(string $class, string $property): ?\ReflectionNamedType
    {
        if (isset($this->cache[$class]) && \array_key_exists($property, $this->cache[$class])) {
            return $this->cache[$class][$property];
        }

        if (!isset($this->cache[$class])) {
            $this->cache[$class] = [];
        }

        try {
            $classReflection = new \ReflectionClass($class);
            $propertyReflection = $classReflection->getProperty($property);
        } catch (\ReflectionException) {
            return $this->cache[$class][$property] = null;
        }

        if (!$propertyReflection->getType() instanceof \ReflectionNamedType || !enum_exists($propertyReflection->getType()->getName())) {
            return $this->cache[$class][$property] = null;
        }

        return $this->cache[$class][$property] = $propertyReflection->getType();
    }
}


Review comment (performance):
```suggestion
            return $this->cache[$class][$property] = false;
```

Code after:

        return new ValueGuess('?' !== $enum[0], Guess::HIGH_CONFIDENCE);
    }

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    public function guessPattern(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    private function getPropertyType(string $class, string $property): string|false
    {
        if (isset($this->cache[$class][$property])) {
            return $this->cache[$class][$property];
        }

        try {
            $propertyReflection = new \ReflectionProperty($class, $property);
        } catch (\ReflectionException) {
            return $this->cache[$class][$property] = false;
        }

        $type = $propertyReflection->getType();
        if (!$type instanceof \ReflectionNamedType || !enum_exists($type->getName())) {
            $enum = false;
        } else {
            $enum = $type->getName();
            if ($type->allowsNull()) {
                $enum = '?'.$enum;
            }
        }

        return $this->cache[$class][$property] = $enum;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Form/EnumFormTypeGuesser.php (PHP)

Code before:
        return new ValueGuess(!$propertyType->allowsNull(), Guess::HIGH_CONFIDENCE);
    }

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    public function guessPattern(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    private function getPropertyType(string $class, string $property): ?\ReflectionNamedType
    {
        if (isset($this->cache[$class]) && \array_key_exists($property, $this->cache[$class])) {
            return $this->cache[$class][$property];
        }

        if (!isset($this->cache[$class])) {
            $this->cache[$class] = [];
        }

        try {
            $classReflection = new \ReflectionClass($class);
            $propertyReflection = $classReflection->getProperty($property);
        } catch (\ReflectionException) {
            return $this->cache[$class][$property] = null;
        }

        if (!$propertyReflection->getType() instanceof \ReflectionNamedType || !enum_exists($propertyReflection->getType()->getName())) {
            return $this->cache[$class][$property] = null;
        }

        return $this->cache[$class][$property] = $propertyReflection->getType();
    }
}


Review comment (suggestion):
```suggestion
            $propertyReflection = new \ReflectionProperty($class, $property);
```

Code after:
            return null;
        }

        return new ValueGuess('?' !== $enum[0], Guess::HIGH_CONFIDENCE);
    }

    public function guessMaxLength(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    public function guessPattern(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    private function getPropertyType(string $class, string $property): string|false
    {
        if (isset($this->cache[$class][$property])) {
            return $this->cache[$class][$property];
        }

        try {
            $propertyReflection = new \ReflectionProperty($class, $property);
        } catch (\ReflectionException) {
            return $this->cache[$class][$property] = false;
        }

        $type = $propertyReflection->getType();
        if (!$type instanceof \ReflectionNamedType || !enum_exists($type->getName())) {
            $enum = false;
        } else {
            $enum = $type->getName();
            if ($type->allowsNull()) {
                $enum = '?'.$enum;
            }
        }

        return $this->cache[$class][$property] = $enum;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Form/EnumFormTypeGuesser.php (PHP)

Code before:
    {
        return null;
    }

    private function getPropertyType(string $class, string $property): ?\ReflectionNamedType
    {
        if (isset($this->cache[$class]) && \array_key_exists($property, $this->cache[$class])) {
            return $this->cache[$class][$property];
        }

        if (!isset($this->cache[$class])) {
            $this->cache[$class] = [];
        }

        try {
            $classReflection = new \ReflectionClass($class);
            $propertyReflection = $classReflection->getProperty($property);
        } catch (\ReflectionException) {
            return $this->cache[$class][$property] = null;
        }

        if (!$propertyReflection->getType() instanceof \ReflectionNamedType || !enum_exists($propertyReflection->getType()->getName())) {
            return $this->cache[$class][$property] = null;
        }

        return $this->cache[$class][$property] = $propertyReflection->getType();
    }
}


Review comment (performance):
```suggestion
        $type = $propertyReflection->getType();

        if (!$type instanceof \ReflectionNamedType || !enum_exists($enum->getName())) {
            $type = false;
        }

        return $this->cache[$class][$property] = $type;
```

Code after:
    }

    public function guessPattern(string $class, string $property): ?ValueGuess
    {
        return null;
    }

    private function getPropertyType(string $class, string $property): string|false
    {
        if (isset($this->cache[$class][$property])) {
            return $this->cache[$class][$property];
        }

        try {
            $propertyReflection = new \ReflectionProperty($class, $property);
        } catch (\ReflectionException) {
            return $this->cache[$class][$property] = false;
        }

        $type = $propertyReflection->getType();
        if (!$type instanceof \ReflectionNamedType || !enum_exists($type->getName())) {
            $enum = false;
        } else {
            $enum = $type->getName();
            if ($type->allowsNull()) {
                $enum = '?'.$enum;
            }
        }

        return $this->cache[$class][$property] = $enum;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Form/EnumFormTypeGuesser.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\Form;

use Symfony\Component\Form\Extension\Core\Type\EnumType as FormEnumType;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;

final class EnumFormTypeGuesser implements FormTypeGuesserInterface
{
    /**
     * @var array<string, array<string, \ReflectionNamedType|false>>
     */
    private array $cache = [];

    public function guessType(string $class, string $property): ?TypeGuess
    {
        if (!($propertyType = $this->getPropertyType($class, $property))) {
            return null;
        }

        return new TypeGuess(FormEnumType::class, ['class' => $propertyType->getName()], Guess::HIGH_CONFIDENCE);
    }

    public function guessRequired(string $class, string $property): ?ValueGuess
    {
        if (!($propertyType = $this->getPropertyType($class, $property))) {
            return null;
        }

Review comment (suggestion):
```suggestion
use Symfony\Component\Form\Extension\Core\Type\EnumType;
```

I don't see why we need the alias here

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\Form;

use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;

final class EnumFormTypeGuesser implements FormTypeGuesserInterface
{
    /**
     * @var array<string, array<string, string|false>>
     */
    private array $cache = [];

    public function guessType(string $class, string $property): ?TypeGuess
    {
        if (!($enum = $this->getPropertyType($class, $property))) {
            return null;
        }

        return new TypeGuess(EnumType::class, ['class' => ltrim($enum, '?')], Guess::HIGH_CONFIDENCE);
    }

    public function guessRequired(string $class, string $property): ?ValueGuess
    {
        if (!($enum = $this->getPropertyType($class, $property))) {
            return null;
        }