php / php/php-src

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

Abierto
#20,632 4 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Feature Status: Verified
Lenguaje dominante
C
Estrellas
40.4k
Forks
8.1k
Merge medio
2 d 13 h
PR fusionados (30 d)
96

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

El issue no identifica archivos ni tests; empieza rastreando en php-src las rutas de coerción numérica y de errores de tipo de argumentos en llamadas coercitivas, y después localiza su cobertura existente. Se considera terminado cuando los errores distinguen las cadenas vacías de otras cadenas no numéricas tanto en los operadores matemáticos como en las llamadas de funciones coercitivas.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
php
Área
backend
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.