Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dd788de

Browse filesBrowse files
committed
Merge branch 'develop' of github.com:magento/magento-coding-standard into 75_linelengthsniff_keyword
# Conflicts: # Magento2/Tests/Files/LineLengthUnitTest.php
2 parents 75b60e9 + 1bdb24d commit dd788de
Copy full SHA for dd788de
Expand file treeCollapse file tree

34 files changed

+495
-129
lines changed
+121Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento2\Sniffs\Commenting;
8+
9+
use PHP_CodeSniffer\Sniffs\Sniff;
10+
use PHP_CodeSniffer\Files\File;
11+
12+
/**
13+
* Detects PHPDoc formatting for classes and interfaces.
14+
*/
15+
class ClassAndInterfacePHPDocFormattingSniff implements Sniff
16+
{
17+
/**
18+
* @var PHPDocFormattingValidator
19+
*/
20+
private $PHPDocFormattingValidator;
21+
22+
/**
23+
* @var string[] List of tags that can not be used in comments
24+
*/
25+
public $forbiddenTags = [
26+
'@category',
27+
'@package',
28+
'@subpackage'
29+
];
30+
31+
/**
32+
* Helper initialisation
33+
*/
34+
public function __construct()
35+
{
36+
$this->PHPDocFormattingValidator = new PHPDocFormattingValidator();
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function register()
43+
{
44+
return [
45+
T_CLASS,
46+
T_INTERFACE
47+
];
48+
}
49+
50+
/**
51+
* @inheritDoc
52+
*/
53+
public function process(File $phpcsFile, $stackPtr)
54+
{
55+
$tokens = $phpcsFile->getTokens();
56+
57+
$namePtr = $phpcsFile->findNext(T_STRING, $stackPtr + 1, null, false, null, true);
58+
59+
$commentStartPtr = $phpcsFile->findPrevious(
60+
[
61+
T_WHITESPACE,
62+
T_DOC_COMMENT_STAR,
63+
T_DOC_COMMENT_WHITESPACE,
64+
T_DOC_COMMENT_TAG,
65+
T_DOC_COMMENT_STRING,
66+
T_DOC_COMMENT_CLOSE_TAG
67+
],
68+
$stackPtr - 1,
69+
null,
70+
true,
71+
null,
72+
true
73+
);
74+
75+
if ($tokens[$commentStartPtr]['code'] !== T_DOC_COMMENT_OPEN_TAG) {
76+
return;
77+
}
78+
79+
if ($this->PHPDocFormattingValidator->providesMeaning($namePtr, $commentStartPtr, $tokens) !== true) {
80+
$phpcsFile->addWarning(
81+
sprintf(
82+
'%s description should contain additional information beyond the name already supplies.',
83+
ucfirst($tokens[$stackPtr]['content'])
84+
),
85+
$stackPtr,
86+
'InvalidDescription'
87+
);
88+
}
89+
90+
$this->validateTags($phpcsFile, $commentStartPtr, $tokens);
91+
}
92+
93+
/**
94+
* Validates that forbidden tags are not used in comment
95+
*
96+
* @param File $phpcsFile
97+
* @param int $commentStartPtr
98+
* @param array $tokens
99+
* @return bool
100+
*/
101+
private function validateTags(File $phpcsFile, $commentStartPtr, $tokens)
102+
{
103+
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
104+
105+
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
106+
if ($tokens[$i]['code'] !== T_DOC_COMMENT_TAG) {
107+
continue;
108+
}
109+
110+
if (in_array($tokens[$i]['content'], $this->forbiddenTags) === true) {
111+
$phpcsFile->addWarning(
112+
sprintf('Tag %s MUST NOT be used.', $tokens[$i]['content']),
113+
$i,
114+
'ForbiddenTags'
115+
);
116+
}
117+
}
118+
119+
return false;
120+
}
121+
}

‎Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Commenting/ConstantsPHPDocFormattingSniff.php
+19-30Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@
1313
*/
1414
class ConstantsPHPDocFormattingSniff implements Sniff
1515
{
16+
/**
17+
* @var PHPDocFormattingValidator
18+
*/
19+
private $PHPDocFormattingValidator;
20+
21+
/**
22+
* Helper initialisation
23+
*/
24+
public function __construct()
25+
{
26+
$this->PHPDocFormattingValidator = new PHPDocFormattingValidator();
27+
}
28+
1629
/**
1730
* @inheritDoc
1831
*/
@@ -45,42 +58,18 @@ public function process(File $phpcsFile, $stackPtr)
4558
null,
4659
true
4760
);
48-
$constName = strtolower(trim($tokens[$constNamePtr]['content'], " '\""));
4961

5062
$commentStartPtr = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1, null, false, null, true);
5163
if ($commentStartPtr === false) {
5264
return;
5365
}
5466

55-
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
56-
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
57-
$token = $tokens[$i];
58-
59-
// Not an interesting string
60-
if ($token['code'] !== T_DOC_COMMENT_STRING) {
61-
continue;
62-
}
63-
64-
// Comment is the same as constant name
65-
$docComment = trim(strtolower($token['content']), ',.');
66-
if ($docComment === $constName) {
67-
continue;
68-
}
69-
70-
// Comment is exactly the same as constant name
71-
$docComment = str_replace(' ', '_', $docComment);
72-
if ($docComment === $constName) {
73-
continue;
74-
}
75-
76-
// We have found at lease one meaningful line in comment description
77-
return;
67+
if ($this->PHPDocFormattingValidator->providesMeaning($constNamePtr, $commentStartPtr, $tokens) !== true) {
68+
$phpcsFile->addWarning(
69+
'Constants must have short description if they add information beyond what the constant name supplies.',
70+
$stackPtr,
71+
'MissingConstantPHPDoc'
72+
);
7873
}
79-
80-
$phpcsFile->addWarning(
81-
'Constants must have short description if they add information beyond what the constant name supplies.',
82-
$stackPtr,
83-
'MissingConstantPHPDoc'
84-
);
8574
}
8675
}
+74Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento2\Sniffs\Commenting;
8+
9+
/**
10+
* Helper class for common DocBlock validations
11+
*/
12+
class PHPDocFormattingValidator
13+
{
14+
/**
15+
* Determines if the comment identified by $commentStartPtr provides additional meaning to origin at $namePtr
16+
*
17+
* @param int $namePtr
18+
* @param int $commentStartPtr
19+
* @param array $tokens
20+
* @return bool
21+
*/
22+
public function providesMeaning($namePtr, $commentStartPtr, $tokens)
23+
{
24+
$commentCloserPtr = $tokens[$commentStartPtr]['comment_closer'];
25+
$name = strtolower(str_replace([' ', '"', '_'], '', $tokens[$namePtr]['content']));
26+
27+
$hasTags = false;
28+
$hasDescription = false;
29+
30+
for ($i = $commentStartPtr; $i <= $commentCloserPtr; $i++) {
31+
$token = $tokens[$i];
32+
33+
// Important, but not the string we are looking for
34+
if ($token['code'] === T_DOC_COMMENT_TAG) {
35+
$hasTags = true;
36+
continue;
37+
}
38+
39+
// Not an interesting string
40+
if ($token['code'] !== T_DOC_COMMENT_STRING) {
41+
continue;
42+
}
43+
44+
// Wrong kind of string
45+
if ($tokens[$i - 2]['code'] === T_DOC_COMMENT_TAG) {
46+
continue;
47+
}
48+
49+
$hasDescription = true;
50+
51+
// Comment is the same as the origin name
52+
$docComment = str_replace(['_', ' ', '.', ','], '', strtolower($token['content']));
53+
if ($docComment === $name) {
54+
continue;
55+
}
56+
57+
// Only difference is word Class or Interface
58+
$docComment = str_replace(['class', 'interface'], '', $docComment);
59+
if ($docComment === $name) {
60+
continue;
61+
}
62+
63+
// We have found at lease one meaningful line in comment description
64+
return true;
65+
}
66+
67+
// Contains nothing but the tags
68+
if ($hasTags === true && $hasDescription === false) {
69+
return true;
70+
}
71+
72+
return false;
73+
}
74+
}

‎Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php

Copy file name to clipboardExpand all lines: Magento2/Sniffs/Functions/DiscouragedFunctionSniff.php
+25-24Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
3939
'^call_user_func_array$' => null,
4040
'^chdir$' => null,
4141
'^chgrp$' => null,
42-
'^chmod$' => null,
42+
'^chmod$' => 'Magento\Framework\Filesystem\DriverInterface::changePermissions()
43+
or Magento\Framework\Filesystem\DriverInterface::changePermissionsRecursively()',
4344
'^chown$' => null,
4445
'^chroot$' => null,
4546
'^com_load_typelib$' => null,
46-
'^copy$' => null,
47+
'^copy$' => 'Magento\Framework\Filesystem\DriverInterface::copy()',
4748
'^curl_.*$' => null,
4849
'^cyrus_connect$' => null,
4950
'^dba_.*$' => null,
@@ -53,17 +54,17 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
5354
'^dcngettext$' => null,
5455
'^dgettext$' => null,
5556
'^dio_.*$' => null,
56-
'^dirname$' => null,
57+
'^dirname$' => 'Magento\Framework\Filesystem\DriverInterface::getParentDirectory()',
5758
'^dngettext$' => null,
5859
'^domxml_.*$' => null,
5960
'^fbsql_.*$' => null,
6061
'^fdf_add_doc_javascript$' => null,
6162
'^fdf_open$' => null,
62-
'^fopen$' => null,
63-
'^fclose$' => null,
63+
'^fopen$' => 'Magento\Framework\Filesystem\DriverInterface::fileOpen()',
64+
'^fclose$' => 'Magento\Framework\Filesystem\DriverInterface::fileClose()',
6465
'^fsockopen$' => null,
6566
'^ftp_.*$' => null,
66-
'^fwrite$' => null,
67+
'^fwrite$' => 'Magento\Framework\Filesystem\DriverInterface::fileWrite()',
6768
'^gettext$' => null,
6869
'^gz.*$' => null,
6970
'^header$' => null,
@@ -79,7 +80,7 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
7980
'^link$' => null,
8081
'^mail$' => null,
8182
'^mb_send_mail$' => null,
82-
'^mkdir$' => null,
83+
'^mkdir$' => 'Magento\Framework\Filesystem\DriverInterface::createDirectory()',
8384
'^move_uploaded_file$' => null,
8485
'^msession_.*$' => null,
8586
'^msg_send$' => null,
@@ -96,7 +97,7 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
9697
'^parse_str$' => null,
9798
'^parse_url$' => null,
9899
'^parsekit_compile_string$' => null,
99-
'^pathinfo$' => null,
100+
'^pathinfo$' => 'Magento\Framework\Filesystem\Io\File::getPathInfo()',
100101
'^pcntl_.*$' => null,
101102
'^posix_.*$' => null,
102103
'^pfpro_.*$' => null,
@@ -106,14 +107,14 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
106107
'^print_r$' => null,
107108
'^printf$' => null,
108109
'^putenv$' => null,
109-
'^readfile$' => null,
110+
'^readfile$' => 'Magento\Framework\Filesystem\DriverInterface::fileRead()',
110111
'^readgzfile$' => null,
111-
'^readline$' => null,
112+
'^readline$' => 'Magento\Framework\Filesystem\DriverInterface::fileReadLine()',
112113
'^readlink$' => null,
113114
'^register_shutdown_function$' => null,
114115
'^register_tick_function$' => null,
115-
'^rename$' => null,
116-
'^rmdir$' => null,
116+
'^rename$' => 'Magento\Framework\Filesystem\DriverInterface::raname()',
117+
'^rmdir$' => 'Magento\Framework\Filesystem\DriverInterface::deleteDirectory()',
117118
'^scandir$' => null,
118119
'^session_.*$' => null,
119120
'^set_include_path$' => null,
@@ -126,9 +127,9 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
126127
'^socket_.*$' => null,
127128
'^stream_.*$' => null,
128129
'^sybase_.*$' => null,
129-
'^symlink$' => null,
130+
'^symlink$' => 'Magento\Framework\Filesystem\DriverInterface::symlink()',
130131
'^syslog$' => null,
131-
'^touch$' => null,
132+
'^touch$' => 'Magento\Framework\Filesystem\DriverInterface::touch()',
132133
'^trigger_error$' => null,
133134
'^unlink$' => null,
134135
'^vprintf$' => null,
@@ -149,7 +150,7 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
149150
'^fdf_.*$' => null,
150151
'^fget.*$' => null,
151152
'^fread$' => null,
152-
'^fflush$' => null,
153+
'^fflush$' => 'Magento\Framework\Filesystem\DriverInterface::fileFlush()',
153154
'^get_browser$' => null,
154155
'^get_headers$' => null,
155156
'^get_meta_tags$' => null,
@@ -194,24 +195,24 @@ class DiscouragedFunctionSniff extends ForbiddenFunctionsSniff
194195
'^gettype$' => null,
195196
'^var_dump$' => null,
196197
'^tempnam$' => null,
197-
'^realpath$' => null,
198+
'^realpath$' => 'Magento\Framework\Filesystem\DriverInterface::getRealPath()',
198199
'^linkinfo$' => null,
199-
'^lstat$' => null,
200+
'^lstat$' => 'Magento\Framework\Filesystem\DriverInterface::stat()',
200201
'^stat$' => null,
201202
'^lchgrp$' => null,
202203
'^lchown$' => null,
203204
'^show_source$' => null,
204-
'^is_dir$' => null,
205+
'^is_dir$' => 'Magento\Framework\Filesystem\DriverInterface::isDirectory()',
205206
'^is_executable$' => null,
206-
'^is_file$' => null,
207+
'^is_file$' => 'Magento\Framework\Filesystem\DriverInterface::isFile()',
207208
'^is_link$' => null,
208-
'^is_readable$' => null,
209-
'^is_writable$' => null,
210-
'^is_writeable$' => null,
209+
'^is_readable$' => 'Magento\Framework\Filesystem\DriverInterface::isReadable()',
210+
'^is_writable$' => 'Magento\Framework\Filesystem\DriverInterface::isWritable()',
211+
'^is_writeable$' => 'Magento\Framework\Filesystem\DriverInterface::isWritable()',
211212
'^is_uploaded_file$' => null,
212-
'^glob$' => null,
213+
'^glob$' => 'Magento\Framework\Filesystem\Glob::glob()',
213214
'^ssh2_.*$' => null,
214-
'^delete$' => null,
215+
'^delete$' => 'Magento\Framework\Filesystem\DriverInterface::deleteFile()',
215216
'^file.*$' => null,
216217
'^chop$' => 'rtrim()',
217218
'^sizeof$' => 'count()',

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.