Kahlan\Analysis\Inspector::typehint PHP Method

typehint() public static method

Returns the type hint of a ReflectionParameter instance.
public static typehint ( object $parameter ) : string
$parameter object A instance of `ReflectionParameter`.
return string The parameter type hint.
    public static function typehint($parameter)
    {
        $typehint = '';
        if ($parameter->getClass()) {
            $typehint = '\\' . $parameter->getClass()->getName();
        } elseif (preg_match('/.*?\\[ \\<[^\\>]+\\> (?:HH\\\\)?(\\w+)(.*?)\\$/', (string) $parameter, $match)) {
            $typehint = $match[1];
            if ($typehint === 'integer') {
                $typehint = 'int';
            } elseif ($typehint === 'boolean') {
                $typehint = 'bool';
            } elseif ($typehint === 'mixed') {
                $typehint = '';
            }
        }
        return $typehint;
    }

Usage Example

Example #1
0
 /**
  * Creates a parameters signature of a `ReflectionMethod` instance.
  *
  * @param  object $method A instance of `ReflectionMethod`.
  * @return string         The parameters definition list.
  */
 protected static function _generateSignature($method)
 {
     $params = [];
     $isVariadic = Suite::$PHP >= 7 ? $method->isVariadic() : false;
     foreach ($method->getParameters() as $num => $parameter) {
         $typehint = Inspector::typehint($parameter);
         $name = $parameter->getName();
         $name = $name && $name !== '...' ? $name : 'param' . $num;
         $reference = $parameter->isPassedByReference() ? '&' : '';
         $default = '';
         if ($parameter->isDefaultValueAvailable()) {
             $default = var_export($parameter->getDefaultValue(), true);
             $default = ' = ' . preg_replace('/\\s+/', '', $default);
         } elseif ($parameter->isOptional()) {
             if ($isVariadic && $parameter->isVariadic()) {
                 $reference = '...';
                 $default = '';
             } else {
                 $default = ' = NULL';
             }
         }
         $typehint = $typehint ? $typehint . ' ' : $typehint;
         $params[] = "{$typehint}{$reference}\${$name}{$default}";
     }
     return join(', ', $params);
 }
All Usage Examples Of Kahlan\Analysis\Inspector::typehint