Skip to content

Commit e64106d

Browse files
committed
Use hasDeprecatedWellFormatted for Method Annotations
1 parent 82723b9 commit e64106d

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

Magento2/Sniffs/Annotation/MethodAnnotationStructureSniff.php

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento2\Sniffs\Annotation;
99

10+
use Magento2\Helpers\Commenting\PHPDocFormattingValidator;
1011
use PHP_CodeSniffer\Files\File;
1112
use PHP_CodeSniffer\Sniffs\Sniff;
1213

@@ -20,12 +21,18 @@ class MethodAnnotationStructureSniff implements Sniff
2021
*/
2122
private $annotationFormatValidator;
2223

24+
/**
25+
* @var PHPDocFormattingValidator
26+
*/
27+
private $PHPDocFormattingValidator;
28+
2329
/**
2430
* AnnotationStructureSniff constructor.
2531
*/
2632
public function __construct()
2733
{
2834
$this->annotationFormatValidator = new AnnotationFormatValidator();
35+
$this->PHPDocFormattingValidator = new PHPDocFormattingValidator();
2936
}
3037

3138
/**
@@ -45,6 +52,16 @@ public function process(File $phpcsFile, $stackPtr)
4552
{
4653
$tokens = $phpcsFile->getTokens();
4754
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, ($stackPtr), 0);
55+
56+
if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStartPtr, $tokens) !== true) {
57+
$phpcsFile->addWarning(
58+
'Motivation behind the added @deprecated tag MUST be explained. '
59+
. '@see tag MUST be used with reference to new implementation when code is deprecated '
60+
. 'and there is a new alternative.',
61+
$stackPtr,
62+
'InvalidDeprecatedTagUsage'
63+
);
64+
}
4865
$commentEndPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_CLOSE_TAG, ($stackPtr), 0);
4966
if (!$commentStartPtr) {
5067
$phpcsFile->addError('Comment block is missing', $stackPtr, 'MethodArguments');

Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.inc

+63
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,67 @@ class MethodAnnotationFixture
306306
{
307307
return $start === $length;
308308
}
309+
310+
/**
311+
* This is a well-formed deprecated function
312+
*
313+
* @deprecated can be used in this context
314+
* @see is ok here
315+
*/
316+
public function deprecated(): bool
317+
{
318+
return true;
319+
}
320+
321+
/**
322+
* This deprecated function is incorrect since it only contains the @deprecated tag
323+
*
324+
* @deprecated
325+
*/
326+
public function incorrectlyDeprecated()
327+
{
328+
return false;
329+
}
330+
331+
/**
332+
* This deprecated function is incorrect since it only contains the @deprecated tag
333+
*
334+
* @deprecated Should not be used
335+
*/
336+
public function incorrectAsWell()
337+
{
338+
return false;
339+
}
340+
341+
/**
342+
* This deprecated function is incorrect since the @see tag does not have extra info
343+
*
344+
* @deprecated
345+
* @see
346+
*/
347+
public function anotherOne()
348+
{
349+
return false;
350+
}
351+
352+
/**
353+
* This deprecated function is incorrect since the @see tag does not have extra info
354+
*
355+
* @deprecated Should not be used
356+
* @see
357+
*/
358+
public function yetAnotherOne()
359+
{
360+
return false;
361+
}
362+
363+
/**
364+
* This function is correct since the @see tag can be used without the @deprecated tag
365+
*
366+
* @see Magento\Framework\NewHandler
367+
*/
368+
public function correctUseOfSee()
369+
{
370+
return true;
371+
}
309372
}

Magento2/Tests/Annotation/MethodAnnotationStructureUnitTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public function getErrorList()
4646
*/
4747
public function getWarningList()
4848
{
49-
return [];
49+
return [
50+
326 => 1,
51+
336 => 1,
52+
347 => 1,
53+
358 => 1
54+
];
5055
}
5156
}

0 commit comments

Comments
 (0)