arnapou/php72fixcount

此包已被放弃且不再维护。未建议替代包。

Arnapou PHP 7.2 修正计数器

v2.1 2022-01-30 17:53 UTC

This package is auto-updated.

Last update: 2024-09-05 17:53:37 UTC


README

在迁移到 PHP 7.2 时,与 count() 函数存在很大的不兼容性。

新的 count() 函数仅在可计数的变量(array 或实现了 Countable 的对象)上工作。

在过去,这很奇怪,但你仍然可以计数整数、布尔值等...

先前 PHP 7.2 对不可计数的参数的行为如下:

  • count(<string>) = 1
  • count(<integer>) = 1
  • count(<boolean>) = 1
  • count(<float>) = 1
  • count(<object>) = 1
  • count(null) = 0

何时使用它

  • 需要快速迁移到 Php 7.2 的旧代码(你没有几天或更多的时间去修复所有代码,同时冒着出现错误的风险)
  • 多年未维护的旧依赖项,你的项目仍然需要,但你不想/没有时间将其分叉以修复 count()
  • 你承认这个工具不会涵盖100%的使用场景(参见下文),你可能会手动做一些修复

现实中

  • 我在一个包含数千次 count 调用的 100 万行旧项目上使用了这个“修复”,没有出现任何问题。加上供应商,代码量增长到 2.5 万行,超过 13k 个文件。分析平均每秒处理 500+ 个文件和 100k+ 行。
  • 后来我花了超过一周的时间手动迁移项目中的每个 "count",但我保留了修复器,因为项目中有多年未维护的依赖项。
  • 实际上,没有人想在整数、字符串或其他东西上使用 count,但来自各种来源(如数据库)的数组有时会产生一个 null,并触发 E_WARNING。这比我们想要的更常见。

“修复” count 的“技巧”

  • 声明一个 count() 函数,在每个修复器检测到使用的命名空间中恢复旧的行为

无法修复的用例

  • 你到处使用反斜杠调用 count()\count()
  • 你在评估字符串(eval() 恶魔)中有 count() 调用
  • 你已经在命名空间中覆盖了 count()
  • 在非命名空间类中的 count() 调用
  • 其他我想象不到的事情...

如何使用它

在你的 composer.json

"require": {
    "arnapou/php72fixcount": "^1.0"
},
"scripts": {
    "post-autoload-dump": [
        "@php vendor/bin/php72-fix-count.php --quiet generate src vendor"
    ]
}

php72-fix-count.php 的用法

PHP 7.2 FIX COUNT

DESCRIPTION
    This command generate php files which are loaded by composer
    in order to fix/hack the breaking change of the count/sizeof
    breaking change for php 7.2+

SYSNOPSIS
    php php72-fix-count.php [OPTION] COMMAND DIRECTORY...

OPTION
    --quiet    silent mode (usefull for composer post-autoload-dump)

COMMAND
    generate   generate the fixes
    search     search the fixes (same as generate but no write)
    clean      remove the fixes

EXAMPLES
    php php72-fix-count.php --quiet generate src vendor
    php php72-fix-count.php clean

你必须将所有目录添加到相同的命令行中,因为每次脚本执行都会覆盖生成的修复,不要 这样做

"scripts": {
    "post-autoload-dump": [
        "@php vendor/bin/php72-fix-count.php --quiet generate src ",             |  DOES NOT
        "@php vendor/bin/php72-fix-count.php --quiet generate vendor"            |  WORK !!!
    ]
}

php.net 的附录

这是从 这个链接 复制粘贴的

当尝试对不可计数的类型使用 count() 时(这包括 sizeof() 别名函数),现在将发出一个 E_WARNING

<?php

var_dump(
    count(null), // NULL is not countable
    count(1), // integers are not countable
    count('abc'), // strings are not countable
    count(new stdclass), // objects not implementing the Countable interface are not countable
    count([1,2]) // arrays are countable
);

上面的示例将输出

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
int(0)
int(1)
int(1)
int(1)
int(2)

PHP 支持

更新标签,分支Php
30/01/20222.x, master5.6 - 8.1
11/03/20211.x5.6 - 7.4