%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home2/vacivi36/code/vendor/friendsofphp/php-cs-fixer/src/Fixer/Naming/
Upload File :
Create Path :
Current File : //home2/vacivi36/code/vendor/friendsofphp/php-cs-fixer/src/Fixer/Naming/NoHomoglyphNamesFixer.php

<?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\Naming;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

/**
 * @author Fred Cox <mcfedr@gmail.com>
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
 */
final class NoHomoglyphNamesFixer extends AbstractFixer
{
    /**
     * Used the program https://github.com/mcfedr/homoglyph-download
     * to generate this list from
     * http://homoglyphs.net/?text=abcdefghijklmnopqrstuvwxyz&lang=en&exc7=1&exc8=1&exc13=1&exc14=1.
     *
     * Symbols replaced include
     * - Latin homoglyphs
     * - IPA extensions
     * - Greek and Coptic
     * - Cyrillic
     * - Cyrillic Supplement
     * - Letterlike Symbols
     * - Latin Numbers
     * - Fullwidth Latin
     *
     * This is not the complete list of unicode homographs, but limited
     * to those you are more likely to have typed/copied by accident
     */
    private static array $replacements = [
        'O' => '0',
        '0' => '0',
        'I' => '1',
        '1' => '1',
        '2' => '2',
        '3' => '3',
        '4' => '4',
        '5' => '5',
        '6' => '6',
        '7' => '7',
        '8' => '8',
        '9' => '9',
        'Α' => 'A',
        'А' => 'A',
        'A' => 'A',
        'ʙ' => 'B',
        'Β' => 'B',
        'В' => 'B',
        'B' => 'B',
        'Ϲ' => 'C',
        'С' => 'C',
        'Ⅽ' => 'C',
        'C' => 'C',
        'Ⅾ' => 'D',
        'D' => 'D',
        'Ε' => 'E',
        'Е' => 'E',
        'E' => 'E',
        'Ϝ' => 'F',
        'F' => 'F',
        'ɢ' => 'G',
        'Ԍ' => 'G',
        'G' => 'G',
        'ʜ' => 'H',
        'Η' => 'H',
        'Н' => 'H',
        'H' => 'H',
        'l' => 'I',
        'Ι' => 'I',
        'І' => 'I',
        'Ⅰ' => 'I',
        'I' => 'I',
        'Ј' => 'J',
        'J' => 'J',
        'Κ' => 'K',
        'К' => 'K',
        'K' => 'K',
        'K' => 'K',
        'ʟ' => 'L',
        'Ⅼ' => 'L',
        'L' => 'L',
        'Μ' => 'M',
        'М' => 'M',
        'Ⅿ' => 'M',
        'M' => 'M',
        'ɴ' => 'N',
        'Ν' => 'N',
        'N' => 'N',
        'Ο' => 'O',
        'О' => 'O',
        'O' => 'O',
        'Ρ' => 'P',
        'Р' => 'P',
        'P' => 'P',
        'Q' => 'Q',
        'ʀ' => 'R',
        'R' => 'R',
        'Ѕ' => 'S',
        'S' => 'S',
        'Τ' => 'T',
        'Т' => 'T',
        'T' => 'T',
        'U' => 'U',
        'Ѵ' => 'V',
        'Ⅴ' => 'V',
        'V' => 'V',
        'W' => 'W',
        'Χ' => 'X',
        'Х' => 'X',
        'Ⅹ' => 'X',
        'X' => 'X',
        'ʏ' => 'Y',
        'Υ' => 'Y',
        'Ү' => 'Y',
        'Y' => 'Y',
        'Ζ' => 'Z',
        'Z' => 'Z',
        '_' => '_',
        'ɑ' => 'a',
        'а' => 'a',
        'a' => 'a',
        'Ь' => 'b',
        'b' => 'b',
        'ϲ' => 'c',
        'с' => 'c',
        'ⅽ' => 'c',
        'c' => 'c',
        'ԁ' => 'd',
        'ⅾ' => 'd',
        'd' => 'd',
        'е' => 'e',
        'e' => 'e',
        'f' => 'f',
        'ɡ' => 'g',
        'g' => 'g',
        'һ' => 'h',
        'h' => 'h',
        'ɩ' => 'i',
        'і' => 'i',
        'ⅰ' => 'i',
        'i' => 'i',
        'ј' => 'j',
        'j' => 'j',
        'k' => 'k',
        'ⅼ' => 'l',
        'l' => 'l',
        'ⅿ' => 'm',
        'm' => 'm',
        'n' => 'n',
        'ο' => 'o',
        'о' => 'o',
        'o' => 'o',
        'р' => 'p',
        'p' => 'p',
        'q' => 'q',
        'r' => 'r',
        'ѕ' => 's',
        's' => 's',
        't' => 't',
        'u' => 'u',
        'ν' => 'v',
        'ѵ' => 'v',
        'ⅴ' => 'v',
        'v' => 'v',
        'ѡ' => 'w',
        'w' => 'w',
        'х' => 'x',
        'ⅹ' => 'x',
        'x' => 'x',
        'у' => 'y',
        'y' => 'y',
        'z' => 'z',
    ];

    /**
     * {@inheritdoc}
     */
    public function getDefinition(): FixerDefinitionInterface
    {
        return new FixerDefinition(
            'Replace accidental usage of homoglyphs (non ascii characters) in names.',
            [new CodeSample("<?php \$nаmе = 'wrong \"a\" character';\n")],
            null,
            'Renames classes and cannot rename the files. You might have string references to renamed code (`$$name`).'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function isRisky(): bool
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isCandidate(Tokens $tokens): bool
    {
        return $tokens->isAnyTokenKindsFound([T_VARIABLE, T_STRING]);
    }

    /**
     * {@inheritdoc}
     */
    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
    {
        foreach ($tokens as $index => $token) {
            if (!$token->isGivenKind([T_VARIABLE, T_STRING])) {
                continue;
            }

            $replaced = Preg::replaceCallback('/[^[:ascii:]]/u', static function (array $matches): string {
                return self::$replacements[$matches[0]] ?? $matches[0];
            }, $token->getContent(), -1, $count);

            if ($count) {
                $tokens->offsetSet($index, new Token([$token->getId(), $replaced]));
            }
        }
    }
}

Zerion Mini Shell 1.0