isOptional() public method

public isOptional ( ) : boolean
return boolean True if this is an optional parameter
Example #1
0
 /**
  * @param Method $method
  * Get a list of methods hydrated with type information
  * for the given partial method
  *
  * @param CodeBase $code_base
  * The global code base holding all state
  *
  * @return Method[]
  * A list of typed methods based on the given method
  */
 private static function methodListFromMethod(Method $method, CodeBase $code_base) : array
 {
     // See if we have any type information for this
     // internal function
     $map_list = UnionType::internalFunctionSignatureMapForFQSEN($method->getFQSEN());
     if (!$map_list) {
         return [$method];
     }
     $alternate_id = 0;
     return array_map(function ($map) use($method, &$alternate_id) : Method {
         $alternate_method = clone $method;
         $alternate_method->setFQSEN($alternate_method->getFQSEN()->withAlternateId($alternate_id++));
         // Set the return type if one is defined
         if (!empty($map['return_type'])) {
             $alternate_method->setUnionType($map['return_type']);
         }
         // Load properties if defined
         foreach ($map['property_name_type_map'] ?? [] as $parameter_name => $parameter_type) {
             $flags = 0;
             $is_optional = false;
             // Check to see if its a pass-by-reference parameter
             if (strpos($parameter_name, '&') === 0) {
                 $flags |= \ast\flags\PARAM_REF;
                 $parameter_name = substr($parameter_name, 1);
             }
             // Check to see if its variadic
             if (strpos($parameter_name, '...') !== false) {
                 $flags |= \ast\flags\PARAM_VARIADIC;
                 $parameter_name = str_replace('...', '', $parameter_name);
             }
             // Check to see if its an optional parameter
             if (strpos($parameter_name, '=') !== false) {
                 $is_optional = true;
                 $parameter_name = str_replace('=', '', $parameter_name);
             }
             $parameter = new Parameter($method->getContext(), $parameter_name, $parameter_type, $flags);
             if ($is_optional) {
                 $parameter->setDefaultValueType(NullType::instance()->asUnionType());
             }
             // Add the parameter
             $alternate_method->parameter_list[] = $parameter;
         }
         $alternate_method->setNumberOfRequiredParameters(array_reduce($alternate_method->parameter_list, function (int $carry, Parameter $parameter) : int {
             return $carry + ($parameter->isOptional() ? 0 : 1);
         }, 0));
         $alternate_method->setNumberOfOptionalParameters(count($alternate_method->parameter_list) - $alternate_method->getNumberOfRequiredParameters());
         return $alternate_method;
     }, $map_list);
 }