/** * @param ReflectionParameter $parameter * @param CollectionInterface $properties * * @return bool */ private function canInject(\ReflectionParameter $parameter, CollectionInterface $properties) : bool { if (!$parameter->allowsNull() && !$properties->hasKey($parameter->name)) { return false; } else { if ($parameter->allowsNull() && !$properties->hasKey($parameter->name)) { return false; } } $property = $properties[$parameter->name]; if ($parameter->hasType()) { $type = $parameter->getType(); if ($type->isBuiltin()) { return (string) $type === gettype($property); } else { if (!is_object($property)) { return false; } } $refl = new \ReflectionObject($property); $wishedClass = (string) $type; return get_class($property) === $wishedClass || $refl->isSubClassOf($wishedClass); } return true; }
public function validate(\ReflectionParameter $parameter, $argument) { if ($parameter->isArray()) { if ($parameter->allowsNull() && is_null($argument)) { return; } $this->validateArrayArgument($argument); } elseif ($parameter->getClass()) { $this->validateObjectArgument($parameter->getClass()->getName(), $argument, $parameter->allowsNull()); } // other arguments don't need to be or can't be validated }
/** * @param Zend_Config $testConfig * @param ReflectionParameter $arg * @throws Exception * @throws KalturaTestException * @return Ambigous */ protected function getArgConfig(Zend_Config $testConfig, ReflectionParameter $arg) { $argName = $arg->getName(); $argConfig = $testConfig->get($argName); KalturaLog::debug("Tests data [{$argName}] config [" . print_r($argConfig, true) . "]"); if (!$argConfig) { if (!$arg->allowsNull()) { throw new Exception("Argument [{$argName}] can't be null for test [" . $this->getName() . "]"); } return null; } if (is_string($argConfig)) { return $argConfig; } switch ($argConfig->objectType) { case 'dependency': throw new KalturaTestException("Argument [{$argName}] taken from dependency"); case 'array': return $this->populateArray($argConfig); case 'native': return $argConfig->value; case 'file': return $argConfig->path; default: return $this->populateObject($argConfig); } }
/** * @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; }
/** * @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; }
/** * Returns whether NULL is allowed as this parameters's value * @return boolean */ public function allowsNull() { if ($this->parameter != null) { return $this->parameter->allowsNull(); } else { return parent::allowsNull(); } }
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()); }
private function getArgument(\ReflectionParameter $argument) { if ($argument->isOptional()) { return $argument->getDefaultValue(); } if ($argument->allowsNull()) { return null; } if ($argument->getClass()) { return $this->getMockBuilder($argument->getClass()->getName())->disableOriginalConstructor()->getMock(); } return null; }
private function shouldParameterHaveAnArgument(\ReflectionParameter $parameter) { if ($parameter->isOptional()) { // any last argument with a default value is optional return false; } if ($parameter->isDefaultValueAvailable()) { // e.g. $username = '******' return false; } if ($parameter->getClass() && $parameter->allowsNull()) { // e.g. LoggerInterface $logger = null return false; } return true; }
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; }
/** * 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; }
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; }
/** * @return bool */ public function allowsNull() { return $this->parameter->allowsNull(); }
/** * 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; }
/** * @param \ReflectionParameter $param * * @return string */ protected function documentParam(\ReflectionParameter $param) { $text = ""; if ($param->isArray()) { $text .= 'array '; } if ($param->isCallable()) { $text .= 'callable '; } $text .= '$' . $param->name; if ($param->isDefaultValueAvailable()) { if ($param->allowsNull()) { $text .= ' = null'; } else { $text .= ' = ' . str_replace("\n", ' ', print_r($param->getDefaultValue(), true)); } } return $text; }
/** * Returns <code>true</code> if the method designated in $methodName * supports <code>null</code> as value of the first/only parameter. * Used when checking if a setter method should allow <code>null</code>. * * @param string $methodName * @return boolean */ private function determineMethodParameterAllowsNull($methodName) { $parameter = new \ReflectionParameter(array($this, $methodName), 0); return $parameter->allowsNull(); }
private function getValue(ReflectionParameter $rp) { if ($rp->isDefaultValueAvailable()) { return $rp->getDefaultValue(); } else { if ($rp->isArray()) { return array(); } else { if ($rp->allowsNull()) { return null; } } } }
/** * @param \ReflectionParameter $parameter * @return string */ protected function handleOptionalParameterWithUnresolvableDefaultValue(\ReflectionParameter $parameter) { if ($parameter->allowsNull()) { return ' = NULL '; } return ' /* = unresolvable */ '; }
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(); }