Pergunta

Eu procurando uma maneira (regex, fragmento ,plugin, etc) para converter as antigas matrizes com a nova sintaxe php com sublimeText.

// Old synthax
$var = array(
   'foo' => 'bar' 
);

// New synthax
$var = [
   'foo' => 'bar'
];

alguém tem uma idéia ?

Foi útil?

Solução

Eu encontrei um script que faz o trabalho perfeitamente!

https://github.com/thomasbachem/php-short-array-Conversor de sintaxe

Outras dicas

Eu descobri que também é possível fazer isso com o php codesniffer: https://github.com/squizlabs/PHP_CodeSniffer

phpcbf src/ --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax

Neste exemplo, você tem que substituir src/ com a pasta que contém os scripts.Como alternativa, você pode fornecer um nome de arquivo.

Talvez um pouco tarde, mas eu criei o meu próprio.Talvez não muito, mas ele faz o trabalho que eu quero.Se você não gosta de guia, alterar no travessão função para 2 ou 4 espaços.

    function loopArray(array $array, $loopcount = 0) {

        $returnString = ($loopcount == 0) ? "[\n" : "";

        $tabKey = indent($loopcount + 2);
        $tabValue = indent($loopcount + 3);

        $lastKey = array_key_last($array);

        foreach ($array as $key => $value) {

            $totalChildren = count($array[$key]);

            $returnString .= $tabKey . '"' . $key . '" => ';

            if ($totalChildren == 0) $returnString .= '[]';
            if ($totalChildren > 0 && is_array($array[$key])) $returnString .= '[' . "\n";

            if (is_array($value)) {

                $returnString .= loopArray($value, $loopcount + 1);
            } else {

                if ($totalChildren == 1) $returnString .=  '"' . $value . '"';
                if ($totalChildren > 1)  $returnString .=  $tabValue . '"' . $value . '"'  . ",\n";
            }

            $returnString .= ($lastKey == $key) ? "\n" . indent($loopcount+1) . "]" : ",\n";
        }

        return $returnString;
    }

    function indent($ammount) {

        return str_repeat("\t", $ammount);
    }

/** use function below only prior to php 7.3 */

    function array_key_last(array $array) {

        $key = NULL;

        if ( is_array( $array ) ) {

            end( $array );
            $key = key( $array );
        }

        return $key;
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top