Fonction each() obsolete depuis PHP7.2.0

Fermé
chabinot Messages postés 321 Date d'inscription mardi 10 novembre 2015 Statut Membre Dernière intervention 22 mars 2024 - 10 févr. 2018 à 14:19
chabinot Messages postés 321 Date d'inscription mardi 10 novembre 2015 Statut Membre Dernière intervention 22 mars 2024 - 10 févr. 2018 à 18:39
Bonjour,
La version de PHP que j'utilise est la 7.2.0, j'utilise l'IDE Sublime Text 3 et ce dernier utilise la fonction each() qui est dépréciée depuis la version 7.2.0 de PHP. Ce qui fait qu'à chaque fois que je crée un fichier .php, il me crée un fichier .php.tmp avec le message suivant:
"Deprecated: The each() function is deprecated. This message will be suppressed on further calls in C:\Users\amaury\AppData\Roaming\Sublime Text 3\Packages\PHPTools\php.tools\codeFormatter.php on line 54"
Voici le code de la ligne 54 du fichier de Sublime Text :
while (list($index, $token) = each($this->tkns)) {

qui se trouve dans le bloc suivant :
final class AddMissingCurlyBraces extends FormatterPass {
	public function format($source) {
		$tmp = $this->addBraces($source);
		while (true) {
			$source = $this->addBraces($tmp);
			if ($source == $tmp) {
				break;
			}
			$tmp = $source;
		}
		return $source;
	}
	private function addBraces($source) {
		$this->tkns = token_get_all($source);
		$this->code = '';
		while (list($index, $token) = <gras>each($this->tkns)</gras>) {
			list($id, $text) = $this->get_token($token);
			$this->ptr = $index;
			switch ($id) {
			case T_FOR:
				$this->append_code($text, false);
				$paren_count = null;
				while (list($index, $token) = each($this->tkns)) {
					list($id, $text) = $this->get_token($token);
					$this->ptr = $index;
					if (ST_PARENTHESES_OPEN == $id) {
						$paren_count++;
					} elseif (ST_PARENTHESES_CLOSE == $id) {
						$paren_count--;
					}
					$this->append_code($text, false);
					if (0 === $paren_count && !$this->is_token(array(T_COMMENT, T_DOC_COMMENT))) {
						break;
					}
				}
				if (!$this->is_token(ST_CURLY_OPEN) && !$this->is_token(ST_COLON)) {
					$ignore_count = 0;
					if (!$this->is_token(array(T_COMMENT, T_DOC_COMMENT), true)) {
						$this->append_code($this->new_line . '{');
					} else {
						$this->append_code('{');
					}
					while (list($index, $token) = each($this->tkns)) {
						list($id, $text) = $this->get_token($token);
						$this->ptr = $index;
						if (ST_PARENTHESES_OPEN == $id || ST_CURLY_OPEN == $id || ST_BRACKET_OPEN == $id) {
							$ignore_count++;
						} elseif (ST_PARENTHESES_CLOSE == $id || ST_CURLY_CLOSE == $id || ST_BRACKET_CLOSE == $id) {
							$ignore_count--;
						}
						$this->append_code($text, false);
						if ($ignore_count <= 0 && !($this->is_token(ST_CURLY_CLOSE) || $this->is_token(ST_SEMI_COLON) || $this->is_token(array(T_WHILE))) && (ST_CURLY_CLOSE == $id || ST_SEMI_COLON == $id || T_ELSE == $id || T_ELSEIF == $id)) {
							break;
						}
					}
					$this->append_code($this->get_crlf_indent() . '}' . $this->get_crlf_indent(), false);
					break 2;
				}
				break;
			case T_IF:
			case T_ELSEIF:
				$this->append_code($text, false);
				$paren_count = null;
				while (list($index, $token) = each($this->tkns)) {
					list($id, $text) = $this->get_token($token);
					$this->ptr = $index;
					if (ST_PARENTHESES_OPEN == $id) {
						$paren_count++;
					} elseif (ST_PARENTHESES_CLOSE == $id) {
						$paren_count--;
					}
					$this->append_code($text, false);
					if (0 === $paren_count && !$this->is_token(array(T_COMMENT, T_DOC_COMMENT))) {
						break;
					}
				}
				if (!$this->is_token(ST_CURLY_OPEN) && !$this->is_token(ST_COLON)) {
					$ignore_count = 0;
					if (!$this->is_token(array(T_COMMENT, T_DOC_COMMENT), true)) {
						// $this->append_code($this->new_line.'{'.$this->new_line);
						$this->append_code($this->new_line . '{');
					} else {
						// $this->append_code('{'.$this->new_line);
						$this->append_code('{');
					}
					while (list($index, $token) = each($this->tkns)) {
						list($id, $text) = $this->get_token($token);
						$this->ptr = $index;
						if (ST_PARENTHESES_OPEN == $id || ST_CURLY_OPEN == $id || ST_BRACKET_OPEN == $id) {
							$ignore_count++;
						} elseif (ST_PARENTHESES_CLOSE == $id || ST_CURLY_CLOSE == $id || ST_BRACKET_CLOSE == $id) {
							$ignore_count--;
						}
						$this->append_code($text, false);
						if ($ignore_count <= 0 && !($this->is_token(ST_CURLY_CLOSE) || $this->is_token(ST_SEMI_COLON) || $this->is_token(array(T_WHILE))) && (ST_CURLY_CLOSE == $id || ST_SEMI_COLON == $id || T_ELSE == $id || T_ELSEIF == $id)) {
							break;
						}
					}
					$this->append_code($this->get_crlf_indent() . '}' . $this->get_crlf_indent(), false);
					break 2;
				}
				break;
			case T_ELSE:
				$this->append_code($text, false);
				if (!$this->is_token(ST_CURLY_OPEN) && !$this->is_token(ST_COLON) && !$this->is_token(array(T_IF))) {
					$ignore_count = 0;
					$this->append_code('{' . $this->new_line);
					while (list($index, $token) = each($this->tkns)) {
						list($id, $text) = $this->get_token($token);
						$this->ptr = $index;
						if (ST_PARENTHESES_OPEN == $id || ST_CURLY_OPEN == $id || ST_BRACKET_OPEN == $id) {
							$ignore_count++;
						} elseif (ST_PARENTHESES_CLOSE == $id || ST_CURLY_CLOSE == $id || ST_BRACKET_CLOSE == $id) {
							$ignore_count--;
						}
						$this->append_code($text, false);
						if ($ignore_count <= 0 && !($this->is_token(ST_CURLY_CLOSE) || $this->is_token(ST_SEMI_COLON) || $this->is_token(array(T_WHILE))) && (ST_CURLY_CLOSE == $id || ST_SEMI_COLON == $id || T_ELSE == $id || T_ELSEIF == $id)) {
							break;
						}
					}
					$this->append_code($this->get_crlf_indent() . '}' . $this->get_crlf_indent(), false);
					break 2;
				}
				break;
			default:
				$this->append_code($text, false);
				break;
			}
		}
		while (list($index, $token) = each($this->tkns)) {
			list($id, $text) = $this->get_token($token);
			$this->ptr = $index;
			$this->append_code($text, false);
		}

		return $this->code;
	}
}

Je ne sais pas par quoi remplacer cette fonction. C'est un peu génant, car il me décale toutes mes indentations.
Merci de votre aide à tous.
Cordialement

3 réponses

jordane45 Messages postés 38144 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 21 avril 2024 4 650
10 févr. 2018 à 16:36
Bonjour,

while (list($index, $token) = each($this->tkns)) {

A mon avis... se remplace sans mal par :
foreach($this->tkns as $index=>$token){
3