Skip to content

Commit 38f565c

Browse files
API class/interfaces should still require DocBlocks for methods
* Updated unit tests to cover new functionality reguarding when we can safely skip requiring DockBlocks * Fix MethodArgumentsSniff to work on PHP files without namespaces
1 parent b442f9a commit 38f565c

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

Magento2/Sniffs/Annotation/MethodArgumentsSniff.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -664,8 +664,15 @@ private function checkIfMethodNeedsDocBlock(File $phpcsFile, $stackPtr) : bool
664664
private function checkIfNamespaceContainsApi(File $phpcsFile) : bool
665665
{
666666
$namespaceStackPtr = $phpcsFile->findNext(T_NAMESPACE, 0);
667+
if (!is_int($namespaceStackPtr)) {
668+
return false;
669+
}
667670
$tokens = $phpcsFile->getTokens();
668-
for ($index = $namespaceStackPtr; 'T_SEMICOLON' !== $tokens[$index]['type']; $index++) {
671+
for (
672+
$index = $namespaceStackPtr;
673+
array_key_exists($index, $tokens) && 'T_SEMICOLON' !== $tokens[$index]['type'];
674+
$index++
675+
) {
669676
if ('T_STRING' === $tokens[$index]['type'] && 'Api' === $tokens[$index]['content']) {
670677
return true;
671678
}

Magento2/Tests/Annotation/MethodArgumentsUnitTest.inc

+52-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,63 @@ public function invalidDocBlockShouldNotCauseFatalErrorInSniff(int $number): int
4141
* @return string
4242
*/
4343
#[\ReturnTypeWillChange]
44-
public function methodWithAttributeAndValidDocblock(string $text): string
44+
public function methodWithAttributeAndValidDocblockWithTypes(string $text): string
45+
{
46+
return $text;
47+
}
48+
49+
/**
50+
* Short description.
51+
*
52+
* @param string $text
53+
* @return string
54+
*/
55+
#[\ReturnTypeWillChange]
56+
public function methodWithAttributeAndValidDocblockWithoutTypes()
4557
{
4658
return $text;
4759
}
4860

4961
#[\ReturnTypeWillChange]
50-
public function methodWithAttributeAndWithoutDocblock(string $text): string
62+
public function methodWithAttributeAndWithoutDocblockWithTypes(string $text): string
63+
{
64+
return $text;
65+
}
66+
67+
#[\ReturnTypeWillChange]
68+
public function methodWithAttributeAndWithoutDocblockWithoutTypes($text)
69+
{
70+
return $text;
71+
}
72+
73+
public function methodWithoutDockblockWithParameterTypeWithoutReturnType(string $text)
74+
{
75+
return $text;
76+
}
77+
78+
public function methodWithoutDockblockWithoutParameterTypeWithReturnType($text) : string
79+
{
80+
return $text;
81+
}
82+
83+
/**
84+
* Short description.
85+
*
86+
* @param string $text
87+
* @return string
88+
*/
89+
public function methodWithDockblockWithParameterTypeWithoutReturnType(string $text)
90+
{
91+
return $text;
92+
}
93+
94+
/**
95+
* Short description.
96+
*
97+
* @param mixed $text
98+
* @return string
99+
*/
100+
public function methodWithDockblockWithoutParameterTypeWithReturnType($text) : string
51101
{
52102
return $text;
53103
}

Magento2/Tests/Annotation/MethodArgumentsUnitTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public function getErrorList()
1818
12 => 1,
1919
21 => 1,
2020
32 => 1,
21-
50 => 1
21+
68 => 1,
22+
73 => 1,
23+
78 => 1,
2224
];
2325
}
2426

Magento2/Tests/Commenting/ClassPropertyPHPDocFormattingUnitTest.inc

+17
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,20 @@ class correctlyFormattedClassMemberDocBlock
201201
*/
202202
protected string $deprecatedWithKeyword;
203203
}
204+
205+
class classWithPropertiesWithTypesAndWithoutDocBlock
206+
{
207+
protected string $test1 = 'asdf';
208+
209+
protected ?string $test2;
210+
211+
private array $array1 = [];
212+
213+
public array $array2;
214+
215+
protected readonly int $readOnlyInteger;
216+
217+
protected readonly string $readOnlyString;
218+
219+
private ?\Exception $exception;
220+
}

0 commit comments

Comments
 (0)