samsonasik / is-deprecated
检查 PHP 用户和核心函数是否已弃用
3.0.0
2020-01-25 12:21 UTC
Requires
- php: ^7.0
- jeremeamia/functionparser: ^1.0
- laminas/laminas-stdlib: ^3.1
Requires (Dev)
- kahlan/kahlan: ^4.5
- php-coveralls/php-coveralls: ^2.0
README
介绍
IsDeprecated 是 PHP7 辅助工具,可以帮助您检测您的函数是否已弃用,并使用 E_USER_DEPRECATED 和 E_DEPRECATED 级别。
特性
- 独立函数检测(E_USER_DEPRECATED)
- 类内函数检测(E_USER_DEPRECATED)
- 核心 PHP 函数检测(E_DEPRECATED)
安装
需要使用 composer
composer require samsonasik/is-deprecated
用法
有两个函数
1. 用户定义函数
/** * @param string|array $function the "functionName" or ["ClassName" or object, "functionName"] or "ClassName::functionName" * @throws InvalidArgumentException when trigger_error found but the error is not E_USER_DEPRECATED * @throws InvalidArgumentException when trigger_error and E_USER_DEPRECATED found but misplaced * @return bool */ function isDeprecatedUser($function): bool
注意:当在条件中触发错误 E_USER_DEPRECATED 时,您需要使用
actual
调用和签名
/** * @param callable $function callable function * @return bool */ function isDeprecatedWithActualCall(callable $function)
2. 核心 PHP 函数
/** * @param callable $function callable function * @return bool */ function isDeprecatedCore(callable $function): bool
独立函数示例
include 'vendor/autoload.php'; // autoload may already handled by your framework use function IsDeprecated\isDeprecatedUser; use function IsDeprecated\isDeprecatedWithActualCall; function foo() { trigger_error('this method has been deprecated.', E_USER_DEPRECATED); echo 'foo' . PHP_EOL; } function foonotdeprecated() { echo 'foo' . PHP_EOL; } function fooDeprecatedWithCondition() { if (1 === 1) { trigger_error('this method has been deprecated.', E_USER_DEPRECATED); echo 'foo' . PHP_EOL; } } // deprecated var_dump(isDeprecatedUser('foo')); // true OR for deprecate with condition var_dump(isDeprecatedWithActualCall(function () { fooDeprecatedWithCondition(); })); // true // not deprecated var_dump(isDeprecatedUser('foonotdeprecated')); // false // Usage Example: if (isDeprecatedUser('foo')) { foonotdeprecated();; } else { foo(); }
类内函数示例
include 'vendor/autoload.php'; // autoload may already handled by your framework use function IsDeprecated\isDeprecatedUser; use function IsDeprecated\isDeprecatedWithActualCall; class Aclass { function foo() { trigger_error('this method has been deprecated.', E_USER_DEPRECATED); echo 'foo' . PHP_EOL; } function foodeprecatedWithCondition() { if (1 === 1) { trigger_error('this method has been deprecated.', E_USER_DEPRECATED); } echo 'foo' . PHP_EOL; } function foonotdeprecated() { echo 'foo' . PHP_EOL; } // you may call inside the class // with $this as first index of array parameter function execute() { if (isDeprecatedUser([$this, 'foo'])) { $this->foonotdeprecated(); return; } $this->foo(); } } // deprecated var_dump(isDeprecatedUser(['Aclass', 'foo'])); // true OR var_dump(isDeprecatedUser([new \Aclass(), 'foo'])); // true OR var_dump(isDeprecatedUser('Aclass::foo')); // true OR for deprecate with condition var_dump(isDeprecatedWithActualCall(function () { // true new \Aclass()->foo(); })); // not deprecated var_dump(isDeprecatedUser(['Aclass', 'foonotdeprecated'])); // false OR var_dump(isDeprecatedUser([new \Aclass, 'foonotdeprecated'])); // false OR var_dump(isDeprecatedUser('Aclass::foonotdeprecated')); // false // Usage Example: if (isDeprecatedUser(['Aclass', 'foo'])) { (new \Aclass())->foonotdeprecated();; } else { (new \Aclass())->foo();; }
核心 PHP 函数示例
include 'vendor/autoload.php'; // autoload may already handled by your framework use function IsDeprecated\isDeprecatedCore; $function = function () { mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); }; //on php 7.1 var_dump(isDeprecatedCore($function)); // true //on php 7.0 var_dump(isDeprecatedCore($function)); // false // Usage Example: if (isDeprecatedCore($function)) { // alternative function, eg: openssl ... } else { $function(); }
限制
对于核心 PHP 函数或带有条件(T_IF 或 T_SWITCH 令牌)的用户函数,实际传递的函数确实需要被调用。这确保我们在调用已弃用的函数时不会出错,并且当使用 isDeprecatedCore()
返回 true 并使用 isDeprecatedWithActualCall
调用时,我们可以使用替代函数。
贡献
非常欢迎贡献。请阅读 CONTRIBUTING.md