php / php/php-src

Use better error message for non-numeric strings in implicit contexts

Offen
#20,632 4 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Feature Status: Verified
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.1k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

Description

When a "non-numeric" string is coerced to an integer or float, PHP raises an error rather than proceeding with the implicit cast. This is a good thing.

However, the error message implies that any string would be invalid, rather than that the content of the string is the problem.

Additionally, a common cause in my experience is that the string is empty, for instance, missing from some input data. It would be helpful for debugging to highlight when this is the case.

Proposal

When coercion to a numeric, float, or int context fails:

  • If the string being coerced is empty, use "empty string" as a pseudo-type in the error message
  • Otherwise, use "non-numeric string" as a pseudo-type in the error message

Current Behaviour

Mathematical operators:

echo 1 + "1";
// 2
echo 1 + "hello";
// TypeError: Unsupported operand types: int + string
echo 1 + "";
// TypeError: Unsupported operand types: int + string

Function calls in coercive-call mode:

declare(strict_types=0);

function foo(int $a) { echo $a; }

foo("1");
// 1
foo("a");
// TypeError: foo(): Argument #1 ($a) must be of type int, string given
foo("");
// TypeError: foo(): Argument #1 ($a) must be of type int, string given

Proposed behaviour

Mathematical operators:

echo 1 + "1";
// 2
echo 1 + "hello";
// TypeError: Unsupported operand types: int + non-numeric string
echo 1 + "";
// TypeError: Unsupported operand types: int + empty string

Function calls in coercive-call mode:

declare(strict_types=0);

function foo(int $a) { echo $a; }

foo("1");
// 1
foo("a");
// TypeError: foo(): Argument #1 ($a) must be of type int, non-numeric string given
foo("");
// TypeError: foo(): Argument #1 ($a) must be of type int, empty string given

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 identifiziert keine Dateien oder Tests; beginne damit, in php-src die Pfade für die numerische Koerzierung und für Typfehler bei Argumenten koerziver Aufrufe nachzuverfolgen, und ermittle dann deren bestehende Abdeckung. Als abgeschlossen gilt die Arbeit, wenn Fehler in mathematischen Operatoren und koerziven Funktionsaufrufen leere Zeichenketten von anderen nicht numerischen Zeichenketten unterscheiden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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