<?php

declare (strict_types=1);
/*
 * This file is part of PHP CS Fixer.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */
namespace PhpCsFixer\Fixer\Basic;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\FixerDefinition\VersionSpecification;
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
/**
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
 */
final class OctalNotationFixer extends AbstractFixer
{
    public function getDefinition() : FixerDefinitionInterface
    {
        return new FixerDefinition('Literal octal must be in `0o` notation.', [new VersionSpecificCodeSample("<?php \$foo = 0123;\n", new VersionSpecification(80100))]);
    }
    public function isCandidate(Tokens $tokens) : bool
    {
        return \PHP_VERSION_ID >= 80100 && $tokens->isTokenKindFound(\T_LNUMBER);
    }
    protected function applyFix(\SplFileInfo $file, Tokens $tokens) : void
    {
        foreach ($tokens as $index => $token) {
            if (!$token->isGivenKind(\T_LNUMBER)) {
                continue;
            }
            $content = $token->getContent();
            $newContent = Preg::replace('#^0_*+([0-7_]+)$#', '0o$1', $content);
            if ($content === $newContent) {
                continue;
            }
            $tokens[$index] = new Token([\T_LNUMBER, $newContent]);
        }
    }
}
