Esempio n. 1
0
 /**
  * @param \ReflectionParameter $parameter
  * @return mixed
  */
 private function getArgumentMock(\ReflectionParameter $parameter)
 {
     if ($parameter->getClass() !== null) {
         return $this->testCase->getMockBuilder($parameter->getClass()->getName())->disableOriginalConstructor()->getMock();
     }
     throw new \Mocktainer\UnmockableMethodArgumentException($parameter->getDeclaringClass()->getName(), $parameter->getDeclaringFunction()->getName(), $parameter->getName());
 }
Esempio n. 2
0
 public function getParamTypeHint(\ReflectionFunctionAbstract $function, \ReflectionParameter $param, array $arguments = [])
 {
     $lowParam = strtolower($param->name);
     if ($function instanceof \ReflectionMethod) {
         $lowClass = strtolower($function->class);
         $lowMethod = strtolower($function->name);
         $paramCacheKey = self::CACHE_KEY_CLASSES . "{$lowClass}.{$lowMethod}.param-{$lowParam}";
     } else {
         $lowFunc = strtolower($function->name);
         $paramCacheKey = $lowFunc !== '{closure}' ? self::CACHE_KEY_FUNCS . ".{$lowFunc}.param-{$lowParam}" : null;
     }
     $typeHint = $paramCacheKey === null ? false : $this->cache->fetch($paramCacheKey);
     if (false !== $typeHint) {
         return $typeHint;
     }
     if ($reflectionClass = $param->getClass()) {
         $typeHint = $reflectionClass->getName();
         $classCacheKey = self::CACHE_KEY_CLASSES . strtolower($typeHint);
         $this->cache->store($classCacheKey, $this->getClass($param->getClass()->getName()));
     } elseif ($function instanceof \ReflectionMethod && ($docBlockParams = $this->getDocBlock($function)->getTagsByName('param')) && !empty($docBlockParams)) {
         $typeHint = $this->getParamDocBlockHint($docBlockParams, $param, $arguments);
         // store the ExtendedReflectionClass in the cache
         if ($typeHint !== false) {
             $classCacheKey = self::CACHE_KEY_CLASSES . strtolower($typeHint);
             $this->cache->store($classCacheKey, $this->getClass($typeHint));
         }
     } else {
         $typeHint = null;
     }
     $this->cache->store($paramCacheKey, $typeHint);
     return $typeHint;
 }
Esempio 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;
 }
 /**
  * @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;
 }
 public function getDeclaration()
 {
     if ($this->reflector->isArray()) {
         $code = 'array ';
     } else {
         $class = $this->reflector->getClass();
         if ($class !== null) {
             $code = $class->name . ' ';
         } else {
             $code = '';
         }
     }
     $code .= '$' . $this->reflector->name;
     if ($this->reflector->isOptional()) {
         $default = $this->reflector->getDefaultValue();
         if (is_null($default)) {
             $default = 'null';
         } elseif (is_bool($default)) {
             $default = $default ? 'true' : 'false';
         } elseif (is_string($default)) {
             $default = "'" . $default . "'";
         } elseif (is_numeric($default)) {
             $default = strval($default);
         } elseif (is_array($default)) {
             $default = 'array()';
         } else {
             echo 'Warning: unknown default type for ' . $this->getMethod()->getFullName() . PHP_EOL;
             var_dump($default);
             $default = 'null';
         }
         $code .= ' = ' . $default;
     }
     return $code;
 }
Esempio n. 6
0
 /**
  * @param string $className
  * @param string $argumentName
  * @param \ReflectionParameter $parameter
  * @return mixed
  */
 private function getArgumentMock(string $className, string $argumentName, \ReflectionParameter $parameter)
 {
     if ($parameter->getClass() !== null) {
         return $this->testCase->getMockBuilder($parameter->getClass()->name)->disableOriginalConstructor()->getMock();
     }
     throw new \Mocktainer\UnmockableConstructorArgumentException($className, $argumentName);
 }
Esempio n. 7
0
 private function isGoutte1()
 {
     $refl = new \ReflectionParameter(array('Goutte\\Client', 'setClient'), 0);
     if ($refl->getClass() && 'Guzzle\\Http\\ClientInterface' === $refl->getClass()->getName()) {
         return true;
     }
     return false;
 }
 /**
  * @inheritdoc
  */
 public function getClass()
 {
     $class = $this->parameter->getClass();
     if ($class) {
         return $class->getName();
     }
     return null;
 }
 private function getParameterInfo(\ReflectionParameter $parameter)
 {
     $paramName = '$' . $parameter->getName() . '=null';
     if ($parameter->isPassedByReference()) {
         $paramName = '&' . $paramName;
     }
     $paramType = $parameter->getClass() !== null ? $parameter->getClass()->getName() : '';
     return "{$paramType} {$paramName}";
 }
Esempio n. 10
0
 /**
  * return type hint, if any (none or object type - inteface, class, ...)
  *
  * @return string
  */
 public function getTypeHint()
 {
     if ($this->typeHint === null) {
         if (($class = $this->reflectionParameter->getClass()) !== null) {
             $this->typeHint = $class->getName();
         }
     }
     return $this->typeHint;
 }
 private function getType(\ReflectionParameter $param)
 {
     if ($param->getClass() && $param->getClass()->getName()) {
         return $param->getClass()->getName();
     }
     if ($param->isArray()) {
         return 'Array';
     }
 }
 public function validate(\ReflectionParameter $parameter, $argument)
 {
     if ($parameter->isArray()) {
         $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
 }
Esempio n. 13
0
 /**
  * @param Horde_Injector $injector
  * @param ReflectionParameter $method
  *
  * @return mixed
  * @throws Horde_Injector_Exception
  */
 public function getParameterDependency(Horde_Injector $injector, ReflectionParameter $parameter)
 {
     if ($parameter->getClass()) {
         return $injector->getInstance($parameter->getClass()->getName());
     } elseif ($parameter->isOptional()) {
         return $parameter->getDefaultValue();
     }
     throw new Horde_Injector_Exception("Untyped parameter \$" . $parameter->getName() . "can't be fulfilled");
 }
 /**
  * @param \ReflectionParameter $reflectionParameter
  *
  * @return null|string
  */
 protected function getTypeByReflectionParameter(\ReflectionParameter $reflectionParameter)
 {
     if ($reflectionParameter->getClass()) {
         return $reflectionParameter->getClass()->getName();
     }
     if ($reflectionParameter->isArray()) {
         return 'array';
     }
     return null;
 }
Esempio n. 15
0
 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;
 }
Esempio n. 16
0
 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}" : '');
 }
Esempio n. 17
0
 private static function loadArray($object, &$result, $prefix = "")
 {
     $reflect = new ReflectionClass($object);
     $typeName = $reflect->getName();
     $properties = ObjectParser::getProperties($object);
     if (is_array($properties)) {
         foreach ($properties as $key => $prop) {
             $propertyName = $prop->getName();
             $propNameCamelCase = ucfirst($propertyName);
             $getMethodName = "get{$propNameCamelCase}";
             $setMethodName = "set{$propNameCamelCase}";
             $setIdMethodName = "{$setMethodName}_id";
             $setMethodParameter = new ReflectionParameter(array($typeName, $setMethodName), 0);
             if ($reflect->getMethod($setMethodName) && $setMethodParameter->getClass() && method_exists($object, $setIdMethodName)) {
                 continue;
             }
             $value = $object->{$getMethodName}();
             $name = $prefix . $propertyName;
             if (is_object($value)) {
                 ObjectParser::loadArray($value, $result, "{$name}.");
             } else {
                 $result[$name] = $value;
             }
         }
         return true;
     }
     return false;
 }
Esempio n. 18
0
 public function getParamTypeHint(\ReflectionFunctionAbstract $function, \ReflectionParameter $param)
 {
     $lowParam = strtolower($param->name);
     if ($function instanceof \ReflectionMethod) {
         $lowClass = strtolower($function->class);
         $lowMethod = strtolower($function->name);
         $paramCacheKey = self::CACHE_KEY_CLASSES . "{$lowClass}.{$lowMethod}.param-{$lowParam}";
     } else {
         $lowFunc = strtolower($function->name);
         $paramCacheKey = $lowFunc !== '{closure}' ? self::CACHE_KEY_FUNCS . ".{$lowFunc}.param-{$lowParam}" : null;
     }
     $typeHint = $paramCacheKey === null ? false : $this->cache->fetch($paramCacheKey);
     if (false !== $typeHint) {
         return $typeHint;
     }
     if ($reflectionClass = $param->getClass()) {
         $typeHint = $reflectionClass->getName();
         $classCacheKey = self::CACHE_KEY_CLASSES . strtolower($typeHint);
         $this->cache->store($classCacheKey, $reflectionClass);
     } else {
         $typeHint = null;
     }
     $this->cache->store($paramCacheKey, $typeHint);
     return $typeHint;
 }
Esempio n. 19
0
 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);
     }
 }
Esempio n. 20
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 '';
             }
         }
     }
 }
Esempio n. 21
0
 public function getClass()
 {
     $phpReflection = parent::getClass();
     $zendReflection = new ZendL_Reflection_Class($phpReflection->getName());
     unset($phpReflection);
     return $zendReflection;
 }
Esempio n. 22
0
 /**
  * Gets the argument value for a parameter.
  *
  * @param \ReflectionParameter $parameter
  *   The parameter of a callable to get the value for.
  *
  * @return mixed
  *   The value of the requested parameter value.
  *
  * @throws \RuntimeException
  *   Thrown when there is a missing parameter.
  */
 protected function getArgument(\ReflectionParameter $parameter)
 {
     $parameter_type_hint = $parameter->getClass();
     $parameter_name = $parameter->getName();
     // If the argument exists and is NULL, return it, regardless of
     // parameter type hint.
     if (!isset($this->objects[$parameter_name]) && array_key_exists($parameter_name, $this->objects)) {
         return NULL;
     }
     if ($parameter_type_hint) {
         // If the argument exists and complies with the type hint, return it.
         if (isset($this->objects[$parameter_name]) && is_object($this->objects[$parameter_name]) && $parameter_type_hint->isInstance($this->objects[$parameter_name])) {
             return $this->objects[$parameter_name];
         }
         // Otherwise, resolve wildcard arguments by type matching.
         foreach ($this->wildcards as $wildcard) {
             if ($parameter_type_hint->isInstance($wildcard)) {
                 return $wildcard;
             }
         }
     } elseif (isset($this->scalars[$parameter_name])) {
         return $this->scalars[$parameter_name];
     }
     // If the callable provides a default value, use it.
     if ($parameter->isDefaultValueAvailable()) {
         return $parameter->getDefaultValue();
     }
     // Can't resolve it: call a method that throws an exception or can be
     // overridden to do something else.
     return $this->handleUnresolvedArgument($parameter);
 }
Esempio n. 23
0
 /**
  * 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;
 }
Esempio n. 24
0
 /**
  * @param \ReflectionParameter $parameter
  * @return mixed
  */
 private function loadParameter(\ReflectionParameter $parameter)
 {
     $class = $parameter->getClass();
     if (is_null($class)) {
         throw new \Exception('Constructor parameter must be autoloaded but missing parameter type for parameter ' . $parameter->getName());
     }
     return $this->getSkeleton()->get($class->getName());
 }
Esempio n. 25
0
 private static function buildField(\ReflectionParameter $parameter, $data)
 {
     // If parameter turns out to be an Object then recurse
     if (!empty($parameter->getClass())) {
         return self::build($parameter->getClass()->getName(), $data);
     }
     // If parameter is type of array, then check whether it is an array of Object
     if ($parameter->isArray()) {
         $possibleObjectClassName = $parameter->getDeclaringClass()->getName() . '_' . $parameter->getName() . '_item';
         if (class_exists($possibleObjectClassName)) {
             return array_map(function ($values) use($possibleObjectClassName) {
                 return self::build($possibleObjectClassName, $values);
             }, $data);
         }
     }
     return $data;
 }
 /**
  * Helper method to get the type of a reflection paramter
  * @param \ReflectionParameter $parameter
  * @return NULL|\ReflectionType|string
  */
 protected function getReflectionParameterName(\ReflectionParameter $parameter)
 {
     // parameter is a class
     if ($class = $parameter->getClass()) {
         return $class->getName() . ' \\$' . $parameter->getName();
     }
     return $parameter->getName();
 }
 /**
  * Returns true if the given controller parameter is type-hinted as an instance of ScopeFetcher.
  *
  * @param \ReflectionParameter $controllerParam A parameter of the controller action.
  *
  * @return bool
  */
 private function isScopeFetcherType(\ReflectionParameter $controllerParam)
 {
     $class = $controllerParam->getClass();
     if (!$class) {
         return false;
     }
     return $class->implementsInterface(self::SCOPE_FETCHER_INTERFACE);
 }
 function it_resolves_an_argument_if_it_is_an_element_type(Factory $factory, \ReflectionClass $class, \ReflectionMethod $constructor, \ReflectionParameter $anyParameter, \ReflectionParameter $elementParameter, \ReflectionClass $elementParameterClass, MyElement $element)
 {
     $constructor->getParameters()->willReturn(array($anyParameter, $elementParameter));
     $elementParameter->getClass()->willReturn($elementParameterClass);
     $elementParameterClass->getName()->willReturn('spec\\SensioLabs\\Behat\\PageObjectExtension\\Context\\Argument\\MyElement');
     $factory->instantiate('spec\\SensioLabs\\Behat\\PageObjectExtension\\Context\\Argument\\MyElement')->willReturn($element);
     $this->resolveArguments($class, array())->shouldReturn(array(1 => $element));
 }
Esempio n. 29
0
 /**
  * Gets a class.
  * 
  * @return \Wingu\OctopusCore\Reflection\ReflectionClass
  */
 public function getClass()
 {
     $class = parent::getClass();
     if ($class !== null) {
         $class = new ReflectionClass($class->getName());
     }
     return $class;
 }
Esempio n. 30
0
 /**
  * Returns the parameter class
  *
  * @return ClassReflection The parameter class
  */
 public function getClass()
 {
     try {
         $class = parent::getClass();
     } catch (\Exception $e) {
         return NULL;
     }
     return is_object($class) ? new ClassReflection($class->getName()) : NULL;
 }