php / php/php-src

declare statement is not scoped when using curly braces

Abierto
#9,446 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Category: Engine Status: Needs Triage
Lenguaje dominante
C
Estrellas
40.4k
Forks
8.2k
Merge medio
2 d 13 h
PR fusionados (30 d)
96

Descripción

Description

When writing tests for phpcs sniff using some useful helpers regarding declare statements, I came up with an unusual behavior of the declare statements, when braces are used.

I was testing out a code that looked like this:

<?php
declare(strict_types=1)
echo 'hi!';

{
// Code.
}

that threw a fatal error because you cannot use curly braces with strict_types directive or alternative control structure syntax (declare: ... enddeclare;)

So I tried to see what will happen if I use a directive that allows the use of curly braces, like encoding. So I tried:

<?php

declare(encoding='ISO-8859-15')
echo 'aäaß' . "\n";

{
echo 'aäaß' . "\n";

}

And I got out:

aÀaÃ
aÀaÃ

I thought I'd get a parse error like when you omit the : when using if with endif; statement. Although, when testing the if statement with the curly braces, like in the example above, I can also see the echoed value before the condition, so I guess that's kinda expected.

What was a buggy behavior was this:

<?php

declare(encoding='ISO-8859-15') {
    echo 'aäaß' . "\n";
}

echo 'aäaß' . "\n";

I expected to see

aÀaÃ
aäaß

But I got (again)

aÀaÃ
aÀaÃ

So it seems that the declare statement, no matter if we are using curly braces, or alternative control structure syntax, will be applied throughout the file, instead of being scoped.

PHP Version

PHP 7.4.26

Operating System

Ubuntu 20.04 (on WSL)

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

El issue no indica ningún archivo fuente ni punto de entrada de pruebas. Empieza reproduciendo los ejemplos de PHP 7.4.26 que involucran declare(encoding=...) y llaves, y luego sigue el manejo de declare en el intérprete de PHP. Se considera terminado cuando la directiva está limitada a la sentencia entre llaves y el comportamiento observado está cubierto por una prueba de regresión.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
php
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.