<?php function ReflectionParameterTest($test, $test2 = null) { echo $test; } $reflect = new ReflectionFunction('ReflectionParameterTest'); $params = $reflect->getParameters(); foreach ($params as $key => $value) { ReflectionParameter::export('ReflectionParameterTest', 'incorrect_parameter'); }
/** * @dataProvider getFunctionNamesAndParamKeys */ public function testExport($functionName = null, $paramKey = null) { if (strpos($functionName, '::') !== false) { $function = explode('::', $functionName); } else { $function = $functionName; } self::assertEquals(ReflectionParameter::export($function, $paramKey, true), ezcReflectionParameter::export($function, $paramKey, true)); }
/** * This method can be used to export a single function or method parameter. * * @param string|array $function Name of the parent function. * @param string|integer $parameter Name or offset of the export parameter. * @param boolean $return Should this method return the export. * * @return string|null */ public static function export($function, $parameter, $return = false) { if (is_callable($function) === false) { throw new ReflectionException(__METHOD__ . '() is not supported.'); } return parent::export($function, $parameter, $return); }
/** * Tests export. */ public function testToString() { $tests = array('declaringFunction', 'reference', 'noReference', 'class', 'noClass', 'array', 'noArray', 'nullClass', 'noNullClass', 'nullArray', 'noNullArray', 'noOptional', 'optionalNull', 'optionalTrue', 'optionalFalse', 'optionalArray', 'optionalString', 'optionalInteger', 'optionalFloat', 'optionalConstant'); foreach ($tests as $test) { $rfl = $this->getParameterReflection($test); $this->assertSame($rfl->internal->__toString(), $rfl->token->__toString()); $this->assertSame(InternalReflectionParameter::export($this->getFunctionName($test), 0, true), ReflectionParameter::export($this->getBroker(), $this->getFunctionName($test), 0, true)); // Test loading from a string $rfl = $this->getParameterReflection($test, true); $this->assertSame($rfl->internal->__toString(), $rfl->token->__toString()); } $this->assertSame(InternalReflectionParameter::export('strpos', 0, true), ReflectionParameter::export($this->getBroker(), 'strpos', 0, true)); }
/** * Exports a reflection object. * * Returns the output if TRUE is specified for $return, printing it otherwise. * This is purely a wrapper method, which calls the corresponding method of * the parent class. * @param mixed $function Function or Method * @param mixed $parameter Parameter * @param boolean $return * Whether to return (TRUE) or print (FALSE) the output * @return mixed */ public static function export($function, $parameter, $return = false) { return parent::export($function, $parameter, $return); }
/** * Tries to find out if the type of the given parameter. Will * return empty string if not possible. * * @example * <code> * <?php * $reflector = new \\ReflectionClass('MyClass'); * foreach($reflector->getMethods() as $method ) { * foreach($method->getParameters() as $param) { * $name = $param->getName(); * $type = Reflector::getParamType($param); * printf("%s = %s\n", $name, $type); * } * } * </code> * * @param \ReflectionParameter $refParam * @return string */ static function getParamType(\ReflectionParameter $refParam) { $export = \ReflectionParameter::export(array($refParam->getDeclaringClass()->name, $refParam->getDeclaringFunction()->name), $refParam->name, true); $export = str_replace(' or NULL', '', $export); $type = preg_replace('/.*?([\\w\\\\]+)\\s+\\$' . current(explode('=', $refParam->name)) . '.*/', '\\1', $export); if (strpos($type, 'Parameter ') !== false) { return ''; } if ($type != 'array' && strpos($type, '\\') !== 0) { $type = '\\' . $type; } return $type; }
<?php function ReflectionParameterTest($test, $test2 = null) { echo $test; } $reflect = new ReflectionFunction('ReflectionParameterTest'); $params = $reflect->getParameters(); try { foreach ($params as $key => $value) { ReflectionParameter::export($reflect, $key); } } catch (ReflectionException $e) { echo $e->getMessage() . "\n"; } try { foreach ($params as $key => $value) { ReflectionParameter::export(42, $key); } } catch (ReflectionException $e) { echo $e->getMessage() . "\n"; }
<?php function ReflectionParameterTest($test, $test2 = null) { echo $test; } $reflect = new ReflectionFunction('ReflectionParameterTest'); foreach ($reflect->getParameters() as $key => $value) { echo ReflectionParameter::export('ReflectionParameterTest', $key); } ?> ==DONE==
public static function getPropsAsStringByReflectionMethod(\ReflectionMethod $method, $withoutValues = false) { $props = array(); if ($method->getDocComment()) { $docComment = new \phpDocumentor\Reflection\DocBlock($method->getDocComment()); foreach ($docComment->getTags() as $tag) { /* @var $tag \phpDocumentor\Reflection\DocBlock\Tag\ReturnTag */ if ($tag->getName() == "param") { $props[] = self::getInstance()->getValueByType($tag->getType(), $withoutValues); } } } else { foreach ($method->getParameters() as $parameter) { // var_dump($parameter->getDefaultValue()); // var_dump($parameter->getDefaultValueConstantName()); $param = \ReflectionParameter::export(array($parameter->getDeclaringClass()->name, $parameter->getDeclaringFunction()->name), $parameter->name, true); preg_match('/(\\[ )(<)(.*)(>)( )([^\\]=]+)/', $param, $matches); if (count($matches) === 7) { $t = explode(" ", trim($matches[6])); if (count($t) === 2) { if (strtolower($t[0]) == "array") { return 'array()'; } if (strtolower($t[0]) == "closure") { return 'function(){}'; } $props[] = '$this->getMock(\'' . str_replace('\\', '\\\\', $t[0]) . '\')'; } else { if ($parameter->isDefaultValueAvailable()) { $props[] = $parameter->getDefaultValue(); } else { $props[] = 1; } } } } } $props2 = array(); foreach ($props as $prop2) { if ($prop2 != "") { $props2[] = $prop2; } } return implode(",", $props2); }
<?php function ReflectionParameterTest($test, $test2 = null) { echo $test; } $reflect = new ReflectionFunction('ReflectionParameterTest'); foreach ($reflect->getParameters() as $key => $value) { ReflectionParameter::export(); } ?> ==DONE==