Only allow loose comparison with numeric operands
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 25/100
Línea de trabajo
No files, tests, or entry points are named. Start by locating existing PHPStan strict rules for arithmetic and loose comparisons, then determine how the proposed numeric-only checks should handle strings, objects, and the spaceship operator. Done means the rule consistently rejects the described non-numeric comparisons and has coverage for the examples.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Similar to arithmetic operators (+/-/*///**/%), loose comparison operators should only be used for numeric values. This is true for ==, !=, <, >, <=, >= and <=>.
Using any of these operators for non-numeric values may lead to unexpected results.
The option to disallow == and != completely doesn't cover the full problem as other comparison operators are still allowed and might give unexpected result. It also disallows cases where you do want to use == because you're comparing numbers.
Good
42 == 42.0 // true
42 == "42" // true
42 == "4.2e1" // true
42 < "0xf1" // true
Bad
// convert to int, I guess
1000 >= "42 bytes" // true
(int)"42 bytes" // 42
// but not always :-(
(int)"4.2e1 bytes" // 42
1000 >= "4.2e1 bytes" // false ??
Universal rules of logic state that if a > b and b > c than a > c
$a = '42';
$b = 10;
$c = '9 dwarfs';
$a > $b // true
$b > $c // true
$a > $c // false ??
The spaceship operator should also not be used for strings.
function cmp1(string $x, string $y) {
return $x <=> $y;
}
function cmp2(string $x, string $y) {
return ($x . 'Foo') <=> ($y . 'Foo');
}
// Both functions should do the same, but...
cmp1("55", "5.5e1"); // 0
cmp2("55", "5.5e1"); // 1
The logic behind wether or not a string is converted to a number is just to complex. It's party explained in the manual, but that's not conclusive.
For strings you SHOULD always use === or strcmp.
While == could be useful to compare objects, the strange behaviour on strings makes this too dangerous.
$one = (object)['a' => 0];
$two = (object)['a' => '0.0'];
$three = (object)['a' => 'bar'];
$one == $two; // true
$one == $three; // true
$two == $three; // false
- Lenguaje dominante
- PHP
- Estrellas
- 711
- Forks
- 62
- Merge medio
- 35 min
- PR fusionados (30 d)
- 4
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de phpstan/phpstan-strict-rules
-
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
phpstan/phpstan-strict-rules#317 · 1 comentario ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
phpstan/phpstan-strict-rules#316 · 3 comentarios ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 52/100
phpstan/phpstan-strict-rules#298 · 2 comentarios · 1 reacción ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 45/100
phpstan/phpstan-strict-rules#289 · 1 comentario ·
-
bug
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
phpstan/phpstan-strict-rules#283 · 6 comentarios ·
Todos los issues de phpstan/phpstan-strict-rules
Issues similares
-
Awaiting Triage
Dificultad 1/5 Menos de una hora Aptitud para principiantes 92/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
WordPress/two-factor#1008 ·
Los mantenedores suelen responder en 1 día
-
sync-en
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
Los mantenedores suelen responder en 1 día
-
sync-en
Dificultad 1/5 1-3 horas Aptitud para principiantes 88/100
Los mantenedores suelen responder en 1 día
-
Перевод устарел
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 22 días