PHP_CodeSniffer::suggestType PHP Method

suggestType() public static method

If type is not one of the standard type, it must be a custom type. Returns the correct type name suggestion if type name is invalid.
public static suggestType ( string $varType ) : string
$varType string The variable type to process.
return string
    public static function suggestType($varType)
    {
        if ($varType === '') {
            return '';
        }
        if (in_array($varType, self::$allowedTypes) === true) {
            return $varType;
        } else {
            $lowerVarType = strtolower($varType);
            switch ($lowerVarType) {
                case 'bool':
                case 'boolean':
                    return 'boolean';
                case 'double':
                case 'real':
                case 'float':
                    return 'float';
                case 'int':
                case 'integer':
                    return 'integer';
                case 'array()':
                case 'array':
                    return 'array';
            }
            //end switch
            if (strpos($lowerVarType, 'array(') !== false) {
                // Valid array declaration:
                // array, array(type), array(type1 => type2).
                $matches = array();
                $pattern = '/^array\\(\\s*([^\\s^=^>]*)(\\s*=>\\s*(.*))?\\s*\\)/i';
                if (preg_match($pattern, $varType, $matches) !== 0) {
                    $type1 = '';
                    if (isset($matches[1]) === true) {
                        $type1 = $matches[1];
                    }
                    $type2 = '';
                    if (isset($matches[3]) === true) {
                        $type2 = $matches[3];
                    }
                    $type1 = self::suggestType($type1);
                    $type2 = self::suggestType($type2);
                    if ($type2 !== '') {
                        $type2 = ' => ' . $type2;
                    }
                    return "array({$type1}{$type2})";
                } else {
                    return 'array';
                }
                //end if
            } else {
                if (in_array($lowerVarType, self::$allowedTypes) === true) {
                    // A valid type, but not lower cased.
                    return $lowerVarType;
                } else {
                    // Must be a custom type name.
                    return $varType;
                }
            }
            //end if
        }
        //end if
    }

Usage Example

 /**
  * Process the var tag.
  *
  * @param int $commentStart The position in the stack where the comment started.
  * @param int $commentEnd   The position in the stack where the comment ended.
  *
  * @return void
  */
 protected function processVar($commentStart, $commentEnd)
 {
     $var = $this->commentParser->getVar();
     if ($var !== null) {
         $errorPos = $commentStart + $var->getLine();
         $index = array_keys($this->commentParser->getTagOrders(), 'var');
         if (count($index) > 1) {
             $error = 'Only 1 @var tag is allowed in variable comment';
             $this->currentFile->addError($error, $errorPos, 'DuplicateVar');
             return;
         }
         if ($index[0] !== 1) {
             $error = 'The @var tag must be the first tag in a variable comment';
             $this->currentFile->addError($error, $errorPos, 'VarOrder');
         }
         $content = $var->getContent();
         if (empty($content) === true) {
             $error = 'Var type missing for @var tag in variable comment';
             $this->currentFile->addError($error, $errorPos, 'MissingVarType');
             return;
         } else {
             $suggestedType = PHP_CodeSniffer::suggestType($content);
             if ($suggestedType !== $content) {
                 // Hotfix - somehow they do not like "int" and "bool".
                 switch ($content) {
                     case 'int':
                         $suggestedType = 'int';
                         break;
                     case 'bool':
                         $suggestedType = 'bool';
                         break;
                     default:
                 }
             }
             if ($content !== $suggestedType) {
                 $error = 'Expected "%s"; found "%s" for @var tag in variable comment';
                 $data = array($suggestedType, $content);
                 $this->currentFile->addError($error, $errorPos, 'IncorrectVarType', $data);
             }
         }
         $spacing = substr_count($var->getWhitespaceBeforeContent(), ' ');
         if ($spacing !== 1) {
             $error = '@var tag indented incorrectly; expected 1 space but found %s';
             $data = array($spacing);
             $this->currentFile->addError($error, $errorPos, 'VarIndent', $data);
         }
     } else {
         $error = 'Missing @var tag in variable comment';
         $this->currentFile->addError($error, $commentEnd, 'MissingVar');
     }
     //end if
 }
All Usage Examples Of PHP_CodeSniffer::suggestType