Exemplo n.º 1
0
 public function __construct(\ReflectionParameter $parameter)
 {
     if (!$parameter->isCallable()) {
         throw new \InvalidArgumentException('Provided parameter should have a callable type hint');
     }
     parent::__construct($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;
 }
Exemplo n.º 3
0
 /**
  * @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;
 }
Exemplo n.º 4
0
 /**
  * @param \ReflectionParameter $param
  *
  * @return string|null
  */
 protected function _getHintName(\ReflectionParameter $param)
 {
     if (version_compare(phpversion(), '5.4', '>=') && $param->isCallable()) {
         return 'callable';
     }
     if ($param->isArray()) {
         return 'array';
     }
     if ($class = $param->getClass()) {
         return $class->getName();
     }
 }
Exemplo n.º 5
0
 private function buildParameter(\ReflectionParameter $parameter)
 {
     switch (true) {
         case $parameter->getClass() !== null:
             return new ClassArgument($parameter);
         case $parameter->isArray():
             return new ArrayArgument($parameter);
         case $parameter->isCallable():
             return new CallableArgument($parameter);
         default:
             return new MixedArgument($parameter);
     }
 }
 /**
  * Checks if the value matches the parameter type.
  *
  * @param \ReflectionParameter $parameter
  * @param mixed                $value
  *
  * @return bool
  */
 protected static function matchType(\ReflectionParameter $parameter, $value)
 {
     if ($class = $parameter->getClass()) {
         return is_object($value) && $class->isInstance($value);
     }
     if ($parameter->isArray()) {
         return is_array($value);
     }
     if ($parameter->isCallable()) {
         return is_callable($value);
     }
     return true;
 }
Exemplo n.º 7
0
 /**
  * @return string|NULL
  */
 public static function getParameterType(\ReflectionParameter $param)
 {
     if ($param->isArray() || $param->isCallable()) {
         return $param->isArray() ? 'array' : 'callable';
     } else {
         try {
             return ($ref = $param->getClass()) ? $ref->getName() : NULL;
         } catch (\ReflectionException $e) {
             if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
                 return $m[1];
             }
             throw $e;
         }
     }
 }
Exemplo n.º 8
0
 private function generateTypeHint(\ReflectionParameter $param)
 {
     if ($param->isArray()) {
         return 'array ';
     } else {
         if ($param->isCallable()) {
             return 'callable ';
         } else {
             if ($param->getClass()) {
                 return $param->getClass()->getName() . ' ';
             } else {
                 return '';
             }
         }
     }
 }
Exemplo n.º 9
0
 /**
  * @return string|NULL
  */
 public static function getParameterType(\ReflectionParameter $param)
 {
     if (PHP_VERSION_ID >= 70000) {
         return $param->hasType() ? (string) $param->getType() : NULL;
     } elseif ($param->isArray()) {
         return 'array';
     } elseif (PHP_VERSION_ID >= 50400 && $param->isCallable()) {
         return 'callable';
     } else {
         try {
             return ($ref = $param->getClass()) ? $ref->getName() : NULL;
         } catch (\ReflectionException $e) {
             if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
                 return $m[1];
             }
             throw $e;
         }
     }
 }
Exemplo n.º 10
0
 /**
  * Returns an associated type to the given parameter if available.
  *
  * @param \ReflectionParameter $parameter
  *
  * @return null|string
  */
 private function getType(\ReflectionParameter $parameter)
 {
     if (PHP_VERSION_ID >= 70000) {
         return $parameter->hasType() ? (string) $parameter->getType() : null;
     }
     if ($parameter->isArray()) {
         return 'array';
     }
     if ($parameter->isCallable()) {
         return 'callable';
     }
     try {
         $refClass = $parameter->getClass();
     } catch (\ReflectionException $e) {
         // mandatory; extract it from the exception message
         return str_replace(array('Class ', ' does not exist'), '', $e->getMessage());
     }
     return $refClass ? $refClass->getName() : null;
 }
Exemplo n.º 11
0
 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;
 }
Exemplo n.º 12
0
 /**
  * @return string|NULL
  */
 public static function getParameterType(\ReflectionParameter $param)
 {
     if (PHP_VERSION_ID >= 70000) {
         if ($param->hasType()) {
             $type = PHP_VERSION_ID >= 70100 ? $param->getType()->getName() : (string) $param->getType();
             return strtolower($type) === 'self' ? $param->getDeclaringClass()->getName() : $type;
         }
     } elseif ($param->isArray() || $param->isCallable()) {
         return $param->isArray() ? 'array' : 'callable';
     } else {
         try {
             return ($ref = $param->getClass()) ? $ref->getName() : NULL;
         } catch (\ReflectionException $e) {
             if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
                 return $m[1];
             }
             throw $e;
         }
     }
 }
Exemplo n.º 13
0
 /**
  * Determine the fully-qualified name of a type-hint for the given param without actually loading typehinted classes.
  *
  * @param \ReflectionParameter $param
  * @return string Hinted typename or NULL when no type-hint is present.
  */
 protected function getParamType(\ReflectionParameter $param)
 {
     if ($param->isArray()) {
         return 'array';
     }
     if ($param->isCallable()) {
         return 'callable';
     }
     $m = NULL;
     if (defined('HHVM_VERSION')) {
         // @codeCoverageIgnoreStart
         $type = $param->getTypehintText();
         if ('' === trim($type)) {
             $type = NULL;
         }
         // @codeCoverageIgnoreEnd
     } elseif (preg_match("'\\[\\s*<[^>]+>\\s+([a-z_][a-z_0-9]*(?:\\s*\\\\\\s*[a-z_][a-z_0-9]*)*)'i", (string) $param, $m)) {
         $type = preg_replace("'\\s+'", '', $m[1]);
     } else {
         $type = NULL;
     }
     if ($type !== NULL) {
         switch (strtolower($type)) {
             case 'self':
                 $ref = $param->getDeclaringFunction();
                 if ($ref instanceof \ReflectionMethod) {
                     return $ref->getDeclaringClass()->name;
                 }
                 throw new \RuntimeException(sprintf('Unable to resolve "self" in parameter "%s" of function %s', $param->name, $ref->name));
             case 'boolean':
                 return 'bool';
             case 'integer':
                 return 'int';
         }
         return $type;
     }
     return NULL;
 }
Exemplo n.º 14
0
 /**
  * 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->isCallable()) {
         $type = 'callable';
     } 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(), true);
         }
     } elseif ($parameter->isOptional()) {
         $defaultValue = 'null';
     }
     $code = ($type ? "{$type} " : '') . ($parameter->isPassedByReference() ? '&' : '') . ($this->useVariadics && $parameter->isVariadic() ? '...' : '') . '$' . $parameter->name . ($defaultValue !== null ? " = " . $defaultValue : '');
     return $code;
 }
Exemplo n.º 15
0
 /**
  * @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;
 }
Exemplo n.º 16
0
 /**
  * Retrieve method parameter info
  *
  * @param \ReflectionParameter $parameter
  * @return array
  */
 protected function _getMethodParameterInfo(\ReflectionParameter $parameter)
 {
     $parameterInfo = ['name' => $parameter->getName(), 'passedByReference' => $parameter->isPassedByReference()];
     if ($parameter->isArray()) {
         $parameterInfo['type'] = 'array';
     } elseif ($parameter->getClass()) {
         $parameterInfo['type'] = $this->_getFullyQualifiedClassName($parameter->getClass()->getName());
     } elseif ($parameter->isCallable()) {
         $parameterInfo['type'] = 'callable';
     }
     if ($parameter->isOptional() && $parameter->isDefaultValueAvailable()) {
         $defaultValue = $parameter->getDefaultValue();
         if (is_string($defaultValue)) {
             $parameterInfo['defaultValue'] = $parameter->getDefaultValue();
         } elseif ($defaultValue === null) {
             $parameterInfo['defaultValue'] = $this->_getNullDefaultValue();
         } else {
             $parameterInfo['defaultValue'] = $defaultValue;
         }
     }
     return $parameterInfo;
 }
Exemplo n.º 17
0
 /**
  * @param \ReflectionParameter $r
  *
  * @return string
  */
 protected function getParameterDefinition(\ReflectionParameter $r)
 {
     $type = '';
     if ($r->isArray()) {
         $type = 'array';
     } elseif ($r->isCallable()) {
         $type = 'callable';
     } elseif ($r->getClass()) {
         $type = '\\' . $r->getClass()->getName();
     }
     $name = $r->getName();
     $default = '';
     if ($r->isDefaultValueAvailable() and $r->isDefaultValueConstant()) {
         $default = ' = \\' . $r->getDefaultValueConstantName();
     } elseif ($r->isDefaultValueAvailable()) {
         $default = ' = ' . var_export($r->getDefaultValue(), true);
     }
     $definition = trim($type . ' $' . $name . $default);
     return $definition;
 }
Exemplo n.º 18
0
 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;
 }
 /**
  * @return [string, bool]
  */
 public static function getParameterType(\ReflectionParameter $param)
 {
     $def = gettype($param->isDefaultValueAvailable() ? $param->getDefaultValue() : NULL);
     if (PHP_VERSION_ID >= 70000) {
         return array((string) $param->getType() ?: $def, $param->hasType() && !$param->getType()->isBuiltin());
     } elseif ($param->isArray()) {
         return array('array', FALSE);
     } elseif (PHP_VERSION_ID >= 50400 && $param->isCallable()) {
         return array('callable', FALSE);
     } else {
         try {
             return ($ref = $param->getClass()) ? array($ref->getName(), TRUE) : array($def, FALSE);
         } catch (\ReflectionException $e) {
             if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
                 throw new \LogicException(sprintf("Class %s not found. Check type hint of parameter \$%s in %s() or 'use' statements.", $m[1], $param->getName(), $param->getDeclaringFunction()->getDeclaringClass()->getName() . '::' . $param->getDeclaringFunction()->getName()));
             }
             throw $e;
         }
     }
 }
Exemplo n.º 20
0
 /**
  * @param \ReflectionParameter $param
  * @return null|string
  */
 private function getParamType(\ReflectionParameter $param)
 {
     $class = $param->getClass();
     if ($class) {
         return $class->getName();
     } elseif ($param->isArray()) {
         return self::TYPE_ARRAY;
     } elseif ($param->isCallable()) {
         return self::TYPE_CALLABLE;
     } else {
         return null;
     }
 }
 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]));
 }
Exemplo n.º 22
0
 /**
  * @param \ReflectionParameter $reflection
  * @return ParameterBlock
  */
 public static function buildFromReflection(\ReflectionParameter $reflection)
 {
     $type = null;
     if ($reflection->isCallable()) {
         $type = 'callable';
     }
     if ($reflection->isArray()) {
         $type = 'array';
     }
     if ($reflection->getClass()) {
         $type = $reflection->getClass()->getName();
     }
     $defaultValue = null;
     if ($reflection->isDefaultValueAvailable()) {
         $defaultValue = $reflection->getDefaultValue();
     }
     return new self($reflection->getName(), $type, $reflection->isOptional(), $defaultValue, $reflection->isPassedByReference());
 }
 /**
  * @param \ReflectionParameter $parameter
  * @return string
  */
 protected function getPhpForParameter(\ReflectionParameter $parameter)
 {
     $php = null;
     if ($parameter->getClass() instanceof \ReflectionClass) {
         $php .= '\\' . $parameter->getClass()->name . ' ';
     } elseif ($parameter->isCallable()) {
         $php .= 'callable ';
     } elseif ($parameter->isArray()) {
         $php .= 'array ';
     }
     if ($parameter->isPassedByReference()) {
         $php .= '&';
     }
     $php .= '$' . $parameter->name;
     if ($parameter->isDefaultValueAvailable()) {
         $parameterDefaultValue = $parameter->getDefaultValue();
         if ($parameter->isDefaultValueConstant()) {
             $defaultValue = $parameter->getDefaultValueConstantName();
         } elseif ($parameterDefaultValue === null) {
             $defaultValue = 'null';
         } elseif (is_bool($parameterDefaultValue)) {
             $defaultValue = $parameterDefaultValue === true ? 'true' : 'false';
         } elseif (is_string($parameterDefaultValue)) {
             $defaultValue = '\'' . $parameterDefaultValue . '\'';
         } elseif (is_array($parameterDefaultValue)) {
             $defaultValue = 'array()';
         } else {
             $defaultValue = $parameterDefaultValue;
         }
         $php .= ' = ' . $defaultValue;
     }
     return $php;
 }
 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());
     }
 }
Exemplo n.º 25
0
 /**
  * Resolve a supplied argument to its final value.
  *
  * A supplied argument may be:
  * 1. A string but the actual required argument is an object so therefore the string(class name)
  * must be resolved into the actual required object.
  *
  * 2. A callable so therefore must be invoked to get its final value.
  *
  * @param mixed $argument
  * @param \ReflectionParameter $parameter
  *
  * @return mixed
  *
  * @throws InjectorException
  */
 private function resolveArgument($argument, \ReflectionParameter $parameter)
 {
     if (is_string($argument) && $parameter->getClass() !== null) {
         return $this->make($argument);
     }
     return is_callable($argument) && !$parameter->isCallable() ? $this->invoke($argument) : $argument;
 }
Exemplo n.º 26
0
 private function isUnsupportedTypeHinting(\ReflectionParameter $parameter)
 {
     return $parameter->isArray() || $parameter->isCallable();
 }
Exemplo n.º 27
0
 private function isUnsupportedTypeHinting(\ReflectionParameter $parameter)
 {
     return $parameter->isArray() || version_compare(PHP_VERSION, '5.4.0', '>') && $parameter->isCallable();
 }
Exemplo n.º 28
0
 /**
  * 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;
 }
 /**
  * Returns if the parameter expects a callback.
  *
  * @return boolean
  */
 public function isCallable()
 {
     return PHP_VERSION >= 50400 && parent::isCallable();
 }
Exemplo n.º 30
0
 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());
 }