public function exportCode() { $default_value = null; if ($this->_parameter->isDefaultValueAvailable()) { $default_value = $this->_parameter->getDefaultValue(); if (is_scalar($default_value) && !is_numeric($default_value)) { $default_value = "'{$default_value}'"; } } elseif ($this->_parameter->isOptional()) { $default_value = 'NULL'; } return sprintf('%s%s$%s%s', $this->_parameter->getClass() ? "{$this->_parameter->getClass()->getName()} " : '', $this->_parameter->isPassedByReference() ? '&' : '', $this->_parameter->getName(), $default_value ? " = {$default_value}" : ''); }
public function __construct(ReflectionParameter $param) { if (method_exists('ReflectionParameter', 'getType')) { if ($type = $param->getType()) { $this->type_hint = (string) $type; } } else { if ($param->isArray()) { $this->type_hint = 'array'; } else { try { if ($this->type_hint = $param->getClass()) { $this->type_hint = $this->type_hint->name; } } catch (ReflectionException $e) { preg_match('/\\[\\s\\<\\w+?>\\s([\\w]+)/s', $param->__toString(), $matches); $this->type_hint = isset($matches[1]) ? $matches[1] : ''; } } } $this->reference = $param->isPassedByReference(); $this->position = $param->getPosition(); $this->name = $param->getName(); if ($param->isDefaultValueAvailable()) { $this->default = var_export($param->getDefaultValue(), true); } }
/** * Creates a PHP parameter from reflection * * @param \ReflectionParameter $ref * @return PhpParameter */ public static function fromReflection(\ReflectionParameter $ref) { $parameter = new static(); $parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference()); if ($ref->isDefaultValueAvailable()) { $parameter->setDefaultValue($ref->getDefaultValue()); } // find type and description in docblock $docblock = new Docblock($ref->getDeclaringFunction()); $params = $docblock->getTags('param'); $tag = $params->find($ref->name, function (ParamTag $t, $name) { return $t->getVariable() == '$' . $name; }); if ($tag !== null) { $parameter->setType($tag->getType(), $tag->getDescription()); } // set type if not found in comment if ($parameter->getType() === null) { if ($ref->isArray()) { $parameter->setType('array'); } elseif ($class = $ref->getClass()) { $parameter->setType($class->getName()); } elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) { $parameter->setType('callable'); } } return $parameter; }
/** * @return self */ public static function from(\ReflectionParameter $from) { $param = new static(); $param->name = $from->getName(); $param->reference = $from->isPassedByReference(); if ($from->isArray()) { $param->typeHint = 'array'; } elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) { $param->typeHint = 'callable'; } else { try { $param->typeHint = $from->getClass() ? '\\' . $from->getClass()->getName() : NULL; } catch (\ReflectionException $e) { if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) { $param->typeHint = '\\' . $m[1]; } else { throw $e; } } } $param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || $param->typeHint && $from->allowsNull() : $from->isDefaultValueAvailable(); $param->defaultValue = PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL; $namespace = $from->getDeclaringClass() ? $from->getDeclaringClass()->getNamespaceName() : NULL; $namespace = $namespace ? "\\{$namespace}\\" : '\\'; if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) { $param->typeHint = substr($param->typeHint, strlen($namespace)); } return $param; }
/** * @return self */ public static function from(\ReflectionParameter $from) { $param = new static($from->getName()); $param->reference = $from->isPassedByReference(); if (PHP_VERSION_ID >= 70000) { $param->typeHint = $from->hasType() ? (string) $from->getType() : NULL; } elseif ($from->isArray()) { $param->typeHint = 'array'; } elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) { $param->typeHint = 'callable'; } else { try { $param->typeHint = $from->getClass() ? $from->getClass()->getName() : NULL; } catch (\ReflectionException $e) { if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) { $param->typeHint = $m[1]; } else { throw $e; } } } $param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || $param->typeHint && $from->allowsNull() : $from->isDefaultValueAvailable(); $param->defaultValue = PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL; return $param; }
/** * Returns whether this parameters is passed to by reference * @return boolean */ public function isPassedByReference() { if ($this->parameter != null) { return $this->parameter->isPassedByReference(); } else { return parent::isPassedByReference(); } }
private function getParameterInfo(\ReflectionParameter $parameter) { $paramName = '$' . $parameter->getName() . '=null'; if ($parameter->isPassedByReference()) { $paramName = '&' . $paramName; } $paramType = $parameter->getClass() !== null ? $parameter->getClass()->getName() : ''; return "{$paramType} {$paramName}"; }
function __construct(API_Doc_Method $method, ReflectionParameter $parameter) { $this->name = $parameter->getName(); $this->is_passed_by_reference = $parameter->isPassedByReference(); $this->allows_null = $parameter->allowsNull(); $this->is_optional = $parameter->isOptional(); $this->is_default_value_available = $parameter->isDefaultValueAvailable(); $this->position = $parameter->getPosition(); $this->declaring_method = $method; }
public function testWrappedMethods() { $php_parameter = new \ReflectionParameter([$this, 'method'], 'param'); $our_parameter = new ReflectionParameter($php_parameter); $this->assertSame($php_parameter->getName(), $our_parameter->getName()); $this->assertSame($php_parameter->allowsNull(), $our_parameter->allowsNull()); $this->assertSame($php_parameter->isOptional(), $our_parameter->isOptional()); $this->assertSame($php_parameter->isDefaultValueAvailable(), $our_parameter->isDefaultValueAvailable()); $this->assertSame($php_parameter->isVariadic(), $our_parameter->isVariadic()); $this->assertSame($php_parameter->isPassedByReference(), $our_parameter->isPassedByReference()); $this->assertSame($php_parameter->getDefaultValue(), $our_parameter->getDefaultValue()); }
/** * @param \ReflectionParameter $parameter * @return Parameter */ public function parameter(\ReflectionParameter $parameter) { $phpyParam = new Parameter($parameter->getName()); if ($parameter->isArray()) { $phpyParam->setTypeHint('array'); } elseif ($class = $parameter->getClass()) { $phpyParam->setTypeHint('\\' . $class->getName()); } if ($parameter->isDefaultValueAvailable()) { $phpyParam->setDefaultValue($parameter->getDefaultValue()); } $phpyParam->setByRef($parameter->isPassedByReference()); return $phpyParam; }
public static function fromReflection(\ReflectionParameter $ref) { $parameter = new static(); $parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference()); if ($ref->isDefaultValueAvailable()) { $parameter->setDefaultValue($ref->getDefaultValue()); } if ($ref->isArray()) { $parameter->setType('array'); } elseif ($class = $ref->getClass()) { $parameter->setType($class->getName()); } elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) { $parameter->setType('callable'); } return $parameter; }
/** * Gets the details for a single method argument * * @param \ReflectionParameter $argument Argument's ReflectionParameter instance * @return ArgumentData */ private function getArgumentDetails(\ReflectionParameter $argument) { $details = new ArgumentData(); $details->name = $argument->getName(); $details->passedByReference = $argument->isPassedByReference(); // true if no typehinting or typehinted argument defaults to null $details->allowsNull = $argument->allowsNull(); $details->dataType = $this->getArgumentType($argument); if ($details->dataType === 'object') { $classData = $this->getDefaultValueClassData($argument->__toString()); $details->className = $classData['className']; $details->classNamespace = $classData['classNamespace']; } if ($argument->isOptional()) { $details->isRequired = false; $details->defaultValue = $argument->getDefaultValue(); } return $details; }
public static function create_from_reflection_parameter(\ReflectionParameter $rp) { $arg = new Argument($rp->getName()); if ($rp->allowsNull()) { $arg->set_null_allowed(true); } if ($rp->isDefaultValueAvailable()) { $arg->set_default($rp->getDefaultValue()); } if ($rp->isArray()) { $arg->set_array(true); } elseif ($type = $rp->getClass()) { $arg->set_type($type->getName()); } if ($rp->isPassedByReference()) { $arg->set_reference(true); } return $arg; }
protected function generateParameterDeclaration(\ReflectionParameter $param) { $output = ''; if ($param->isArray()) { $output .= 'array '; } elseif (is_callable(array($param, 'isCallable')) && $param->isCallable()) { $output .= 'callable '; } elseif ($param->getClass()) { $output .= '\\' . $param->getClass()->getName() . ' '; } if ($param->isPassedByReference()) { $output .= '&'; } $output .= '$' . $param->getName(); if ($param->isDefaultValueAvailable()) { $output .= ' = ' . $param->getDefaultValue(); } return $output; }
/** * Collect method parameter info * * @param ReflectionParameter $parameter * @return array */ protected function _getMethodParameterInfo(ReflectionParameter $parameter) { $parameterInfo = array('name' => $parameter->getName(), 'passedByReference' => $parameter->isPassedByReference()); if ($parameter->isArray()) { $parameterInfo['type'] = 'array'; } elseif ($parameter->getClass()) { $parameterInfo['type'] = $this->_getFullyQualifiedClassName($parameter->getClass()->getName()); } if ($parameter->isOptional() && $parameter->isDefaultValueAvailable()) { $defaultValue = $parameter->getDefaultValue(); if (is_string($defaultValue)) { $parameterInfo['defaultValue'] = $this->_escapeDefaultValue($parameter->getDefaultValue()); } elseif ($defaultValue === null) { $parameterInfo['defaultValue'] = $this->_getNullDefaultValue(); } else { $parameterInfo['defaultValue'] = $defaultValue; } } return $parameterInfo; }
public function __construct($method, $param) { $param = new ReflectionParameter($method, $param); $this->name = $param->name; if ($param->isDefaultValueAvailable()) { $default = $param->getDefaultValue(); if ($default === NULL) { $this->default .= 'NULL'; } elseif (is_bool($default)) { $this->default .= $default ? 'TRUE' : 'FALSE'; } elseif (is_string($default)) { $this->default .= "'" . $default . "'"; } else { $this->default .= print_r($default, TRUE); } } if ($param->isPassedByReference()) { $this->byref = TRUE; } if ($param->isOptional()) { $this->optional = TRUE; } }
function argData(ReflectionParameter $arg) { $details = ""; $declaringclass = $arg->getDeclaringClass(); $name = $arg->getName(); $class = $arg->getClass(); $position = $arg->getPosition(); $details .= "\${$name} has position {$position}\n"; if (!empty($class)) { $classname = $class->getName(); $details .= "\${$name} must be a {$classname} object\n"; } if ($arg->isPassedByReference()) { $details .= "\${$name} is passed by reference\n"; } if ($arg->isDefaultValueAvailable()) { $def = $arg->getDefaultValue(); $details .= "\${$name} has default: {$def}\n"; } if ($arg->allowsNull()) { $details .= "\${$name} can be null\n"; } return $details; }
/** * Converts the given parameter reflection into an information array * * @param ReflectionParameter $parameter The parameter to reflect * @return array Parameter information array * @author Robert Lemke <*****@*****.**> * @author Sebastian Kurfürst <*****@*****.**> */ protected function convertParameterReflectionToArray(\ReflectionParameter $parameter, \ReflectionMethod $method = NULL) { $parameterInformation = array('position' => $parameter->getPosition(), 'byReference' => $parameter->isPassedByReference() ? TRUE : FALSE, 'array' => $parameter->isArray() ? TRUE : FALSE, 'optional' => $parameter->isOptional() ? TRUE : FALSE, 'allowsNull' => $parameter->allowsNull() ? TRUE : FALSE); $parameterClass = $parameter->getClass(); $parameterInformation['class'] = $parameterClass !== NULL ? $parameterClass->getName() : NULL; if ($parameter->isDefaultValueAvailable()) { $parameterInformation['defaultValue'] = $parameter->getDefaultValue(); } if ($parameterClass !== NULL) { $parameterInformation['type'] = ltrim($parameterClass->getName(), '\\'); } elseif ($method !== NULL) { $methodTagsAndValues = $this->getMethodTagsValues($method->getDeclaringClass()->getName(), $method->getName()); if (isset($methodTagsAndValues['param']) && isset($methodTagsAndValues['param'][$parameter->getPosition()])) { $explodedParameters = explode(' ', $methodTagsAndValues['param'][$parameter->getPosition()]); if (count($explodedParameters) >= 2) { $parameterInformation['type'] = ltrim($explodedParameters[0], '\\'); } } } return $parameterInformation; }
/** * Return string representation of parameter * * @param Parameter|ParsedParameter $parameter Reflection parameter * * @return string */ protected function getParameterCode($parameter) { $type = ''; if ($parameter->isArray()) { $type = 'array'; } elseif ($parameter->getClass()) { $type = '\\' . $parameter->getClass()->name; } $defaultValue = null; $isDefaultValueAvailable = $parameter->isDefaultValueAvailable(); if ($isDefaultValueAvailable) { if ($parameter instanceof ParsedParameter) { $defaultValue = $parameter->getDefaultValueDefinition(); } else { $defaultValue = var_export($parameter->getDefaultValue()); } } elseif ($parameter->isOptional()) { $defaultValue = 'null'; } $code = ($type ? "{$type} " : '') . ($parameter->isPassedByReference() ? '&' : '') . ($this->useVariadics && $parameter->isVariadic() ? '...' : '') . '$' . $parameter->name . ($defaultValue !== null ? " = " . $defaultValue : ''); return $code; }
public function matchReflector(\ReflectionParameter $reflector) { $this->setReference($reflector->isPassedByReference()); // Match the reflector's type hint. if ($reflector->isArray()) { $this->setTypeHint('array'); } elseif ($reflector->isCallable()) { $this->setTypeHint('callable'); } elseif ($class = $reflector->getClass()) { $this->setTypeHint($class->getName()); } // Match the reflector's default value, if there is one. It will be a // scalar value, an array of scalar values, or a constant. if ($reflector->isDefaultValueAvailable()) { if ($reflector->isDefaultValueConstant()) { $this->setValue(ConstantNode::create($reflector->getDefaultValueConstantName())); } else { $this->setValue(Node::fromValue($reflector->getDefaultValue())); } } return $this; }
private function GenerateMethodParameter(\ReflectionParameter $MethodParameter) { $TypeHint = ''; if ($MethodParameter->isArray()) { $TypeHint = 'array'; } else { if ($MethodParameter->isCallable()) { $TypeHint = 'callable'; } else { if ($MethodParameter->getClass() !== null) { $TypeHint = '\\' . $MethodParameter->getClass()->getName(); } } } $Reference = $MethodParameter->isPassedByReference() ? '&' : ''; $VariableName = '$' . $MethodParameter->getName(); $DefaultValue = ''; if ($MethodParameter->isDefaultValueAvailable()) { $DefaultValue .= '= '; /** * -- CANT USE DUE TO COMPATIBILITY WITH PHP 5.4 -- * if($MethodParameter->isDefaultValueConstant()) * $DefaultValue .= '\\' . $MethodParameter->getDefaultValueConstantName(); * else */ $DefaultValue .= var_export($MethodParameter->getDefaultValue(), true); } return implode(' ', array_filter([$TypeHint, $Reference, $VariableName, $DefaultValue])); }
private static function getParameterExpression(\ReflectionParameter $parameter) { $typeHint = null; if ($parameter->isArray()) { $typeHint = 'array'; } elseif ($parameter->isCallable()) { $typeHint = 'callable'; } elseif ($parameter->getClass() !== null) { $typeHint = $parameter->getClass()->getName(); $typeHint = $typeHint[0] === '\\' ? $typeHint : '\\' . $typeHint; } return O\Expression::parameter($parameter->getName(), $typeHint, $parameter->isDefaultValueAvailable() ? O\Expression::value($parameter->getDefaultValue()) : null, $parameter->isPassedByReference(), method_exists($parameter, 'isVariadic') && $parameter->isVariadic()); }
/** * Builds a string for a single parameter of a method. * * @param \ReflectionParameter $parameter * A reflection object of the parameter. * * @return string */ protected function buildParameter(\ReflectionParameter $parameter) { $parameter_string = ''; if ($parameter->isArray()) { $parameter_string .= 'array '; } elseif ($parameter->isCallable()) { $parameter_string .= 'callable '; } elseif ($class = $parameter->getClass()) { $parameter_string .= '\\' . $class->getName() . ' '; } if ($parameter->isPassedByReference()) { $parameter_string .= '&'; } $parameter_string .= '$' . $parameter->getName(); if ($parameter->isDefaultValueAvailable()) { $parameter_string .= ' = '; $parameter_string .= var_export($parameter->getDefaultValue(), TRUE); } return $parameter_string; }
function transformParameter(ReflectionParameter $param, $meta) { $name = $param->getName(); if ($param->getClass() instanceof ReflectionClass) { $type = $param->getClass()->name; } else { $type = @$meta['type']; } if (!$type) { $type = 'mixed'; } $description = @$meta['description']; $allow_null = $param->allowsNull(); $passed_by_ref = $param->isPassedByReference(); $is_optional = $param->isOptional(); ob_start(); include "templates/" . $this->template . "/parameter.tmpl.php"; return ob_get_clean(); }
/** * @param ReflectionParameter $arg * @return string */ protected function generateMethodArgument(ReflectionParameter $arg) { if ($arg->isArray()) { $type_hint = 'array '; } else { $type_hint = $arg->getClass() ? $arg->getClass()->getName() . ' ' : ''; } $ref = $arg->isPassedByReference() ? '&' : ''; $default = $arg->isDefaultValueAvailable() ? ' = ' . $this->generateDefaultValue($arg->getDefaultValue()) : ''; return $type_hint . $ref . '$' . $arg->getName() . $default; }
/** * This will return a full proxy-method-parameter source. * * @param \ReflectionParameter $parameter The method parameter. * * @see Proxy::$methodTemplate * * @return string */ protected function createParameter(\ReflectionParameter $parameter) { $parameterSrc = ''; $paramClass = $parameter->getClass(); if ($parameter->isArray()) { $parameterSrc .= 'array '; } else { if ($paramClass) { $parameterSrc .= $paramClass->getName() . ' '; } } if ($parameter->isPassedByReference()) { $parameterSrc .= ' &'; } $parameterSrc .= '$' . $parameter->getName(); if ($parameter->isOptional()) { $parameterSrc .= '='; if ($parameter->getDefaultValue() === null) { $parameterSrc .= 'null'; } else { if ($parameter->getDefaultValue() === false) { $parameterSrc .= 'false'; } else { if ($parameter->getDefaultValue() === true) { $parameterSrc .= 'true'; } else { if (is_array($parameter->getDefaultValue())) { $parameterSrc .= 'array()'; } else { $parameterSrc .= "'" . $parameter->getDefaultValue() . "'"; } } } } } return $parameterSrc; }
function argData(ReflectionParameter $arg) { $details = ""; $declaringClass = $arg->getDeclaringClass(); $name = $arg->getName(); $class = $arg->getClass(); $position = $arg->getPosition(); $details .= "{$name} Находится на позиции {$position} <br>"; if (!empty($class)) { $classname = $class->getName(); // возвращает объект класса ReflectionClass если в сигнатуре класса было уточнение $details .= "{$name} должен быть объектом типа {$classname} <br>"; } if ($arg->isPassedByReference()) { // проверяет является ли аргумент ссылкой $details .= "{$name} передан по ссылке <br>"; } if ($arg->isDefaultValueAvailable()) { // проверяет есть ли значение по умолчанию $def = $arg->getDefaultValue(); // получает значение по умолчанию $details = "{$name} по умолчанию равно: {$def} <br>"; } return $details; }
/** * Returns parameter name. * * @param \ReflectionParameter $param ReflectionParameter object. * @return string Parameter name. */ protected function getParameterName(\ReflectionParameter $param) { if ($param->isPassedByReference()) { return $this->referenceIndicator . $this->variableIndicator . $param->getName(); } return $this->variableIndicator . $param->getName(); }
/** * Generates the code for an individual method parameter. * * @param ReflectionParameter $parameter * * @return string */ protected function implementParameter(ReflectionParameter $parameter) { $default = ''; $type = ''; if ($parameter->isArray()) { $type = 'array '; } elseif ($parameter->getClass() !== null) { $type = $parameter->getClass()->getName() . ' '; } if ($parameter->isDefaultValueAvailable()) { $default = ' = ' . var_export($parameter->getDefaultValue(), true); } elseif ($parameter->isOptional()) { $default = ' = null'; } return $type . ($parameter->isPassedByReference() ? '&' : '') . '$parm' . $parameter->getPosition() . $default; }
private function assertSameParameterAttributes(\ReflectionParameter $original, ReflectionParameter $stubbed) { $this->assertSame($original->getName(), $stubbed->getName()); $this->assertSame($original->isArray(), $stubbed->isArray()); $this->assertSame($original->isCallable(), $stubbed->isCallable()); //$this->assertSame($original->allowsNull(), $stubbed->allowsNull()); @TODO WTF? $this->assertSame($original->canBePassedByValue(), $stubbed->canBePassedByValue()); $this->assertSame($original->isOptional(), $stubbed->isOptional()); $this->assertSame($original->isPassedByReference(), $stubbed->isPassedByReference()); $this->assertSame($original->isVariadic(), $stubbed->isVariadic()); if ($class = $original->getClass()) { $stubbedClass = $stubbed->getClass(); $this->assertInstanceOf(ReflectionClass::class, $stubbedClass); $this->assertSame($class->getName(), $stubbedClass->getName()); } else { $this->assertNull($stubbed->getClass()); } }