Skip to content

Commit

Permalink
Merge pull request #5 from mediact/feature/compatibility-phpcs-33
Browse files Browse the repository at this point in the history
2.0.2 Compatibility with PHPCS 3.3
  • Loading branch information
janmartenjongerius authored Jun 7, 2018
2 parents 452ccc0 + 36f5478 commit 123ce9c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 53 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^5.4.0 || ^7.0",
"squizlabs/php_codesniffer": "^3.0",
"squizlabs/php_codesniffer": "^3.3",
"phpmd/phpmd": "^2.0"
},
"require-dev": {
Expand Down
60 changes: 8 additions & 52 deletions src/MediactCommon/Sniffs/Php7/ReturnTypeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ public function process(File $file, $stackPtr)
return;
}

$functionStart = $stackPtr;
$functionBodyStart = $this->findFunctionBodyStartIndex($file, $functionStart);
$commentEnd = $this->findCommentEndIndex($file, $stackPtr);
$commentStart = $this->findCommentStartIndex($file, $commentEnd);
$functionStart = $stackPtr;
$commentEnd = $this->findCommentEndIndex($file, $stackPtr);
$commentStart = $this->findCommentStartIndex($file, $commentEnd);

if ($commentStart && $functionBodyStart) {
if (is_int($commentStart) && is_int($commentEnd)) {
$suggestedReturnTypes = $this->findSuggestedReturnTypes($file, $commentStart);
$returnType = $this->findActualReturnType($file, $functionStart, $functionBodyStart);
$returnType = $this->findActualReturnType($file, $functionStart);

$this->validateMultipleReturnTypes($file, $functionStart, $returnType, $suggestedReturnTypes);
$this->validateReturnTypeNotEmpty($file, $functionStart, $returnType, $suggestedReturnTypes);
Expand Down Expand Up @@ -163,57 +162,14 @@ protected function findSuggestedReturnTypes(File $file, $commentStart)
/**
* @param File $file
* @param string $functionStart
* @param string $functionBodyStart
*
* @return string
*/
protected function findActualReturnType(
File $file,
$functionStart,
$functionBodyStart
) {
$returnTypeIndex = $file->findNext(
T_RETURN_TYPE,
$functionStart,
$functionBodyStart
);

if (!$returnTypeIndex) {
// Sometimes the return tag has been parsed wrong by PHPCS
$returnTypeIndex = $this->findFunctionArrayReturnIndex(
$file,
$functionStart,
$functionBodyStart
);
}

return $returnTypeIndex
? $file->getTokens()[$returnTypeIndex]['content']
: '';
}

/**
* @param File $file
* @param int $functionStart
* @param int $functionBodyStart
*
* @return int|bool
*/
protected function findFunctionArrayReturnIndex(
File $file,
$functionStart,
$functionBodyStart
$functionStart
) {
$closingIndex = $file->findNext(
T_CLOSE_PARENTHESIS,
$functionStart,
$functionBodyStart
);

return $file->findNext(
[T_ARRAY_HINT],
$closingIndex,
$functionBodyStart
);
$properties = $file->getMethodProperties($functionStart);
return $properties['return_type'];
}
}
1 change: 1 addition & 0 deletions src/MediactCommon/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<exclude name="Generic.Commenting.DocComment.ContentAfterOpen"/>
<exclude name="Generic.Commenting.DocComment.ContentBeforeClose"/>
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
<exclude name="Generic.Commenting.DocComment.SpacingBeforeTags"/>
</rule>
<rule ref="Squiz.Commenting.FunctionComment">
<exclude name="Squiz.Commenting.FunctionComment.MissingParamComment"/>
Expand Down

0 comments on commit 123ce9c

Please sign in to comment.