Skip to content

Commit

Permalink
Finalize DocBlockParamAllowDefaultValue sniff
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Mar 10, 2016
1 parent 929715f commit b8d3ae6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
47 changes: 31 additions & 16 deletions PSR2R/Sniffs/Commenting/DocBlockParamAllowDefaultValueSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,24 @@ public function process(PHP_CodeSniffer_File $phpCsFile, $stackPointer) {
if ($methodSignatureValue['typehint']) {
$typeIndex = $methodSignatureValue['typehint'];
$type = $tokens[$typeIndex]['content'];
if (!in_array($type, $pieces) && ($type !== 'array' || !$this->isOfTypeArray($pieces))) {
if (!in_array($type, $pieces) && ($type !== 'array' || !$this->containsTypeArray($pieces))) {
$pieces[] = $type;
$error = 'Possible doc block error: `' . $content . '` seems to be missing type `' . $type . '`.';
$fix = $phpCsFile->addFixableError($error, $classNameIndex, 'Typehint');
if ($fix) {
$pieces[] = $type;
$content = implode('|', $pieces);
$phpCsFile->fixer->replaceToken($classNameIndex, $content . $appendix);
}
}
}
if ($methodSignatureValue['default']) {
$typeIndex = $methodSignatureValue['default'];
$type = $tokens[$typeIndex]['content'];
if (!in_array($type, $pieces) && ($type !== 'array' || !$this->isOfTypeArray($pieces))) {
$type = $methodSignatureValue['default'];

if (!in_array($type, $pieces) && ($type !== 'array' || !$this->containsTypeArray($pieces))) {
$pieces[] = $type;
$error = 'Possible doc block error: `' . $content . '` seems to be missing type `' . $type . '`.';
$fix = $phpCsFile->addFixableError($error, $classNameIndex, 'Default');
if ($fix) {
$pieces[] = $type;
$content = implode('|', $pieces);
$phpCsFile->fixer->replaceToken($classNameIndex, $content . $appendix);
}
Expand All @@ -132,27 +132,42 @@ protected function getMethodSignature(PHP_CodeSniffer_File $phpCsFile, $stackPtr
$arguments = [];
$i = $startIndex;
while ($nextVariableIndex = $phpCsFile->findNext(T_VARIABLE, $i + 1, $endIndex)) {
$typehint = $default = null;
$typehintIndex = $defaultIndex = $default = null;
$possibleTypeHint = $phpCsFile->findPrevious([T_ARRAY_HINT, T_CALLABLE], $nextVariableIndex - 1, $nextVariableIndex - 3);
if ($possibleTypeHint) {
$typehint = $possibleTypeHint;
}
if ($possibleTypeHint) {
$typehint = $possibleTypeHint;
$typehintIndex = $possibleTypeHint;
}

$possibleEqualIndex = $phpCsFile->findNext([T_EQUAL], $nextVariableIndex + 1, $nextVariableIndex + 2);
$possibleEqualIndex = $phpCsFile->findNext([T_EQUAL], $nextVariableIndex + 1, $nextVariableIndex + 3);
if ($possibleEqualIndex) {
$possibleDefaultValue = $phpCsFile->findNext([T_STRING, T_TRUE, T_FALSE, T_NULL, T_ARRAY], $possibleEqualIndex + 1, $possibleEqualIndex + 2);
$whitelist = [T_CONSTANT_ENCAPSED_STRING, T_TRUE, T_FALSE, T_NULL, T_OPEN_SHORT_ARRAY, T_LNUMBER, T_DNUMBER];
$possibleDefaultValue = $phpCsFile->findNext($whitelist, $possibleEqualIndex + 1, $possibleEqualIndex + 3);
if ($possibleDefaultValue) {
$default = $possibleDefaultValue;
$defaultIndex = $possibleDefaultValue;
//$default = $tokens[$defaultIndex]['content'];
if ($tokens[$defaultIndex]['code'] === T_CONSTANT_ENCAPSED_STRING) {
$default = 'string';
} elseif ($tokens[$defaultIndex]['code'] === T_OPEN_SHORT_ARRAY) {
$default = 'array';
} elseif ($tokens[$defaultIndex]['code'] === T_FALSE || $tokens[$defaultIndex]['code'] === T_TRUE) {
$default = 'bool';
} elseif ($tokens[$defaultIndex]['code'] === T_LNUMBER) {
$default = 'int';
} elseif ($tokens[$defaultIndex]['code'] === T_DNUMBER) {
$default = 'float';
} elseif ($tokens[$defaultIndex]['code'] === T_NULL) {
$default = 'null';
} else {
//die('Invalid default type: ' . $default);
}
}
}

$arguments[] = [
'variable' => $nextVariableIndex,
'typehint' => $typehint,
'default' => $default
'typehint' => $typehintIndex,
'defaultIndex' => $defaultIndex,
'default' => $default,
];

$i = $nextVariableIndex;
Expand Down
2 changes: 1 addition & 1 deletion PSR2R/Sniffs/Commenting/DocBlockParamArraySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function process(PHP_CodeSniffer_File $phpCsFile, $stackPointer) {
if (!in_array('array', $pieces, true)) {
continue;
}
if (!$this->isOfTypeArray($pieces)) {
if (!$this->containsTypeArray($pieces)) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion PSR2R/Tools/Traits/CommentingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function hasInheritDoc(PHP_CodeSniffer_File $phpCsFile, $docBlockStart
* @param array $docBlockTypes
* @return bool
*/
protected function isOfTypeArray($docBlockTypes) {
protected function containsTypeArray($docBlockTypes) {
foreach ($docBlockTypes as $docBlockType) {
if (strpos($docBlockType, '[]') !== false) {
return true;
Expand Down

0 comments on commit b8d3ae6

Please sign in to comment.