Ejemplo n.º 1
0
Archivo: Clazz.php Proyecto: etsy/phan
 /**
  * Add a method to this class
  *
  * @param CodeBase $code_base
  * A reference to the code base in which the ancestor exists
  *
  * @param Method $method
  * The method to copy onto this class
  *
  * @param Option<Type>|None $type_option
  * A possibly defined type used to define template
  * parameter types when importing the method
  *
  * @return null
  */
 public function addMethod(CodeBase $code_base, Method $method, $type_option)
 {
     $method_fqsen = FullyQualifiedMethodName::make($this->getFQSEN(), $method->getName(), $method->getFQSEN()->getAlternateId());
     // Don't overwrite overridden methods with
     // parent methods
     if ($code_base->hasMethodWithFQSEN($method_fqsen)) {
         // Note that we're overriding something
         $existing_method = $code_base->getMethodByFQSEN($method_fqsen);
         $existing_method->setIsOverride(true);
         // Don't add the method
         return;
     }
     if ($method->getFQSEN() !== $method_fqsen) {
         $method = clone $method;
         $method->setDefiningFQSEN($method->getFQSEN());
         $method->setFQSEN($method_fqsen);
         // If we have a parent type defined, map the method's
         // return type and parameter types through it
         if ($type_option->isDefined()) {
             // Map the method's return type
             if ($method->getUnionType()->hasTemplateType()) {
                 $method->setUnionType($method->getUnionType()->withTemplateParameterTypeMap($type_option->get()->getTemplateParameterTypeMap($code_base)));
             }
             // Map each method parameter
             $method->setParameterList(array_map(function (Parameter $parameter) use($type_option, $code_base) : Parameter {
                 if (!$parameter->getUnionType()->hasTemplateType()) {
                     return $parameter;
                 }
                 $mapped_parameter = clone $parameter;
                 $mapped_parameter->setUnionType($mapped_parameter->getUnionType()->withTemplateParameterTypeMap($type_option->get()->getTemplateParameterTypeMap($code_base)));
                 return $mapped_parameter;
             }, $method->getParameterList()));
         }
     }
     if ($method->getHasYield()) {
         // There's no phpdoc standard for template types of Generators at the moment.
         $newType = UnionType::fromFullyQualifiedString('\\Generator');
         $oldType = $method->getUnionType();
         if (!$newType->canCastToUnionType($method->getUnionType())) {
             $method->setUnionType($newType);
         }
     }
     $code_base->addMethod($method);
 }
Ejemplo n.º 2
0
Archivo: Clazz.php Proyecto: tpunt/phan
 /**
  * Add a method to this class
  *
  * @param CodeBase $code_base
  * A reference to the code base in which the ancestor exists
  *
  * @param Method $method
  * The method to copy onto this class
  *
  * @param Option<Type> $type_option
  * A possibly defined type used to define template
  * parameter types when importing the method
  *
  * @return null
  */
 public function addMethod(CodeBase $code_base, Method $method, $type_option)
 {
     $method_fqsen = FullyQualifiedMethodName::make($this->getFQSEN(), $method->getName());
     // Don't overwrite overridden methods with
     // parent methods
     if ($code_base->hasMethodWithFQSEN($method_fqsen)) {
         // Note that we're overriding something
         $existing_method = $code_base->getMethodByFQSEN($method_fqsen);
         $existing_method->setIsOverride(true);
         // Don't add the method
         return;
     }
     if ($method->getFQSEN() !== $method_fqsen) {
         $method = clone $method;
         $method->setDefiningFQSEN($method->getFQSEN());
         $method->setFQSEN($method_fqsen);
         // If we have a parent type defined, map the method's
         // return type and parameter types through it
         if ($type_option->isDefined()) {
             // Map the method's return type
             if ($method->getUnionType()->hasTemplateType()) {
                 $method->setUnionType($method->getUnionType()->withTemplateParameterTypeMap($type_option->get()->getTemplateParameterTypeMap($code_base)));
             }
             // Map each method parameter
             $method->setParameterList(array_map(function (Parameter $parameter) use($type_option, $code_base) : Parameter {
                 if (!$parameter->getUnionType()->hasTemplateType()) {
                     return $parameter;
                 }
                 $mapped_parameter = clone $parameter;
                 $mapped_parameter->setUnionType($mapped_parameter->getUnionType()->withTemplateParameterTypeMap($type_option->get()->getTemplateParameterTypeMap($code_base)));
                 return $mapped_parameter;
             }, $method->getParameterList()));
         }
     }
     $code_base->addMethod($method);
 }