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

未关闭
#15,762 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
php

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
PHP
星标
14.2k
派生
972
平均合并
13 小时 25 分钟
30 天内合并 PR
142

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

phpstan/phpstan 的其他 Issue

查看 phpstan/phpstan 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。