Skip to content

Commit

Permalink
API class/interfaces should still require DocBlocks for methods
Browse files Browse the repository at this point in the history
* Updated unit tests to cover new functionality reguarding when we can safely skip requiring DockBlocks
* Fix MethodArgumentsSniff to work on PHP files without namespaces
  • Loading branch information
JacobBrownAustin committed Dec 18, 2023
1 parent b442f9a commit 38f565c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
9 changes: 8 additions & 1 deletion Magento2/Sniffs/Annotation/MethodArgumentsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,15 @@ private function checkIfMethodNeedsDocBlock(File $phpcsFile, $stackPtr) : bool
private function checkIfNamespaceContainsApi(File $phpcsFile) : bool
{
$namespaceStackPtr = $phpcsFile->findNext(T_NAMESPACE, 0);
if (!is_int($namespaceStackPtr)) {
return false;
}
$tokens = $phpcsFile->getTokens();
for ($index = $namespaceStackPtr; 'T_SEMICOLON' !== $tokens[$index]['type']; $index++) {
for (
$index = $namespaceStackPtr;
array_key_exists($index, $tokens) && 'T_SEMICOLON' !== $tokens[$index]['type'];
$index++
) {
if ('T_STRING' === $tokens[$index]['type'] && 'Api' === $tokens[$index]['content']) {
return true;
}
Expand Down
54 changes: 52 additions & 2 deletions Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,63 @@ public function invalidDocBlockShouldNotCauseFatalErrorInSniff(int $number): int
* @return string
*/
#[\ReturnTypeWillChange]
public function methodWithAttributeAndValidDocblock(string $text): string
public function methodWithAttributeAndValidDocblockWithTypes(string $text): string
{
return $text;
}

/**
* Short description.
*
* @param string $text
* @return string
*/
#[\ReturnTypeWillChange]
public function methodWithAttributeAndValidDocblockWithoutTypes()
{
return $text;
}

#[\ReturnTypeWillChange]
public function methodWithAttributeAndWithoutDocblock(string $text): string
public function methodWithAttributeAndWithoutDocblockWithTypes(string $text): string
{
return $text;
}

#[\ReturnTypeWillChange]
public function methodWithAttributeAndWithoutDocblockWithoutTypes($text)
{
return $text;
}

public function methodWithoutDockblockWithParameterTypeWithoutReturnType(string $text)
{
return $text;
}

public function methodWithoutDockblockWithoutParameterTypeWithReturnType($text) : string
{
return $text;
}

/**
* Short description.
*
* @param string $text
* @return string
*/
public function methodWithDockblockWithParameterTypeWithoutReturnType(string $text)
{
return $text;
}

/**
* Short description.
*
* @param mixed $text
* @return string
*/
public function methodWithDockblockWithoutParameterTypeWithReturnType($text) : string
{
return $text;
}
4 changes: 3 additions & 1 deletion Magento2/Tests/Annotation/MethodArgumentsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function getErrorList()
12 => 1,
21 => 1,
32 => 1,
50 => 1
68 => 1,
73 => 1,
78 => 1,
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,20 @@ class correctlyFormattedClassMemberDocBlock
*/
protected string $deprecatedWithKeyword;
}

class classWithPropertiesWithTypesAndWithoutDocBlock
{
protected string $test1 = 'asdf';

protected ?string $test2;

private array $array1 = [];

public array $array2;

protected readonly int $readOnlyInteger;

protected readonly string $readOnlyString;

private ?\Exception $exception;
}

0 comments on commit 38f565c

Please sign in to comment.