Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php declare(strict_types=1); /* * This file is part of PHP CS Fixer. * * (c) Fabien..

Decoded Output download

<?php

declare(strict_types=1);

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) Fabien Potencier <[email protected]>
 *     Dariusz Rumiski <[email protected]>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace PhpCsFixer\Fixer\ControlStructure;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\TokensAnalyzer;

/**
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
 *
 * @phpstan-type _AutogeneratedInputConfiguration array{
 *  style?: 'braces'|'semicolon'
 * }
 * @phpstan-type _AutogeneratedComputedConfiguration array{
 *  style: 'braces'|'semicolon'
 * }
 */
final class EmptyLoopBodyFixer extends AbstractFixer implements ConfigurableFixerInterface
{
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
    use ConfigurableFixerTrait;

    private const STYLE_BRACES = 'braces';

    private const STYLE_SEMICOLON = 'semicolon';

    private const TOKEN_LOOP_KINDS = [T_FOR, T_FOREACH, T_WHILE];

    public function getDefinition(): FixerDefinitionInterface
    {
        return new FixerDefinition(
            'Empty loop-body must be in configured style.',
            [
                new CodeSample("<?php while(foo()){}
"),
                new CodeSample(
                    "<?php while(foo());
",
                    [
                        'style' => 'braces',
                    ]
                ),
            ]
        );
    }

    /**
     * {@inheritdoc}
     *
     * Must run before BracesFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer.
     * Must run after NoEmptyStatementFixer.
     */
    public function getPriority(): int
    {
        return 39;
    }

    public function isCandidate(Tokens $tokens): bool
    {
        return $tokens->isAnyTokenKindsFound(self::TOKEN_LOOP_KINDS);
    }

    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
    {
        if (self::STYLE_BRACES === $this->configuration['style']) {
            $analyzer = new TokensAnalyzer($tokens);
            $fixLoop = static function (int $index, int $endIndex) use ($tokens, $analyzer): void {
                if ($tokens[$index]->isGivenKind(T_WHILE) && $analyzer->isWhilePartOfDoWhile($index)) {
                    return;
                }

                $semiColonIndex = $tokens->getNextMeaningfulToken($endIndex);

                if (!$tokens[$semiColonIndex]->equals(';')) {
                    return;
                }

                $tokens[$semiColonIndex] = new Token('{');
                $tokens->insertAt($semiColonIndex + 1, new Token('}'));
            };
        } else {
            $fixLoop = static function (int $index, int $endIndex) use ($tokens): void {
                $braceOpenIndex = $tokens->getNextMeaningfulToken($endIndex);

                if (!$tokens[$braceOpenIndex]->equals('{')) {
                    return;
                }

                $braceCloseIndex = $tokens->getNextNonWhitespace($braceOpenIndex);

                if (!$tokens[$braceCloseIndex]->equals('}')) {
                    return;
                }

                $tokens[$braceOpenIndex] = new Token(';');
                $tokens->clearTokenAndMergeSurroundingWhitespace($braceCloseIndex);
            };
        }

        for ($index = $tokens->count() - 1; $index > 0; --$index) {
            if ($tokens[$index]->isGivenKind(self::TOKEN_LOOP_KINDS)) {
                $endIndex = $tokens->getNextTokenOfKind($index, ['(']); // proceed to open '('
                $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $endIndex); // proceed to close ')'
                $fixLoop($index, $endIndex); // fix loop if needs fixing
            }
        }
    }

    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
    {
        return new FixerConfigurationResolver([
            (new FixerOptionBuilder('style', 'Style of empty loop-bodies.'))
                ->setAllowedTypes(['string'])
                ->setAllowedValues([self::STYLE_BRACES, self::STYLE_SEMICOLON])
                ->setDefault(self::STYLE_SEMICOLON)
                ->getOption(),
        ]);
    }
}
 ?>

Did this file decode correctly?

Original Code

<?php

declare(strict_types=1);

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) Fabien Potencier <[email protected]>
 *     Dariusz Rumiski <[email protected]>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace PhpCsFixer\Fixer\ControlStructure;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\TokensAnalyzer;

/**
 * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
 *
 * @phpstan-type _AutogeneratedInputConfiguration array{
 *  style?: 'braces'|'semicolon'
 * }
 * @phpstan-type _AutogeneratedComputedConfiguration array{
 *  style: 'braces'|'semicolon'
 * }
 */
final class EmptyLoopBodyFixer extends AbstractFixer implements ConfigurableFixerInterface
{
    /** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
    use ConfigurableFixerTrait;

    private const STYLE_BRACES = 'braces';

    private const STYLE_SEMICOLON = 'semicolon';

    private const TOKEN_LOOP_KINDS = [T_FOR, T_FOREACH, T_WHILE];

    public function getDefinition(): FixerDefinitionInterface
    {
        return new FixerDefinition(
            'Empty loop-body must be in configured style.',
            [
                new CodeSample("<?php while(foo()){}\n"),
                new CodeSample(
                    "<?php while(foo());\n",
                    [
                        'style' => 'braces',
                    ]
                ),
            ]
        );
    }

    /**
     * {@inheritdoc}
     *
     * Must run before BracesFixer, NoExtraBlankLinesFixer, NoTrailingWhitespaceFixer.
     * Must run after NoEmptyStatementFixer.
     */
    public function getPriority(): int
    {
        return 39;
    }

    public function isCandidate(Tokens $tokens): bool
    {
        return $tokens->isAnyTokenKindsFound(self::TOKEN_LOOP_KINDS);
    }

    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
    {
        if (self::STYLE_BRACES === $this->configuration['style']) {
            $analyzer = new TokensAnalyzer($tokens);
            $fixLoop = static function (int $index, int $endIndex) use ($tokens, $analyzer): void {
                if ($tokens[$index]->isGivenKind(T_WHILE) && $analyzer->isWhilePartOfDoWhile($index)) {
                    return;
                }

                $semiColonIndex = $tokens->getNextMeaningfulToken($endIndex);

                if (!$tokens[$semiColonIndex]->equals(';')) {
                    return;
                }

                $tokens[$semiColonIndex] = new Token('{');
                $tokens->insertAt($semiColonIndex + 1, new Token('}'));
            };
        } else {
            $fixLoop = static function (int $index, int $endIndex) use ($tokens): void {
                $braceOpenIndex = $tokens->getNextMeaningfulToken($endIndex);

                if (!$tokens[$braceOpenIndex]->equals('{')) {
                    return;
                }

                $braceCloseIndex = $tokens->getNextNonWhitespace($braceOpenIndex);

                if (!$tokens[$braceCloseIndex]->equals('}')) {
                    return;
                }

                $tokens[$braceOpenIndex] = new Token(';');
                $tokens->clearTokenAndMergeSurroundingWhitespace($braceCloseIndex);
            };
        }

        for ($index = $tokens->count() - 1; $index > 0; --$index) {
            if ($tokens[$index]->isGivenKind(self::TOKEN_LOOP_KINDS)) {
                $endIndex = $tokens->getNextTokenOfKind($index, ['(']); // proceed to open '('
                $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $endIndex); // proceed to close ')'
                $fixLoop($index, $endIndex); // fix loop if needs fixing
            }
        }
    }

    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
    {
        return new FixerConfigurationResolver([
            (new FixerOptionBuilder('style', 'Style of empty loop-bodies.'))
                ->setAllowedTypes(['string'])
                ->setAllowedValues([self::STYLE_BRACES, self::STYLE_SEMICOLON])
                ->setDefault(self::STYLE_SEMICOLON)
                ->getOption(),
        ]);
    }
}

Function Calls

None

Variables

None

Stats

MD5 0fbc04bf5c2836e2c80d8ef384f0756b
Eval Count 0
Decode Time 96 ms