Skip to content

Commit fe201de

Browse files
committed
Merge branch 'specialMethod' of https://github.com/VincentLanglet/PHP_CodeSniffer
2 parents d96b6d8 + ccca313 commit fe201de

File tree

8 files changed

+127
-11
lines changed

8 files changed

+127
-11
lines changed

src/Standards/PEAR/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ class FunctionCommentSniff implements Sniff
2525
*/
2626
public $minimumVisibility = 'private';
2727

28+
/**
29+
* Array of methods which do not require a return type.
30+
*
31+
* @var array
32+
*/
33+
public $specialMethods = [
34+
'__construct',
35+
'__destruct',
36+
];
37+
2838

2939
/**
3040
* Returns an array of tokens this test wants to listen for.
@@ -135,7 +145,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
135145

136146
// Skip constructor and destructor.
137147
$methodName = $phpcsFile->getDeclarationName($stackPtr);
138-
$isSpecialMethod = ($methodName === '__construct' || $methodName === '__destruct');
148+
$isSpecialMethod = in_array($methodName, $this->specialMethods, true);
139149

140150
$return = null;
141151
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
@@ -150,17 +160,17 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
150160
}
151161
}
152162

153-
if ($isSpecialMethod === true) {
154-
return;
155-
}
156-
157163
if ($return !== null) {
158164
$content = $tokens[($return + 2)]['content'];
159165
if (empty($content) === true || $tokens[($return + 2)]['code'] !== T_DOC_COMMENT_STRING) {
160166
$error = 'Return type missing for @return tag in function comment';
161167
$phpcsFile->addError($error, $return, 'MissingReturnType');
162168
}
163169
} else {
170+
if ($isSpecialMethod === true) {
171+
return;
172+
}
173+
164174
$error = 'Missing @return tag in function comment';
165175
$phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
166176
}//end if

src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,34 @@ private function setTranslator4($a, &$b): void
398398
{
399399
$this->translator = $translator;
400400
}
401+
402+
class Bar {
403+
/**
404+
* The PHP5 constructor
405+
*
406+
* @return
407+
*/
408+
public function __construct() {
409+
410+
}
411+
}
412+
413+
// phpcs:set PEAR.Commenting.FunctionComment specialMethods[]
414+
class Bar {
415+
/**
416+
* The PHP5 constructor
417+
*/
418+
public function __construct() {
419+
420+
}
421+
}
422+
423+
// phpcs:set PEAR.Commenting.FunctionComment specialMethods[] ignored
424+
/**
425+
* Should be ok
426+
*/
427+
public function ignored() {
428+
429+
}
430+
431+
// phpcs:set PEAR.Commenting.FunctionComment specialMethods[] __construct,__destruct

src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.inc.fixed

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,34 @@ private function setTranslator4($a, &$b): void
398398
{
399399
$this->translator = $translator;
400400
}
401+
402+
class Bar {
403+
/**
404+
* The PHP5 constructor
405+
*
406+
* @return
407+
*/
408+
public function __construct() {
409+
410+
}
411+
}
412+
413+
// phpcs:set PEAR.Commenting.FunctionComment specialMethods[]
414+
class Bar {
415+
/**
416+
* The PHP5 constructor
417+
*/
418+
public function __construct() {
419+
420+
}
421+
}
422+
423+
// phpcs:set PEAR.Commenting.FunctionComment specialMethods[] ignored
424+
/**
425+
* Should be ok
426+
*/
427+
public function ignored() {
428+
429+
}
430+
431+
// phpcs:set PEAR.Commenting.FunctionComment specialMethods[] __construct,__destruct

src/Standards/PEAR/Tests/Commenting/FunctionCommentUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public function getErrorList()
6868
361 => 1,
6969
363 => 1,
7070
364 => 1,
71+
406 => 1,
72+
417 => 1,
7173
];
7274

7375
}//end getErrorList()

src/Standards/Squiz/Sniffs/Commenting/FunctionCommentSniff.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
6767

6868
// Skip constructor and destructor.
6969
$methodName = $phpcsFile->getDeclarationName($stackPtr);
70-
$isSpecialMethod = ($methodName === '__construct' || $methodName === '__destruct');
71-
if ($isSpecialMethod === true) {
72-
return;
73-
}
70+
$isSpecialMethod = in_array($methodName, $this->specialMethods, true);
7471

7572
if ($return !== null) {
7673
$content = $tokens[($return + 2)]['content'];
@@ -181,6 +178,10 @@ protected function processReturn(File $phpcsFile, $stackPtr, $commentStart)
181178
}//end if
182179
}//end if
183180
} else {
181+
if ($isSpecialMethod === true) {
182+
return;
183+
}
184+
184185
$error = 'Missing @return tag in function comment';
185186
$phpcsFile->addError($error, $tokens[$commentStart]['comment_closer'], 'MissingReturn');
186187
}//end if

src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ class Baz {
666666
* Test
667667
*
668668
* @return void
669-
* @throws E
669+
* @throws E
670670
*/
671671
function myFunction() {}
672672

@@ -1021,3 +1021,23 @@ public function foo($a, $b) {}
10211021
* @return mixed
10221022
*/
10231023
public function foo(mixed $a): mixed {}
1024+
1025+
// phpcs:set Squiz.Commenting.FunctionComment specialMethods[]
1026+
class Bar {
1027+
/**
1028+
* The PHP5 constructor
1029+
*/
1030+
public function __construct() {
1031+
1032+
}
1033+
}
1034+
1035+
// phpcs:set Squiz.Commenting.FunctionComment specialMethods[] ignored
1036+
/**
1037+
* Should be ok
1038+
*/
1039+
public function ignored() {
1040+
1041+
}
1042+
1043+
// phpcs:set Squiz.Commenting.FunctionComment specialMethods[] __construct,__destruct

src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.inc.fixed

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ class Baz {
666666
* Test
667667
*
668668
* @return void
669-
* @throws E
669+
* @throws E
670670
*/
671671
function myFunction() {}
672672

@@ -1021,3 +1021,23 @@ public function foo($a, $b) {}
10211021
* @return mixed
10221022
*/
10231023
public function foo(mixed $a): mixed {}
1024+
1025+
// phpcs:set Squiz.Commenting.FunctionComment specialMethods[]
1026+
class Bar {
1027+
/**
1028+
* The PHP5 constructor
1029+
*/
1030+
public function __construct() {
1031+
1032+
}
1033+
}
1034+
1035+
// phpcs:set Squiz.Commenting.FunctionComment specialMethods[] ignored
1036+
/**
1037+
* Should be ok
1038+
*/
1039+
public function ignored() {
1040+
1041+
}
1042+
1043+
// phpcs:set Squiz.Commenting.FunctionComment specialMethods[] __construct,__destruct

src/Standards/Squiz/Tests/Commenting/FunctionCommentUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function getErrorList()
115115
997 => 1,
116116
1004 => 2,
117117
1006 => 1,
118+
1029 => 1,
118119
];
119120

120121
// Scalar type hints only work from PHP 7 onwards.

0 commit comments

Comments
 (0)