From 6b86cf8828d73af9dad6f3c8bfe9f3dc7c269fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 21 Feb 2018 00:22:08 +0000 Subject: [PATCH] Optimize native function and constant calls --- README.md | 36 +++++++++++ composer.lock | 10 ++-- scoper.inc.php | 13 +--- specs/class-FQ.php | 20 +++---- ...-level-with-single-level-use-and-alias.php | 2 +- ...-scope-two-level-with-single-level-use.php | 4 +- specs/class-const/global-scope-two-level.php | 4 +- ...-level-with-single-level-use-and-alias.php | 2 +- ...-scope-two-level-with-single-level-use.php | 4 +- .../class-const/namespace-scope-two-level.php | 4 +- specs/class/abstract.php | 6 +- specs/class/conditional.php | 10 ++-- specs/class/final.php | 2 +- specs/class/interface.php | 2 +- specs/class/regular-extend.php | 2 +- specs/class/regular.php | 6 +- specs/const/native-const.php | 59 +++++++++++++++++++ specs/func-declaration/global.php | 6 +- specs/func-declaration/method.php | 4 +- specs/func-declaration/namespace.php | 6 +- specs/function/native-func.php | 59 +++++++++++++++++++ ...-parts-with-single-level-use-and-alias.php | 4 +- ...-scope-two-parts-with-single-level-use.php | 8 +-- specs/new/global-scope-two-parts.php | 4 +- ...namespace-two-parts-with-two-level-use.php | 2 +- .../self-static-parent-method.php | 4 +- ...-parts-with-single-level-use-and-alias.php | 6 +- ...-scope-two-parts-with-single-level-use.php | 8 +-- .../static-method/global-scope-two-parts.php | 4 +- ...namespace-two-parts-with-two-level-use.php | 2 +- specs/static-method/namespace-two-parts.php | 2 +- specs/string-literal/func-arg.php | 2 +- specs/string-literal/var.php | 2 +- specs/use/use-class.php | 2 +- src/NodeVisitor/NameStmtPrefixer.php | 39 ++++++------ 35 files changed, 248 insertions(+), 102 deletions(-) create mode 100644 specs/const/native-const.php create mode 100644 specs/function/native-func.php diff --git a/README.md b/README.md index 821f1415..3485c169 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ potentially very difficult to debug due to dissimilar or unsupported package ver - [Limitations](#limitations) - [PSR-0 support](#psr-0-support) - [String values](#string-values) + - [Native functions and constants shadowing](#native-functions-shadowing) - [Contributing](#contributing) - [Credits](#credits) @@ -437,6 +438,41 @@ bound to have confusing cases. For example: name or a random string. +### Native functions and constants shadowing + +In the following example: + +```php +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return [ + 'meta' => [ + 'title' => 'Native constant calls', + // Default values. If not specified will be the one used + 'prefix' => 'Humbug', + 'whitelist' => [], + ], + + 'Internal function in a namespace: make the call into a FQ call' => <<<'PHP' + <<<'PHP' +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +return [ + 'meta' => [ + 'title' => 'Native function calls', + // Default values. If not specified will be the one used + 'prefix' => 'Humbug', + 'whitelist' => [], + ], + + 'Internal function in a namespace: make the call into a FQ call' => <<<'PHP' + <<<'PHP' +reflector->isConstantInternal($resolvedName->toString()) - // Constants have a fallback autoloading so we cannot prefix them when the name is ambiguous - // See https://wiki.php.net/rfc/fallback-to-root-scope-deprecation - || false === ($resolvedName instanceof FullyQualified) - ) - ) { - return $resolvedName; + // Constants have a fallback autoloading so we cannot prefix them when the name is ambiguous + // See https://wiki.php.net/rfc/fallback-to-root-scope-deprecation + if ($parentNode instanceof ConstFetch) { + if ($this->reflector->isConstantInternal($resolvedName->toString())) { + return new FullyQualified($resolvedName->toString(), $resolvedName->getAttributes()); + } + + if (false === ($resolvedName instanceof FullyQualified)) { + return $resolvedName; + } } - if ( - $parentNode instanceof FuncCall - && ( - $this->reflector->isFunctionInternal($resolvedName->toString()) - // Functions have a fallback autoloading so we cannot prefix them when the name is ambiguous - // See https://wiki.php.net/rfc/fallback-to-root-scope-deprecation - || false === ($resolvedName instanceof FullyQualified) - ) - ) { - return $resolvedName; + // Functions have a fallback autoloading so we cannot prefix them when the name is ambiguous + // See https://wiki.php.net/rfc/fallback-to-root-scope-deprecation + if ($parentNode instanceof FuncCall) { + if ($this->reflector->isFunctionInternal($resolvedName->toString())) { + return new FullyQualified($resolvedName->toString(), $resolvedName->getAttributes()); + } + + if (false === ($resolvedName instanceof FullyQualified)) { + return $resolvedName; + } } if ('self' === (string) $resolvedName && $parentNode instanceof ClassMethod) {