Use better error message for non-numeric strings in implicit contexts
Nessuno ha ancora preso questa issue.
- Lingua principale
- C
- Stelle
- 40.4k
- Fork
- 8.1k
- Merge medio
- 2g 13h
- PR unite (30g)
- 96
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
La issue non identifica file o test; inizia tracciando in php-src i percorsi della coercizione numerica e degli errori di tipo degli argomenti nelle chiamate coercitive, quindi individua la relativa copertura esistente. Il lavoro è completato quando gli errori distinguono le stringhe vuote dalle altre stringhe non numeriche sia negli operatori matematici sia nelle chiamate di funzioni coercitive.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- php
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100