Skip to content

Commit

Permalink
move external util class to function comment sniff (autoloader problem)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-evans committed Aug 10, 2018
1 parent f60cb6a commit c8093e4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 106 deletions.
96 changes: 92 additions & 4 deletions West/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\FunctionCommentSniff as SquizFunctionCommentSniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Config;
use West\CodingStandard\Util\Common;

/**
* Formatting tests for functions (including methods).
Expand Down Expand Up @@ -50,6 +49,23 @@ class FunctionCommentSniff extends SquizFunctionCommentSniff
*/
private $phpVersion = null;

/**
* An array of variable types for param/var we will check.
*
* @var string[]
*/
private static $allowedTypes = [
'array',
'bool',
'float',
'int',
'mixed',
'object',
'string',
'resource',
'callable',
];

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -106,7 +122,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
$typeNames = explode('|', $returnType);
$suggestedNames = [];
foreach ($typeNames as $i => $typeName) {
$suggestedName = Common::suggestType($typeName);
$suggestedName = self::suggestType($typeName);
if (in_array($suggestedName, $suggestedNames) === false) {
$suggestedNames[] = $suggestedName;
}
Expand Down Expand Up @@ -390,7 +406,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
$typeName = substr($typeName, 1);
}

$suggestedName = Common::suggestType($typeName);
$suggestedName = self::suggestType($typeName);
$suggestedTypeNames[] = $suggestedName;

if (count($typeNames) > 1) {
Expand All @@ -405,7 +421,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
$suggestedTypeHint = 'callable';
} else if (strpos($suggestedName, 'callback') !== false) {
$suggestedTypeHint = 'callable';
} else if (in_array($suggestedName, Common::$allowedTypes) === false) {
} else if (in_array($suggestedName, self::$allowedTypes) === false) {
$suggestedTypeHint = $suggestedName;
}

Expand Down Expand Up @@ -550,4 +566,76 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
$phpcsFile->addError($error, $commentStart, 'MissingParamTag', $data);
}
}

/**
* Returns a valid variable type for param/var tag.
*
* 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.
*
* @param string $varType The variable type to process.
*
* @return string
*/
private 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 'bool';
case 'double':
case 'real':
case 'float':
return 'float';
case 'int':
case 'integer':
return 'int';
case 'array()':
case 'array':
return 'array';
}

if (strpos($lowerVarType, 'array(') !== false) {
// Valid array declaration:
// array, array(type), array(type1 => type2).
$matches = [];
$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';
}
} elseif (in_array($lowerVarType, self::$allowedTypes) === true) {
// A valid type, but not lower cased.
return $lowerVarType;
} else {
// Must be a custom type name.
return $varType;
}
}
}
}
102 changes: 0 additions & 102 deletions West/Util/Common.php

This file was deleted.

0 comments on commit c8093e4

Please sign in to comment.