Skip to content

Commit dba4d2b

Browse files
authored
Merge pull request #183 from magento-commerce/develop
Version 23
2 parents 29e6052 + d0298c4 commit dba4d2b

15 files changed

+229
-79
lines changed

Magento2/Helpers/Commenting/PHPDocFormattingValidator.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,11 @@ public function hasDeprecatedWellFormatted($commentStartPtr, $tokens)
121121
if ($deprecatedPtr === -1) {
122122
return true;
123123
}
124-
125-
$seeTagRequired = false;
126-
if ($tokens[$deprecatedPtr + 2]['code'] !== T_DOC_COMMENT_STRING) {
127-
$seeTagRequired = true;
128-
}
129124
$seePtr = $this->getTagPosition('@see', $commentStartPtr, $tokens);
130125
if ($seePtr === -1) {
131-
return !$seeTagRequired;
126+
return false;
132127
}
128+
133129
return $tokens[$seePtr + 2]['code'] === T_DOC_COMMENT_STRING;
134130
}
135131

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/Sniffs/Commenting/ClassPropertyPHPDocFormattingSniff.php

+9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public function processMemberVar(File $phpcsFile, $stackPtr)
6767
}
6868

6969
$commentStart = $tokens[$commentEnd]['comment_opener'];
70+
if ($this->PHPDocFormattingValidator->hasDeprecatedWellFormatted($commentStart, $tokens) !== true) {
71+
$phpcsFile->addWarning(
72+
'Motivation behind the added @deprecated tag MUST be explained. '
73+
. '@see tag MUST be used with reference to new implementation when code is deprecated '
74+
. 'and there is a new alternative.',
75+
$stackPtr,
76+
'InvalidDeprecatedTagUsage'
77+
);
78+
}
7079
$varAnnotationPosition = null;
7180
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
7281
if ($tokens[$tag]['content'] === '@var') {

Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php

+38-38
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
4040
'^chdir$' => null,
4141
'^chgrp$' => null,
4242
'^chmod$' => 'Magento\Framework\Filesystem\DriverInterface::changePermissions()
43-
or Magento\Framework\Filesystem\DriverInterface::changePermissionsRecursively()',
43+
or Magento\Framework\Filesystem\DriverInterface::changePermissionsRecursively',
4444
'^chown$' => null,
4545
'^chroot$' => null,
4646
'^com_load_typelib$' => null,
47-
'^copy$' => 'Magento\Framework\Filesystem\DriverInterface::copy()',
47+
'^copy$' => 'Magento\Framework\Filesystem\DriverInterface::copy',
4848
'^curl_.*$' => null,
4949
'^cyrus_connect$' => null,
5050
'^dba_.*$' => null,
@@ -54,21 +54,21 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
5454
'^dcngettext$' => null,
5555
'^dgettext$' => null,
5656
'^dio_.*$' => null,
57-
'^dirname$' => 'Magento\Framework\Filesystem\DriverInterface::getParentDirectory()',
57+
'^dirname$' => 'Magento\Framework\Filesystem\DriverInterface::getParentDirectory',
5858
'^dngettext$' => null,
5959
'^domxml_.*$' => null,
6060
'^fbsql_.*$' => null,
6161
'^fbsql$' => null,
6262
'^fdf_add_doc_javascript$' => null,
6363
'^fdf_open$' => null,
64-
'^fopen$' => 'Magento\Framework\Filesystem\DriverInterface::fileOpen()',
65-
'^fclose$' => 'Magento\Framework\Filesystem\DriverInterface::fileClose()',
66-
'^fsockopen$' => 'Magento\Framework\Filesystem\Driver\Http::open()',
64+
'^fopen$' => 'Magento\Framework\Filesystem\DriverInterface::fileOpen',
65+
'^fclose$' => 'Magento\Framework\Filesystem\DriverInterface::fileClose',
66+
'^fsockopen$' => 'Magento\Framework\Filesystem\Driver\Http::open',
6767
'^ftp_.*$' => null,
68-
'^fwrite$' => 'Magento\Framework\Filesystem\DriverInterface::fileWrite()',
69-
'^fputs$' => 'Magento\Framework\Filesystem\DriverInterface::fileWrite()',
70-
'^gettext$' => 'Magento\Framework\Translate\AdapterInterface::translate()',
71-
'^_$' => 'Magento\Framework\Translate\AdapterInterface::translate()',
68+
'^fwrite$' => 'Magento\Framework\Filesystem\DriverInterface::fileWrite',
69+
'^fputs$' => 'Magento\Framework\Filesystem\DriverInterface::fileWrite',
70+
'^gettext$' => 'Magento\Framework\Translate\AdapterInterface::translate',
71+
'^_$' => 'Magento\Framework\Translate\AdapterInterface::translate',
7272
'^gz.*$' => null,
7373
'^header$' => null,
7474
'^highlight_file$' => null,
@@ -84,7 +84,7 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
8484
'^link$' => null,
8585
'^mail$' => null,
8686
'^mb_send_mail$' => null,
87-
'^mkdir$' => 'Magento\Framework\Filesystem\DriverInterface::createDirectory()',
87+
'^mkdir$' => 'Magento\Framework\Filesystem\DriverInterface::createDirectory',
8888
'^move_uploaded_file$' => null,
8989
'^msession_.*$' => null,
9090
'^msg_send$' => null,
@@ -102,7 +102,7 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
102102
'^parse_str$' => null,
103103
'^parse_url$' => null,
104104
'^parsekit_compile_string$' => null,
105-
'^pathinfo$' => 'Magento\Framework\Filesystem\Io\File::getPathInfo()',
105+
'^pathinfo$' => 'Magento\Framework\Filesystem\Io\File::getPathInfo',
106106
'^pcntl_.*$' => null,
107107
'^posix_.*$' => null,
108108
'^pfpro_.*$' => null,
@@ -112,30 +112,30 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
112112
'^print_r$' => null,
113113
'^printf$' => null,
114114
'^putenv$' => null,
115-
'^readfile$' => 'Magento\Framework\Filesystem\DriverInterface::fileRead()',
115+
'^readfile$' => 'Magento\Framework\Filesystem\DriverInterface::fileRead',
116116
'^readgzfile$' => null,
117-
'^readline$' => 'Magento\Framework\Filesystem\DriverInterface::fileReadLine()',
117+
'^readline$' => 'Magento\Framework\Filesystem\DriverInterface::fileReadLine',
118118
'^readlink$' => null,
119119
'^register_shutdown_function$' => null,
120120
'^register_tick_function$' => null,
121-
'^rename$' => 'Magento\Framework\Filesystem\DriverInterface::rename()',
122-
'^rmdir$' => 'Magento\Framework\Filesystem\DriverInterface::deleteDirectory()',
121+
'^rename$' => 'Magento\Framework\Filesystem\DriverInterface::rename',
122+
'^rmdir$' => 'Magento\Framework\Filesystem\DriverInterface::deleteDirectory',
123123
'^scandir$' => null,
124124
'^session_.*$' => null,
125125
'^set_include_path$' => null,
126126
'^ini_set$' => null,
127127
'^ini_alter$' => null,
128128
'^set_time_limit$' => null,
129129
'^setcookie$' => null,
130-
'^setlocale$' => 'Magento\Framework\Translate\AdapterInterface::setlocale()',
130+
'^setlocale$' => 'Magento\Framework\Translate\AdapterInterface::setLocale',
131131
'^setrawcookie$' => null,
132132
'^sleep$' => null,
133133
'^socket_.*$' => null,
134134
'^stream_.*$' => null,
135135
'^sybase_.*$' => null,
136-
'^symlink$' => 'Magento\Framework\Filesystem\DriverInterface::symlink()',
136+
'^symlink$' => 'Magento\Framework\Filesystem\DriverInterface::symlink',
137137
'^syslog$' => null,
138-
'^touch$' => 'Magento\Framework\Filesystem\DriverInterface::touch()',
138+
'^touch$' => 'Magento\Framework\Filesystem\DriverInterface::touch',
139139
'^trigger_error$' => null,
140140
'^unlink$' => null,
141141
'^vprintf$' => null,
@@ -158,7 +158,7 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
158158
'^fdf_.*$' => null,
159159
'^fget.*$' => null,
160160
'^fread$' => null,
161-
'^fflush$' => 'Magento\Framework\Filesystem\DriverInterface::fileFlush()',
161+
'^fflush$' => 'Magento\Framework\Filesystem\DriverInterface::fileFlush',
162162
'^get_browser$' => null,
163163
'^get_headers$' => null,
164164
'^get_meta_tags$' => null,
@@ -204,23 +204,23 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
204204
'^gettype$' => null,
205205
'^var_dump$' => null,
206206
'^tempnam$' => null,
207-
'^realpath$' => 'Magento\Framework\Filesystem\DriverInterface::getRealPath()',
207+
'^realpath$' => 'Magento\Framework\Filesystem\DriverInterface::getRealPath',
208208
'^linkinfo$' => null,
209-
'^lstat$' => 'Magento\Framework\Filesystem\DriverInterface::stat()',
209+
'^lstat$' => 'Magento\Framework\Filesystem\DriverInterface::stat',
210210
'^stat$' => null,
211211
'^lchgrp$' => null,
212212
'^lchown$' => null,
213-
'^is_dir$' => 'Magento\Framework\Filesystem\DriverInterface::isDirectory()',
213+
'^is_dir$' => 'Magento\Framework\Filesystem\DriverInterface::isDirectory',
214214
'^is_executable$' => null,
215-
'^is_file$' => 'Magento\Framework\Filesystem\DriverInterface::isFile()',
215+
'^is_file$' => 'Magento\Framework\Filesystem\DriverInterface::isFile',
216216
'^is_link$' => null,
217-
'^is_readable$' => 'Magento\Framework\Filesystem\DriverInterface::isReadable()',
218-
'^is_writable$' => 'Magento\Framework\Filesystem\DriverInterface::isWritable()',
219-
'^is_writeable$' => 'Magento\Framework\Filesystem\DriverInterface::isWritable()',
217+
'^is_readable$' => 'Magento\Framework\Filesystem\DriverInterface::isReadable',
218+
'^is_writable$' => 'Magento\Framework\Filesystem\DriverInterface::isWritable',
219+
'^is_writeable$' => 'Magento\Framework\Filesystem\DriverInterface::isWritable',
220220
'^is_uploaded_file$' => null,
221-
'^glob$' => 'Magento\Framework\Filesystem\Glob::glob()',
221+
'^glob$' => 'Magento\Framework\Filesystem\Glob::glob',
222222
'^ssh2_.*$' => null,
223-
'^delete$' => 'Magento\Framework\Filesystem\DriverInterface::deleteFile()',
223+
'^delete$' => 'Magento\Framework\Filesystem\DriverInterface::deleteFile',
224224
'^file.*$' => null,
225225
'^chop$' => 'rtrim()',
226226
'^sizeof$' => 'count()',
@@ -229,15 +229,15 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
229229
'^strval$' => '(string) construction',
230230
'^htmlspecialchars$' => '\Magento\Framework\Escaper->escapeHtml',
231231
'^getimagesize$' => 'getimagesizefromstring',
232-
'^file_exists$' => 'Magento\Framework\Filesystem\DriverInterface::isExists()',
233-
'^file_get_contents$' => 'Magento\Framework\Filesystem\DriverInterface::fileGetContents()',
234-
'^file_put_contents$' => 'Magento\Framework\Filesystem\DriverInterface::filePutContents()',
235-
'^fgetcsv$' => 'Magento\Framework\Filesystem\DriverInterface::fileGetCsv()',
236-
'^fputcsv$' => 'Magento\Framework\Filesystem\DriverInterface::filePutCsv()',
237-
'^ftell$' => 'Magento\Framework\Filesystem\DriverInterface::fileTell()',
238-
'^fseek$' => 'Magento\Framework\Filesystem\DriverInterface::fileSeek()',
239-
'^feof$' => 'Magento\Framework\Filesystem\DriverInterface::endOfFile()',
240-
'^flock$' => 'Magento\Framework\Filesystem\DriverInterface::fileLock()',
232+
'^file_exists$' => 'Magento\Framework\Filesystem\DriverInterface::isExists',
233+
'^file_get_contents$' => 'Magento\Framework\Filesystem\DriverInterface::fileGetContents',
234+
'^file_put_contents$' => 'Magento\Framework\Filesystem\DriverInterface::filePutContents',
235+
'^fgetcsv$' => 'Magento\Framework\Filesystem\DriverInterface::fileGetCsv',
236+
'^fputcsv$' => 'Magento\Framework\Filesystem\DriverInterface::filePutCsv',
237+
'^ftell$' => 'Magento\Framework\Filesystem\DriverInterface::fileTell',
238+
'^fseek$' => 'Magento\Framework\Filesystem\DriverInterface::fileSeek',
239+
'^feof$' => 'Magento\Framework\Filesystem\DriverInterface::endOfFile',
240+
'^flock$' => 'Magento\Framework\Filesystem\DriverInterface::fileLock',
241241
'^date_sunrise$' => 'date_sun_info',
242242
'^date_sunset$' => 'date_sun_info',
243243
'^strptime$' => 'date_parse_from_format',

Magento2/Sniffs/NamingConvention/ReservedWordsSniff.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ReservedWordsSniff implements Sniff
3434
'object' => '7',
3535
'mixed' => '7',
3636
'numeric' => '7',
37+
'match' => '8'
3738
];
3839

3940
private const CLASS_ERROR_CODE = 'ForbiddenAsClassName';

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
}

Magento2/Tests/Commenting/ClassAndInterfacePHPDocFormattingUnitTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public function getWarningList($testFile = '')
3333
65 => 1,
3434
66 => 1,
3535
101 => 1,
36+
109 => 1,
3637
118 => 1,
37-
127 => 1,
38+
127 => 1
3839
];
3940
}
4041
}

0 commit comments

Comments
 (0)