Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement isDefaultValueConstant and fix getDefaultValueConstantName #4009

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions hphp/runtime/ext/reflection/ext_reflection-classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,36 @@ 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, false if it is not
* or NULL on failure.
*/
public function isDefaultValueConstant() {
if (array_key_exists('default', $this->info)) {
$defaultText = $this->info['defaultText'];

$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 NULL;
}

// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from
Expand Down Expand Up @@ -399,11 +429,11 @@ 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
Expand Down
26 changes: 26 additions & 0 deletions hphp/test/slow/reflection/is_default_value_constant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

const ONE = 1;
const TWO = 3.1415;
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 test($param) {
$r = new ReflectionParameter('params', $param);
var_dump($r->isDefaultValueConstant());
}

test('a');
test('b');
test('c');
test('d');
test('e');
test('f');
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)