From 96c4eeec98477db57b2241f17c7a4a95695fce4b Mon Sep 17 00:00:00 2001 From: Brown Date: Mon, 25 Nov 2019 13:08:38 -0500 Subject: [PATCH] When calling magic method clone node info --- .../Expression/Call/MethodCallAnalyzer.php | 9 ++++ src/Psalm/spl_object_id.php | 52 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/Psalm/spl_object_id.php diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php index fd291f095..32c18d830 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/MethodCallAnalyzer.php @@ -701,6 +701,8 @@ class MethodCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\ $args = $stmt->args; + $old_node_data = null; + if (!$codebase->methods->methodExists( $method_id, $context->calling_method_id, @@ -821,6 +823,9 @@ class MethodCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\ $args ); + $old_node_data = $statements_analyzer->node_data; + $statements_analyzer->node_data = clone $statements_analyzer->node_data; + $args = [ new PhpParser\Node\Arg(new PhpParser\Node\Scalar\String_($method_name_lc)), new PhpParser\Node\Arg(new PhpParser\Node\Expr\Array_($array_values)), @@ -1406,6 +1411,10 @@ class MethodCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\ } } + if ($old_node_data) { + $statements_analyzer->node_data = $old_node_data; + } + if (!$args && $lhs_var_id) { if ($config->memoize_method_calls) { $method_var_id = $lhs_var_id . '->' . $method_name_lc . '()'; diff --git a/src/Psalm/spl_object_id.php b/src/Psalm/spl_object_id.php new file mode 100644 index 000000000..cb1dfd0d1 --- /dev/null +++ b/src/Psalm/spl_object_id.php @@ -0,0 +1,52 @@ +isUserDefined()) { + /** + * See https://github.com/runkit7/runkit_object_id for a faster native version for php <= 7.1 + * + * @param object $object + * @return int The object id + */ + function spl_object_id($object) : int + { + return runkit_object_id($object); + } + } elseif (PHP_INT_SIZE === 8) { + /** + * See https://github.com/runkit7/runkit_object_id for a faster native version for php <= 7.1 + * + * @param object $object + * @return int (The object id, XORed with a random number) + */ + function spl_object_id($object) : int + { + $hash = spl_object_hash($object); + // Fit this into a php long (32-bit or 64-bit signed int). + // The first 16 hex digits (64 bytes) vary, the last 16 don't. + // Values are usually padded with 0s at the front. + return intval(substr($hash, 1, 15), 16); + } + } else { + /** + * See https://github.com/runkit7/runkit_object_id for a faster native version for php <= 7.1 + * + * @param object $object + * @return int (The object id, XORed with a random number) + */ + function spl_object_id($object) : int + { + $hash = spl_object_hash($object); + // Fit this into a php long (32-bit or 64-bit signed int). + // The first 16 hex digits (64 bytes) vary, the last 16 don't. + // Values are usually padded with 0s at the front. + return intval(substr($hash, 9, 7), 16); + } + } +}