From 95c86dfaeb169e5e0e07b114e4b510a8f2f4397e Mon Sep 17 00:00:00 2001 From: Lucas Tadeu Teixeira Date: Fri, 17 Oct 2014 16:00:03 -0700 Subject: [PATCH 1/8] Resolve issue #3812 The 'defaultText' field inside the 'info' array of a ReflectionParameter instance is matched against a regex that checks if it complies with the accepted format for labels and constants names. Then it checks whether or not it is one of the following reserved words: 'NULL', 'false' and 'true'. If it is, then the function returns FALSE. Otherwise, TRUE. --- .../ext/reflection/ext_reflection-classes.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/hphp/runtime/ext/reflection/ext_reflection-classes.php b/hphp/runtime/ext/reflection/ext_reflection-classes.php index 503064d5841100..7362c48cefaa1c 100644 --- a/hphp/runtime/ext/reflection/ext_reflection-classes.php +++ b/hphp/runtime/ext/reflection/ext_reflection-classes.php @@ -358,6 +358,29 @@ public function isDefaultValueAvailable() { return (!$defaultValue instanceof stdClass); } + /** + * ( excerpt from + * http://php.net/manual/en/reflectionparameter.isdefaultvalueconstant.php + * ) + * + * Returns whether the default value of this parameter is constant. + * + * @return mixed TRUE if a default value is constant, otherwise + * FALSE + */ + public function isDefaultValueConstant() { + if(array_key_exists('default', $this->info)) { + $defaultText = $this->info['defaultText']; + $pattern = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'; + + if(preg_match($pattern, $defaultText, $matches) === 1) + return !in_array($defaultText, array('NULL', 'true', 'false')); + + return false; + } + return false; + } + // This doc comment block generated by idl/sysdoc.php /** * ( excerpt from From b7a1c779223709f6e9df8bcb602db2b1338f834f Mon Sep 17 00:00:00 2001 From: Lucas Tadeu Teixeira Date: Fri, 17 Oct 2014 16:08:15 -0700 Subject: [PATCH 2/8] Add tests for the isDefaultValueConstant method from the ReflectionParameter class. --- .../reflection/is_default_value_constant.php | 22 +++++++++++++++++++ .../is_default_value_constant.php.expect | 6 +++++ 2 files changed, 28 insertions(+) create mode 100644 hphp/test/slow/reflection/is_default_value_constant.php create mode 100644 hphp/test/slow/reflection/is_default_value_constant.php.expect diff --git a/hphp/test/slow/reflection/is_default_value_constant.php b/hphp/test/slow/reflection/is_default_value_constant.php new file mode 100644 index 00000000000000..5720ba4cf41d18 --- /dev/null +++ b/hphp/test/slow/reflection/is_default_value_constant.php @@ -0,0 +1,22 @@ +isDefaultValueConstant()); +} + +test('a'); +test('b'); +test('c'); +test('d'); +test('e'); +test('f'); diff --git a/hphp/test/slow/reflection/is_default_value_constant.php.expect b/hphp/test/slow/reflection/is_default_value_constant.php.expect new file mode 100644 index 00000000000000..f09e09cbaf9c5d --- /dev/null +++ b/hphp/test/slow/reflection/is_default_value_constant.php.expect @@ -0,0 +1,6 @@ +bool(false) +bool(true) +bool(true) +bool(true) +bool(true) +bool(false) From 46c1d30aa45891bd59be288f037ff9a8ebc79ab5 Mon Sep 17 00:00:00 2001 From: Lucas Tadeu Teixeira Date: Fri, 17 Oct 2014 16:11:46 -0700 Subject: [PATCH 3/8] Resolve issue #3828. The method getDefaultValueConstantName returns NULL in case the value is NOT a constant; otherwise, it returns the constant's name. To check if the value is indeed a constant, we use the method isDefaultValueConstant method. --- hphp/runtime/ext/reflection/ext_reflection-classes.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hphp/runtime/ext/reflection/ext_reflection-classes.php b/hphp/runtime/ext/reflection/ext_reflection-classes.php index 7362c48cefaa1c..015611a551a613 100644 --- a/hphp/runtime/ext/reflection/ext_reflection-classes.php +++ b/hphp/runtime/ext/reflection/ext_reflection-classes.php @@ -422,11 +422,10 @@ public function getDefaultValueText() { * @return mixed Returns string on success or NULL on failure. */ public function getDefaultValueConstantName() { - if (array_key_exists('defaultText', $this->info)) { + if ($this->isDefaultValueConstant()) return $this->info['defaultText']; - } - return ''; + return NULL; } // This doc comment block generated by idl/sysdoc.php From 375be4f0865acea895d2218b3e22dd801fcf4b80 Mon Sep 17 00:00:00 2001 From: Lucas Tadeu Teixeira Date: Fri, 17 Oct 2014 18:58:53 -0700 Subject: [PATCH 4/8] Fix indentation problems. --- .../test/slow/reflection/is_default_value_constant.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/hphp/test/slow/reflection/is_default_value_constant.php b/hphp/test/slow/reflection/is_default_value_constant.php index 5720ba4cf41d18..6e63c1d9ba913f 100644 --- a/hphp/test/slow/reflection/is_default_value_constant.php +++ b/hphp/test/slow/reflection/is_default_value_constant.php @@ -5,9 +5,13 @@ const THREE = "this is a constant string"; const FOUR = true; -function params($a = "string param", $b = ONE, $c=TWO, - $d=THREE, $e=FOUR, $f=[1,2,3]) { - } +function params($a = "string param", + $b = ONE, + $c=TWO, + $d=THREE, + $e=FOUR, + $f=[1,2,3]) { +} function test($param) { $r = new ReflectionParameter('params', $param); From 8c66d3c61a0caa15fe231bde8fb400f2e69ef8d0 Mon Sep 17 00:00:00 2001 From: Lucas Tadeu Teixeira Date: Fri, 17 Oct 2014 18:59:43 -0700 Subject: [PATCH 5/8] Add spaces after ifs and brackets. Correct the doc on return values to lowercase booleans for the isDefaultValueConstant method. Add return of NULL (to the method cited above) when the given argument does not have a default value. --- .../ext/reflection/ext_reflection-classes.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/hphp/runtime/ext/reflection/ext_reflection-classes.php b/hphp/runtime/ext/reflection/ext_reflection-classes.php index 015611a551a613..ebe58ffaf9fd37 100644 --- a/hphp/runtime/ext/reflection/ext_reflection-classes.php +++ b/hphp/runtime/ext/reflection/ext_reflection-classes.php @@ -365,20 +365,21 @@ public function isDefaultValueAvailable() { * * Returns whether the default value of this parameter is constant. * - * @return mixed TRUE if a default value is constant, otherwise - * FALSE + * @return mixed true if a default value is constant, false if it is not + * or NULL on failure. */ public function isDefaultValueConstant() { - if(array_key_exists('default', $this->info)) { + if (array_key_exists('default', $this->info)) { $defaultText = $this->info['defaultText']; $pattern = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'; - if(preg_match($pattern, $defaultText, $matches) === 1) + if (preg_match($pattern, $defaultText, $matches) === 1) { return !in_array($defaultText, array('NULL', 'true', 'false')); + } return false; } - return false; + return NULL; } // This doc comment block generated by idl/sysdoc.php @@ -422,8 +423,9 @@ public function getDefaultValueText() { * @return mixed Returns string on success or NULL on failure. */ public function getDefaultValueConstantName() { - if ($this->isDefaultValueConstant()) + if ($this->isDefaultValueConstant()) { return $this->info['defaultText']; + } return NULL; } From 4e10a00bf58bef3e084bbb23b8f100da0648898a Mon Sep 17 00:00:00 2001 From: Lucas Tadeu Teixeira Date: Thu, 6 Nov 2014 06:12:16 -0800 Subject: [PATCH 6/8] Fix regex for checking if a parameter is a constant or not. --- .../ext/reflection/ext_reflection-classes.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/hphp/runtime/ext/reflection/ext_reflection-classes.php b/hphp/runtime/ext/reflection/ext_reflection-classes.php index ebe58ffaf9fd37..b3947da2e24155 100644 --- a/hphp/runtime/ext/reflection/ext_reflection-classes.php +++ b/hphp/runtime/ext/reflection/ext_reflection-classes.php @@ -371,13 +371,19 @@ public function isDefaultValueAvailable() { public function isDefaultValueConstant() { if (array_key_exists('default', $this->info)) { $defaultText = $this->info['defaultText']; - $pattern = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'; - if (preg_match($pattern, $defaultText, $matches) === 1) { - return !in_array($defaultText, array('NULL', 'true', 'false')); - } + $quoted_pattern = '/^(["\']).*\1$/'; + $braketed_pattern = '/^\[.*\]$/'; + + if(is_numeric($defaultText) || + in_array($defaultText, array('NULL', 'true', 'false') || + preg_match($quoted_pattern, $defaultText) || + preg_match($braketed_pattern, $defaultText) || + substr($defaultText, 0, 5) === 'array')) + return false; + + return true; - return false; } return NULL; } From d110d55c76dd521b626c181753efe7c5fd54b388 Mon Sep 17 00:00:00 2001 From: Lucas Tadeu Teixeira Date: Thu, 6 Nov 2014 06:15:28 -0800 Subject: [PATCH 7/8] Add missing enclosing parenthesis. --- hphp/runtime/ext/reflection/ext_reflection-classes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hphp/runtime/ext/reflection/ext_reflection-classes.php b/hphp/runtime/ext/reflection/ext_reflection-classes.php index b3947da2e24155..b5146740be93b0 100644 --- a/hphp/runtime/ext/reflection/ext_reflection-classes.php +++ b/hphp/runtime/ext/reflection/ext_reflection-classes.php @@ -376,7 +376,7 @@ public function isDefaultValueConstant() { $braketed_pattern = '/^\[.*\]$/'; if(is_numeric($defaultText) || - in_array($defaultText, array('NULL', 'true', 'false') || + in_array($defaultText, array('NULL', 'true', 'false')) || preg_match($quoted_pattern, $defaultText) || preg_match($braketed_pattern, $defaultText) || substr($defaultText, 0, 5) === 'array')) From a9f744736225d99ffdaa0805167203a864cd853d Mon Sep 17 00:00:00 2001 From: Lucas Tadeu Teixeira Date: Thu, 6 Nov 2014 06:19:15 -0800 Subject: [PATCH 8/8] Remove extra enclosing parenthesis. --- hphp/runtime/ext/reflection/ext_reflection-classes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hphp/runtime/ext/reflection/ext_reflection-classes.php b/hphp/runtime/ext/reflection/ext_reflection-classes.php index b5146740be93b0..72da4fb5263579 100644 --- a/hphp/runtime/ext/reflection/ext_reflection-classes.php +++ b/hphp/runtime/ext/reflection/ext_reflection-classes.php @@ -379,7 +379,7 @@ public function isDefaultValueConstant() { in_array($defaultText, array('NULL', 'true', 'false')) || preg_match($quoted_pattern, $defaultText) || preg_match($braketed_pattern, $defaultText) || - substr($defaultText, 0, 5) === 'array')) + substr($defaultText, 0, 5) === 'array') return false; return true;