Phan\Language\Element\Method::defaultConstructorForClassInContext PHP Method

defaultConstructorForClassInContext() public static method

public static defaultConstructorForClassInContext ( Clazz $clazz, Context $context, CodeBase $code_base ) : Method
$clazz Clazz
$context Phan\Language\Context
$code_base Phan\CodeBase
return Method A default constructor for the given class
    public static function defaultConstructorForClassInContext(Clazz $clazz, Context $context, CodeBase $code_base) : Method
    {
        $method_fqsen = FullyQualifiedMethodName::make($clazz->getFQSEN(), '__construct');
        $method = new Method($context, '__construct', $clazz->getUnionType(), 0, $method_fqsen);
        if ($clazz->hasMethodWithName($code_base, $clazz->getName())) {
            $old_style_constructor = $clazz->getMethodByName($code_base, $clazz->getName());
            $method->setParameterList($old_style_constructor->getParameterList());
            $method->setNumberOfRequiredParameters($old_style_constructor->getNumberOfRequiredParameters());
            $method->setNumberOfOptionalParameters($old_style_constructor->getNumberOfOptionalParameters());
        }
        return $method;
    }

Usage Example

Example #1
0
 /**
  * @return Method
  * The method with the given name
  */
 public function getMethodByNameInContext(CodeBase $code_base, string $name, Context $context) : Method
 {
     $method_fqsen = FullyQualifiedMethodName::make($this->getFQSEN(), $name);
     if (!$code_base->hasMethod($method_fqsen)) {
         if ('__construct' === $name) {
             // Create a default constructor if its requested
             // but doesn't exist yet
             $default_constructor = Method::defaultConstructorForClassInContext($this, $this->getContext()->withClassFQSEN($this->getFQSEN()));
             $this->addMethod($code_base, $default_constructor);
             return $default_constructor;
         }
         throw new CodeBaseException("Method with name {$name} does not exist for class {$this->getFQSEN()}.");
     }
     return $code_base->getMethod($method_fqsen);
 }