Float ranges: exclusive bounds in PHPDoc, rounded bound literals and readable descriptions

Aberta
#15,762 1 comentário 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
5/5
Tempo estimado
Mais de uma semana
Facilidade para iniciantes
35/100
Tipo de issue
Funcionalidade
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
php

Direção de pesquisa

Start by reading the float range handling and PHPDoc parsing code, then inspect tests for float types and PHPDoc bounds. The issue leaves the exclusive-bound notation and several behavior choices open, so first agree on scope and notation with maintainers. Done means the selected behavior is covered by tests for parsing, displayed descriptions, and any new float range names.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

Feature request

Float ranges keep both bounds inclusive and express a strict comparison by moving the bound to the adjacent double. In the type system this is exact: for every double $x, $x > 1.5 holds exactly when $x >= 1.5000000000000002. The PHPDoc side has no counterpart for it, though, and that shows in three places: writing such a type, reading it in messages, and a typo that silently flips its meaning. I'd like to settle how PHPDoc should handle exclusive bounds and to fix the smaller parts that do not depend on it.

Current behaviour (2.3.6)
Code Type
if ($n > 1.5) float<1.5000000000000002, max>
if ($n >= 1.5) float<1.5, max>
if ($n > 0.1 + 0.2) float<0.3000000000000001, max>
if ($n > 0) float<5.0E-324, max>
@param positive-float float<5.0E-324, max>
@param negative-float float<min, -5.0E-324>
@param non-positive-float float<min, 0.0>
@param non-negative-float float<0.0, max>
@param finite-float float<-1.7976931348623157E+308, 1.7976931348623157E+308>
@param non-zero-float unresolvable
@param float<1.5000000000000001, max> float<1.5, max>
Problems
  • An exclusive bound has to be written as the adjacent double. "Greater than 1.5" is float<1.5000000000000002, max>, which works and matches the narrowed type, but the writer has to find that value first.
  • A bound literal close to the intended one can silently flip the meaning. PHP rounds 1.5000000000000001 to 1.5 (https://3v4l.org/c6HrD), so float<1.5000000000000001, max> includes 1.5, the opposite of what a writer aiming at "greater than 1.5" meant. Nothing is reported.
  • Messages show the adjacent double. For example expects float<1.5000000000000002, max>, float<1.5, max> given. $n > 0 is shown as float<5.0E-324, max> although it is exactly positive-float, and the named float types are never shown by name. For integers, int<1, max> instead of positive-int is still readable; for floats the bound turns into 5.0E-324.
  • Some names are missing. There is no non-zero-float, unlike non-zero-int. positive-float and non-negative-float include INF, and a finite positive float can only be written as float<5.0E-324, 1.7976931348623157E+308>.
Options
  • A. Syntax for exclusive bounds in PHPDoc. A way to mark a bound of float<…> as exclusive. This needs a phpdoc-parser change and a decision on the notation, so it is the largest of the options.
  • B. Describe exclusive bounds and named ranges readably. Print the four sign ranges as positive-float, negative-float, non-positive-float and non-negative-float. A bound that is the adjacent double of a shorter decimal could also be printed as excluding that decimal, but without option A that output could not be written back into PHPDoc.
  • C. Report a bound literal that rounds to a different, shorter float. For example, report 1.5000000000000001 as the same float as 1.5. A literal like 0.1, whose shortest representation is the written text, is not reported, so the rule should be quiet on ordinary bounds.
  • D. Add missing names. non-zero-float, and possibly finite variants of the sign types.
Open points
  • The notation for option A, or whether exclusive bounds stay expressible only through the adjacent double and the named types. The named types already cover the most common exclusive bounds, those at zero.
  • How far option B goes. Printing the names of the sign ranges is independent of option A. Printing other exclusive bounds would produce types that cannot be written back.
  • Whether option C is a PHPDoc rule of its own (it is independent of A and B), and at which level.
  • Which names option D adds, given that INF is part of the unbounded sign types.

C and the naming part of B do not depend on A, so I can send those as PRs first if that fits.

Linguagem predominante
PHP
Estrelas
14.2k
Forks
972
Merge médio
13h 25min
PRs com merge (30d)
142

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de phpstan/phpstan

Todas as issues de phpstan/phpstan

Issues semelhantes

Mais issues de PHP

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.