Use better error message for non-numeric strings in implicit contexts
Personne n'a encore pris cette issue.
- Langage dominant
- C
- Étoiles
- 40.4k
- Forks
- 8.2k
- Merge moyen
- 2 j 13 h
- PR mergées (30 j)
- 96
Description
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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
L’issue n’identifie ni fichiers ni tests ; commencez par suivre dans php-src les chemins de coercition numérique et d’erreur de type des arguments des appels coercitifs, puis localisez leur couverture existante. Le travail est considéré comme terminé lorsque les erreurs distinguent les chaînes vides des autres chaînes non numériques, à la fois dans les opérateurs mathématiques et dans les appels de fonctions coercitifs.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- php
- Domaine
- backend
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 45/100