Creates context instance.

@param ReflectionClass $reflection
@param array           $arguments

@return mixed
9771
private function createInstance(ReflectionClass $reflection, array $arguments)
    {
        if (count($arguments)) {
            return $reflection->newInstanceArgs($arguments);
        }

        return $reflection->newInstance();
    }
473907
public static function createResourceForClass(\ReflectionClass $reflectionClass)
    {
        @trigger_error('The '.__METHOD__.'() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use ContainerBuilder::getReflectionClass() instead.', E_USER_DEPRECATED);

        $metadata = array();

        foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
            if (!$reflectionMethod->isStatic()) {
                $metadata[$reflectionMethod->name] = self::getResourceMetadataForMethod($reflectionMethod);
            }
        }

        return new AutowireServiceResource($reflectionClass->name, $reflectionClass->getFileName(), $metadata);
    }
117472
private function getInstance($class, array $args)
    {
        // Hack to avoid Reflection for most cases
        switch (count($args)) {
            case 0:
                return new $class;
            case 1:
                return new $class($args[0]);
            case 2:
                return new $class($args[0], $args[1]);
            case 3:
                return new $class($args[0], $args[1], $args[2]);
            case 4:
                return new $class($args[0], $args[1], $args[2], $args[3]);
            case 5:
                return new $class($args[0], $args[1], $args[2], $args[3], $args[4]);
            case 6:
                return new $class($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
            default:
                $reflection = new ReflectionClass();
                return $reflection->newInstanceArgs($args);
        }
    }
1313647
static function createFromReflection(\ReflectionClass $reflectionClass) {
    $namespaceUseContext = ('' === $namespaceName = $reflectionClass->getNamespaceName())
      ? new NamespaceUseContext_Empty()
      : new NamespaceUseContext_NamespaceOnly($namespaceName);
    return new Declaration($reflectionClass->getDocComment(), $namespaceUseContext);
  }
326470
protected static function createInstance(array $args = [])
    {
        if (!self::$reflection) {
            self::$reflection = new \ReflectionClass('IntlDateFormatter');
        }

        $instance = self::$reflection->newInstanceArgs($args);

        self::checkInternalClass($instance, '\IntlDateFormatter', $args);

        return $instance;
    }
755734
public function make($name, array $args = [])
    {
        if (! isset($this->classes[$name])) {
            throw new FactoryException('"' . $name . '" is not a registered class');
        }

        $class = new \ReflectionClass($this->classes[$name]);

        return $class->newInstanceArgs($args);
    }
1057989
public static function instantiateClassByConstructor($class, $args)
    {
        switch (count($args)) {
            case 0:
                return new $class();
            case 1:
                return new $class($args[0]);
            case 2:
                return new $class($args[0], $args[1]);
            case 3:
                return new $class($args[0], $args[1], $args[2]);
            case 4:
                return new $class($args[0], $args[1], $args[2], $args[3]);
            default:
                $rClass = new \ReflectionClass($class);
                return $rClass->newInstanceArgs($args);
        }
    }
28056
public function make(string $class = null, string $type = null, array $args = [])
    {
        $class = $class ?? $this->class ?? $this->resolveClassName($type);
        \assert('class_exists($class)');
        $reflection = new \ReflectionClass($class);
        if ($args !== [] && !\is_int(key($args))) {
            $args = $this->resolveConstructorArguments($args, $reflection);
        }
        return $reflection->newInstanceArgs($args);
    }
886287
public function callClassMethod($class, $method, ...$arguments)
	{
		$resolvedParameters = [];
		$reflectedClass = new ReflectionCLass($class);
		if (!$reflectedClass->hasMethod($method)) {
			throw new ReflectionException('Call to undefined method ' . $method);
		}

		$methodName = $method;
		$method = $reflectedClass->getMethod($method);
		$methodParameters = $method->getParameters();

		if (count($methodParameters) < 1) {
			return $method;
		}

		$resolvedParameters = array_map(function($param) use ($reflectedClass) {
			$type = $param->getType();

			if (preg_match("/(.*?)\\\/", $type)) {
				$type = (String) $type;
				return new $type;
			}

			if (!$param->isDefaultValueAvailable()) {
				return $param = '';
			}

			return $param->getDefaultValue();
			
		}, $methodParameters);

		$resolvedParameters = array_filter($resolvedParameters);
		$resolvedParameters = array_merge($resolvedParameters, $arguments);

		if ($methodName == '__construct') {
			return $reflectedClass->newInstanceArgs($resolvedParameters);
		}

		return $method->invokeArgs($class, $resolvedParameters);
	}
662662
private function createInstance(string $class, array $parameters, string $context = null)
    {
        try {
            $reflection = new \ReflectionClass($class);
        } catch (\ReflectionException $e) {
            throw new ContainerException($e->getMessage(), $e->getCode(), $e);
        }

        //We have to construct class using external injector when we know exact context
        if (empty($parameters) && $this->hasInjector($reflection)) {
            $instance = $this->getInjector($reflection)->createInjection($reflection, $context);

            if (!$reflection->isInstance($instance)) {
                throw new InjectionException(sprintf(
                    "Invalid injection response for '%s'",
                    $reflection->getName()
                ));
            }

            return $instance;
        }

        if (!$reflection->isInstantiable()) {
            throw new ContainerException(sprintf("Class '%s' can not be constructed", $class));
        }

        if (!empty($constructor = $reflection->getConstructor())) {
            //Using constructor with resolved arguments
            $instance = $reflection->newInstanceArgs(
                $this->resolveArguments($constructor, $parameters)
            );
        } else {
            //No constructor specified
            $instance = $reflection->newInstance();
        }

        return $instance;
    }
1169253
public static function newInstance($class, ...$args)
    {
        if(!isset(static::$classNameMap[$class]))
        {
            $ref = new \ReflectionClass($class);
            $className = static::getNewClassName($ref->getShortName());
            $tpl = static::getTpl($ref, $className);
            eval($tpl);
            static::$classNameMap[$class] = $className;
        }
        return new static::$classNameMap[$class](...$args);
    }
323271
public static function make($namespace,$args=[]) {
        $reflect = new \ReflectionClass($namespace);
        $constructor = $reflect->getConstructor();
        if (!$constructor || $constructor->getNumberOfParameters() == 0) {
            $constructor = $reflect->newInstance();
        }
        else {
            $constructor = $reflect->newInstanceArgs($args);
        }
        return $constructor;
    }
328500
public function getParameters(\ReflectionFunctionAbstract $reflection, array $args = [])
    {
        $dependencies = $reflection->getParameters();
        $parameters = [];

        foreach ($dependencies as $parameter) {
            $parameters[] = $this->getParameter($parameter, $args);
        }

        return $parameters;
    }
563982
protected function createDefinition(\ReflectionClass $reflection): Definition
    {
        $definition = new Definition($reflection->getName());

        foreach ($this->processors as $processor)
        {
            $processor->process($reflection, $definition);
        }

        $parent = $reflection->getParentClass();

        if ($parent !== false) {
            $definition->merge($this->createDefinition($parent));
        }

        foreach ($reflection->getTraits() as $trait)
        {
            $definition->merge($this->createDefinition($trait));
        }

        return $definition;
    }
918097
private function resolveConstructorArguments(ReflectionMethod $constructor, array $arguments)
    {
        $constructorParameters = $constructor->getParameters();

        foreach ($constructorParameters as $position => $parameter) {
            if ($parameter->getClass() && ServiceMocker::class === $parameter->getClass()->getName()) {
                $arguments[$position] = $this->serviceMocker;
            }
        }

        return $arguments;
    }
507417
private function resolverClass($dependency, $arguments)
    {
        list($resolveClass, $reflectionParam) = $this->injector->getReflectionParam($dependency);

        // Application and Container cannot be injected into controller currently
        // since Application constructor is protected
        if ($reflectionParam->IsInstantiable()) {
            return $this->makeInstance($resolveClass, $arguments);
        }

        return $this->injector->interfaceInjection($reflectionParam);
    }
383595
public final function getInstance($requestedClass, ...$arguments)
    {
        if (isset($this->singletons[$requestedClass])){
            return $this->singletons[$requestedClass];
        }

        $useSingleton = false;

        // Check for singletons first as they should trump previous registrations of non
        // singleton mappings.
        if (isset($this->concreteClassMappings['_'.$requestedClass])) {
            $class = $this->concreteClassMappings['_'.$requestedClass];
            $useSingleton = true;
        } elseif (isset($this->concreteClassMappings[$requestedClass])){
            $class = $this->concreteClassMappings[$requestedClass];
        } else {
            $class = $requestedClass;
        }

        $reflection = new \ReflectionClass($class);

        if ($reflection->isAbstract()){
            throw new ClassMappingException($class);
        }

        $constructor = $reflection->getConstructor();

        if ($constructor == null){
            // No defined constructor so exit simply with a new instance.
            $instance = $reflection->newInstanceArgs($arguments);
        } else {
            $instance = $this->generateInstanceFromConstructor($reflection, $constructor, $arguments);
        }

        if ($useSingleton) {
            $this->singletons[$requestedClass] = $instance;
        }

        return $instance;
    }