php / php/php-src

declare statement is not scoped when using curly braces

Offen
#9,446 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Category: Engine Status: Needs Triage
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.2k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

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)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Das Issue nennt weder eine Quelldatei noch einen Test-Einstiegspunkt. Beginne damit, die PHP-7.4.26-Beispiele mit declare(encoding=...) und geschweiften Klammern zu reproduzieren, und verfolge anschließend die Behandlung von declare im PHP-Interpreter. Als erledigt gilt die Aufgabe, wenn die Direktive auf die Anweisung in den geschweiften Klammern beschränkt ist und das beobachtete Verhalten durch einen Regressionstest abgedeckt wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
php
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.