php / php/php-src

declare statement is not scoped when using curly braces

Aperta
#9,446 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Category: Engine Status: Needs Triage
Lingua principale
C
Stelle
40.4k
Fork
8.2k
Merge medio
2g 13h
PR unite (30g)
96

Descrizione

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)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

La issue non indica alcun file sorgente né alcun punto di ingresso dei test. Inizia riproducendo gli esempi di PHP 7.4.26 che coinvolgono declare(encoding=...) e le parentesi graffe, quindi segui la gestione di declare nell’interprete PHP. Il lavoro è completato quando la direttiva è limitata all’istruzione tra parentesi graffe e il comportamento osservato è coperto da un test di regressione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
php
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.