ronantakizawa/codereview-bench huggingface dataset:
Repository: symfony/symfony
File: src/Symfony/Component/TypeInfo/Type/ArrayShapeType.php (PHP)

Code before:
    public function getShape(): array
    {
        return $this->shape;
    }

    public function isSealed(): bool
    {
        return $this->sealed;
    }

    public function accepts(mixed $value): bool
    {
        if (!\is_array($value)) {
            return false;
        }

        foreach ($this->shape as $key => $shapeValue) {
            if (!($shapeValue['optional'] ?? false) && !\array_key_exists($key, $value)) {
                return false;
            }
        }

        foreach ($value as $key => $itemValue) {
            $valueType = $this->shape[$key]['type'] ?? false;
            if (!$valueType) {
                return !$this->sealed;
            }

            if (!$valueType->accepts($itemValue)) {
                return false;
            }
        }

        return true;
    }

    public function __toString(): string
    {
        $items = [];

        foreach ($this->shape as $key => $value) {
            $itemKey = \is_int($key) ? (string) $key : \sprintf("'%s'", $key);
            if ($value['optional'] ?? false) {
                $itemKey = \sprintf('%s?', $itemKey);
            }

            $items[] = \sprintf('%s: %s', $itemKey, $value['type']);
        }

        if (!$this->sealed) {
            $items[] = '...';

Review comment (suggestion):
It should continue if it's not sealed, the type must be validated for each declared key.

Code after:
        }

        if ($extraKeyType && !$extraKeyType->isIdentifiedBy(TypeIdentifier::INT, TypeIdentifier::STRING)) {
            throw new InvalidArgumentException(\sprintf('"%s" is not a valid array key type.', (string) $extraKeyType));
        }
    }

    /**
     * @return array<array{type: Type, optional: bool}>
     */
    public function getShape(): array
    {
        return $this->shape;
    }

    public function isSealed(): bool
    {
        return null === $this->extraValueType;
    }

    public function getExtraKeyType(): ?Type
    {
        return $this->extraKeyType;
    }

    public function getExtraValueType(): ?Type
    {
        return $this->extraKeyType;
    }

    public function accepts(mixed $value): bool
    {
        if (!\is_array($value)) {
            return false;
        }

        foreach ($this->shape as $key => $shapeValue) {
            if (!($shapeValue['optional'] ?? false) && !\array_key_exists($key, $value)) {
                return false;
            }
        }

        foreach ($value as $key => $itemValue) {
            $valueType = $this->shape[$key]['type'] ?? false;

            if ($valueType && !$valueType->accepts($itemValue)) {
                return false;
            }

            if (!$valueType && ($this->isSealed() || !$this->extraKeyType->accepts($key) || !$this->extraValueType->accepts($itemValue))) {
                return false;
Repository: symfony/symfony
File: src/Symfony/Component/Lock/Tests/Serializer/LockKeyNormalizerTest.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\Lock\Tests\Serializer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Lock\Exception\UnserializableKeyException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Serializer\LockKeyNormalizer;

class LockKeyNormalizerTest extends TestCase
{
    private LockKeyNormalizer $normalizer;

    protected function setUp(): void
    {
        $this->normalizer = new LockKeyNormalizer();
    }

    public function testNormalizeAndDenormalize()
    {
        $key = new Key(__METHOD__);
        $key->reduceLifetime(1);
        $key->setState('foo', 'bar');

        $copy = $this->normalizer->denormalize($this->normalizer->normalize($key), Key::class);
        $this->assertSame($key->getState('foo'), $copy->getState('foo'));
        $this->assertEqualsWithDelta($key->getRemainingLifetime(), $copy->getRemainingLifetime(), 0.001);
    }

    public function testNormalizingUnserializableLockThrows()
    {
        $key = new Key(__METHOD__);
        $key->markUnserializable();

        $this->expectException(UnserializableKeyException::class);
        $this->normalizer->normalize($key);
    }
}


Review comment (refactor):
Let's remove this setup and create new instances in each test for more isolation and less memory consumption.

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\Lock\Tests\Serializer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Lock\Exception\UnserializableKeyException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Serializer\LockKeyNormalizer;

class LockKeyNormalizerTest extends TestCase
{
    public function testNormalizeAndDenormalize()
    {
        $key = new Key(__METHOD__);
        $key->reduceLifetime(1);
        $key->setState('foo', 'bar');
        $normalizer = new LockKeyNormalizer();

        $copy = $normalizer->denormalize($normalizer->normalize($key), Key::class);
        $this->assertSame($key->getState('foo'), $copy->getState('foo'));
        $this->assertEqualsWithDelta($key->getRemainingLifetime(), $copy->getRemainingLifetime(), 0.001);
    }

    public function testNormalizingUnserializableLockThrows()
    {
        $key = new Key(__METHOD__);
        $key->markUnserializable();
        $normalizer = new LockKeyNormalizer();

        $this->expectException(UnserializableKeyException::class);
        $normalizer->normalize($key);
    }
}
Repository: symfony/symfony
File: src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php (PHP)

Code before:
        }
        if (!$this->output) {
            return new ConsoleFormatter($this->consoleFormatterOptions);
        }

        return new ConsoleFormatter(array_replace([
            'colors' => $this->output->isDecorated(),
            'multiline' => OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity(),
        ], $this->consoleFormatterOptions));
    }

    /**
     * Updates the logging level based on the verbosity setting of the console output.
     *
     * @return bool Whether the handler is enabled and verbosity is not set to quiet
     */
    private function updateLevel(): bool
    {
        if (null === $this->output) {
            return false;
        }

        $verbosity = $this->output->getVerbosity();
        if (isset($this->verbosityLevelMap[$verbosity])) {
            $this->setLevel($this->verbosityLevelMap[$verbosity]);
        } elseif (!$this->handleSilent && $verbosity === OutputInterface::VERBOSITY_SILENT) {
            return false;
        } else {
            $this->setLevel(Level::Debug);
        }

        return true;
    }
}


Review comment (suggestion):
```suggestion
        } elseif (!$this->handleSilent && OutputInterface::VERBOSITY_SILENT === $verbosity) {
```

Code after:
        if (!$this->output) {
            return new ConsoleFormatter($this->consoleFormatterOptions);
        }

        return new ConsoleFormatter(array_replace([
            'colors' => $this->output->isDecorated(),
            'multiline' => OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity(),
        ], $this->consoleFormatterOptions));
    }

    /**
     * Updates the logging level based on the verbosity setting of the console output.
     *
     * @return bool Whether the handler is enabled and verbosity is not set to quiet
     */
    private function updateLevel(): bool
    {
        if (null === $this->output) {
            return false;
        }

        $verbosity = $this->output->getVerbosity();
        if (isset($this->verbosityLevelMap[$verbosity])) {
            $this->setLevel($this->verbosityLevelMap[$verbosity]);
        } elseif (\defined('\Symfony\Component\Console\Output\OutputInterface::VERBOSITY_SILENT') && OutputInterface::VERBOSITY_SILENT === $verbosity) {
            return false;
        } else {
            $this->setLevel(Level::Debug);
        }

        return true;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Security/Core/Authorization/Strategy/AccessDecisionStrategyInterface.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\Security\Core\Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\AccessDecision;

/**
 * A strategy for turning a stream of votes into a final decision.
 *
 * @author Alexander M. Turek <me@derrabus.de>
 */
interface AccessDecisionStrategyInterface
{
    /**
     * @param \Traversable<int> $results
     */
    public function decide(\Traversable $results, /* AccessDecision $accessDecision = null */): bool;
}


Review comment (suggestion):
```suggestion
    * @param \Traversable<int> $results
    * @param ?AccessDecision   $accessDecision
    * 
    */
    public function decide(\Traversable $results/* , ?AccessDecision $accessDecision = null */): bool;
```

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\Security\Core\Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\AccessDecision;

/**
 * A strategy for turning a stream of votes into a final decision.
 *
 * @author Alexander M. Turek <me@derrabus.de>
 */
interface AccessDecisionStrategyInterface
{
    /**
     * @param \Traversable<int> $results
     * @param ?AccessDecision   $accessDecision
     */
    public function decide(\Traversable $results/* , ?AccessDecision $accessDecision = null */): bool;
}
Repository: symfony/symfony
File: src/Symfony/Component/Security/Core/Authorization/Voter/Vote.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\Security\Core\Authorization\Voter;

class Vote
{
    /**
     * @var class-string<VoterInterface>|string
     */
    public string $voter;

    /**
     * @var VoterInterface::ACCESS_*
     */
    public int $result;

    /**
     * @var list<string>
     */
    public array $reasons = [];

    public array|object|null $extraData = null;

    public function addReason(string $reason): void
    {
        $this->reasons[] = $reason;
    }
}


Review comment (suggestion):
I would only accept an array, to encourage ppl that use this to properly use keys as namespaces:
```suggestion
    public array $extraData = [];
```

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\Security\Core\Authorization\Voter;

class Vote
{
    /**
     * @var class-string<VoterInterface>|string
     */
    public string $voter;

    /**
     * @var VoterInterface::ACCESS_*
     */
    public int $result;

    /**
     * @var list<string>
     */
    public array $reasons = [];

    /**
     * @var array<string, mixed>
     */
    public array $extraData = [];

    public function addReason(string $reason): void
    {
        $this->reasons[] = $reason;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Security/Core/Authorization/Voter/Vote.php (PHP)

Code before:
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\Authorization\Voter;

class Vote
{
    /**
     * @var class-string<VoterInterface>|string
     */
    public string $voter;

    /**
     * @var VoterInterface::ACCESS_*
     */
    public int $result;

    /**
     * @var list<string>
     */
    public array $reasons = [];

    public array $extraData = [];

    public function addReason(string $reason): void
    {
        $this->reasons[] = $reason;
    }
}


Review comment (suggestion):
```suggestion
    /**
     * @var array<string, mixed>
     */
    public array $extraData = [];
```

Code after:
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\Authorization\Voter;

class Vote
{
    /**
     * @var class-string<VoterInterface>|string
     */
    public string $voter;

    /**
     * @var VoterInterface::ACCESS_*
     */
    public int $result;

    /**
     * @var list<string>
     */
    public array $reasons = [];

    /**
     * @var array<string, mixed>
     */
    public array $extraData = [];

    public function addReason(string $reason): void
    {
        $this->reasons[] = $reason;
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Security/Core/Authorization/Strategy/AccessDecisionStrategyInterface.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\Security\Core\Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\AccessDecision;

/**
 * A strategy for turning a stream of votes into a final decision.
 *
 * @author Alexander M. Turek <me@derrabus.de>
 */
interface AccessDecisionStrategyInterface
{
    /**
     * @param \Traversable<int> $results
     * @param ?AccessDecision   $accessDecision
     */
    public function decide(\Traversable $results/* , AccessDecision $accessDecision = null */): bool;
}


Review comment (suggestion):
```suggestion
    public function decide(\Traversable $results/* , ?AccessDecision $accessDecision = null */): bool;
```

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\Security\Core\Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\AccessDecision;

/**
 * A strategy for turning a stream of votes into a final decision.
 *
 * @author Alexander M. Turek <me@derrabus.de>
 */
interface AccessDecisionStrategyInterface
{
    /**
     * @param \Traversable<int> $results
     * @param ?AccessDecision   $accessDecision
     */
    public function decide(\Traversable $results/* , ?AccessDecision $accessDecision = null */): bool;
}
Repository: symfony/symfony
File: src/Symfony/Component/Security/Core/Authorization/Strategy/AffirmativeStrategy.php (PHP)

Code before:
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\AccessDecision;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

/**
 * Grants access if any voter returns an affirmative response.
 *
 * If all voters abstained from voting, the decision will be based on the
 * allowIfAllAbstainDecisions property value (defaults to false).
 *
 * @author Fabien Potencier <fabien@symfony.com>
 * @author Alexander M. Turek <me@derrabus.de>
 */
final class AffirmativeStrategy implements AccessDecisionStrategyInterface, \Stringable
{
    public function __construct(
        private bool $allowIfAllAbstainDecisions = false,
    ) {
    }

    public function decide(\Traversable $results, ?AccessDecision $accessDecision): bool
    {
        $deny = 0;
        foreach ($results as $result) {
            if (VoterInterface::ACCESS_GRANTED === $result) {
                return true;
            }

            if (VoterInterface::ACCESS_DENIED === $result) {
                ++$deny;
            }
        }

        if ($deny > 0) {
            return false;
        }

        return $this->allowIfAllAbstainDecisions;
    }

    public function __toString(): string
    {
        return 'affirmative';
    }
}


Review comment (suggestion):
```suggestion
    public function decide(\Traversable $results, ?AccessDecision $accessDecision = null): bool
```

Code after:
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\AccessDecision;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

/**
 * Grants access if any voter returns an affirmative response.
 *
 * If all voters abstained from voting, the decision will be based on the
 * allowIfAllAbstainDecisions property value (defaults to false).
 *
 * @author Fabien Potencier <fabien@symfony.com>
 * @author Alexander M. Turek <me@derrabus.de>
 */
final class AffirmativeStrategy implements AccessDecisionStrategyInterface, \Stringable
{
    public function __construct(
        private bool $allowIfAllAbstainDecisions = false,
    ) {
    }

    public function decide(\Traversable $results, ?AccessDecision $accessDecision = null): bool
    {
        $deny = 0;
        foreach ($results as $result) {
            if (VoterInterface::ACCESS_GRANTED === $result) {
                return true;
            }

            if (VoterInterface::ACCESS_DENIED === $result) {
                ++$deny;
            }
        }

        if ($deny > 0) {
            return false;
        }

        return $this->allowIfAllAbstainDecisions;
    }

    public function __toString(): string
    {
        return 'affirmative';
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Security/Core/Authorization/Strategy/PriorityStrategy.php (PHP)

Code before:
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\AccessDecision;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

/**
 * Grant or deny access depending on the first voter that does not abstain.
 * The priority of voters can be used to overrule a decision.
 *
 * If all voters abstained from voting, the decision will be based on the
 * allowIfAllAbstainDecisions property value (defaults to false).
 *
 * @author Fabien Potencier <fabien@symfony.com>
 * @author Alexander M. Turek <me@derrabus.de>
 */
final class PriorityStrategy implements AccessDecisionStrategyInterface, \Stringable
{
    public function __construct(
        private bool $allowIfAllAbstainDecisions = false,
    ) {
    }

    public function decide(\Traversable $results, ?AccessDecision $accessDecision): bool
    {
        foreach ($results as $result) {
            if (VoterInterface::ACCESS_GRANTED === $result) {
                return true;
            }

            if (VoterInterface::ACCESS_DENIED === $result) {
                return false;
            }
        }

        return $this->allowIfAllAbstainDecisions;
    }

    public function __toString(): string
    {
        return 'priority';
    }
}


Review comment (suggestion):
```suggestion
    public function decide(\Traversable $results, ?AccessDecision $accessDecision = null): bool
```

Code after:
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\AccessDecision;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

/**
 * Grant or deny access depending on the first voter that does not abstain.
 * The priority of voters can be used to overrule a decision.
 *
 * If all voters abstained from voting, the decision will be based on the
 * allowIfAllAbstainDecisions property value (defaults to false).
 *
 * @author Fabien Potencier <fabien@symfony.com>
 * @author Alexander M. Turek <me@derrabus.de>
 */
final class PriorityStrategy implements AccessDecisionStrategyInterface, \Stringable
{
    public function __construct(
        private bool $allowIfAllAbstainDecisions = false,
    ) {
    }

    public function decide(\Traversable $results, ?AccessDecision $accessDecision = null): bool
    {
        foreach ($results as $result) {
            if (VoterInterface::ACCESS_GRANTED === $result) {
                return true;
            }

            if (VoterInterface::ACCESS_DENIED === $result) {
                return false;
            }
        }

        return $this->allowIfAllAbstainDecisions;
    }

    public function __toString(): string
    {
        return 'priority';
    }
}
Repository: symfony/symfony
File: src/Symfony/Component/Security/Core/Authorization/Strategy/UnanimousStrategy.php (PHP)

Code before:
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\AccessDecision;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

/**
 * Grants access if only grant (or abstain) votes were received.
 *
 * If all voters abstained from voting, the decision will be based on the
 * allowIfAllAbstainDecisions property value (defaults to false).
 *
 * @author Fabien Potencier <fabien@symfony.com>
 * @author Alexander M. Turek <me@derrabus.de>
 */
final class UnanimousStrategy implements AccessDecisionStrategyInterface, \Stringable
{
    public function __construct(
        private bool $allowIfAllAbstainDecisions = false,
    ) {
    }

    public function decide(\Traversable $results, ?AccessDecision $accessDecision): bool
    {
        $grant = 0;
        foreach ($results as $result) {
            if (VoterInterface::ACCESS_DENIED === $result) {
                return false;
            }

            if (VoterInterface::ACCESS_GRANTED === $result) {
                ++$grant;
            }
        }

        // no deny votes
        if ($grant > 0) {
            return true;
        }

        return $this->allowIfAllAbstainDecisions;
    }

    public function __toString(): string
    {
        return 'unanimous';
    }
}

Review comment (suggestion):
```suggestion
    public function decide(\Traversable $results, ?AccessDecision $accessDecision = null): bool
```

Code after:
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Security\Core\Authorization\Strategy;

use Symfony\Component\Security\Core\Authorization\AccessDecision;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

/**
 * Grants access if only grant (or abstain) votes were received.
 *
 * If all voters abstained from voting, the decision will be based on the
 * allowIfAllAbstainDecisions property value (defaults to false).
 *
 * @author Fabien Potencier <fabien@symfony.com>
 * @author Alexander M. Turek <me@derrabus.de>
 */
final class UnanimousStrategy implements AccessDecisionStrategyInterface, \Stringable
{
    public function __construct(
        private bool $allowIfAllAbstainDecisions = false,
    ) {
    }

    public function decide(\Traversable $results, ?AccessDecision $accessDecision = null): bool
    {
        $grant = 0;
        foreach ($results as $result) {
            if (VoterInterface::ACCESS_DENIED === $result) {
                return false;
            }

            if (VoterInterface::ACCESS_GRANTED === $result) {
                ++$grant;
            }
        }

        // no deny votes
        if ($grant > 0) {
            return true;
        }

        return $this->allowIfAllAbstainDecisions;
    }

    public function __toString(): string
    {
        return 'unanimous';
    }
}