Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit 443720d

Browse files
committed
Upgraded PHP Code sniff to 2.8.1, WPCS to 0.11
1 parent c24882a commit 443720d

File tree

218 files changed

+9125
-2599
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

218 files changed

+9125
-2599
lines changed

justcoded/JustcodedWordpress/Sniffs/NamingConventions/ValidVariableNameSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class JustcodedWordpress_Sniffs_NamingConventions_ValidVariableNameSniff extends
3333
*
3434
* @var string[]
3535
*/
36-
public $customVariablesWhitelist = array(
36+
public $customPropertiesWhitelist = array(
3737
'SLUG',
3838
'TITLE',
3939
);

justcoded/JustcodedWordpress/ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<rule ref="WordPress-Core">
99
<exclude name="WordPress.WP.I18n.NonSingularStringLiteralDomain"/>
10+
<exclude name="WordPress.WP.I18n.MissingTranslatorsComment"/>
1011
<exclude name="WordPress.WP.I18n.MismatchedPlaceholders"/>
1112
<exclude name="WordPress.WP.I18n.MissingSingularPlaceholders"/>
1213

phpcs/.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
/CodeSniffer/Standards/Zend/Tests export-ignore
99
.travis.yml export-ignore
1010
package.xml export-ignore
11+
phpunit.xml.dist export-ignore
12+
php5-testingConfig.ini export-ignore
13+
php7-testingConfig.ini export-ignore

phpcs/CodeSniffer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class PHP_CodeSniffer
7373
*
7474
* @var string
7575
*/
76-
const VERSION = '2.7.1';
76+
const VERSION = '2.8.1';
7777

7878
/**
7979
* Package stability; either stable, beta or alpha.

phpcs/CodeSniffer/CLI.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,7 @@ public function process($values=array())
10131013
$this->printUsage();
10141014
exit(2);
10151015
} else {
1016+
$this->values['stdin'] = $fileContents;
10161017
$phpcs->processFile('STDIN', $fileContents);
10171018
}
10181019
}

phpcs/CodeSniffer/File.php

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,10 @@ public function start($contents=null)
602602
// If short open tags are off but the file being checked uses
603603
// short open tags, the whole content will be inline HTML
604604
// and nothing will be checked. So try and handle this case.
605-
if ($foundCode === false && $this->tokenizerType === 'PHP') {
605+
// We don't show this error for STDIN because we can't be sure the content
606+
// actually came directly from the user. It could be something like
607+
// refs from a Git pre-push hook.
608+
if ($foundCode === false && $this->tokenizerType === 'PHP' && $this->_file !== 'STDIN') {
606609
$shortTags = (bool) ini_get('short_open_tag');
607610
if ($shortTags === false) {
608611
$error = 'No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them.';
@@ -1942,7 +1945,9 @@ private static function _recurseScopeMap(
19421945
// scope tokens. E.g., if (1) 1; 1 ? (1 ? 1 : 1) : 1;
19431946
// If an IF statement below this one has an opener but no
19441947
// keyword, the opener will be incorrectly assigned to this IF statement.
1945-
if (($currType === T_IF || $currType === T_ELSE)
1948+
// The same case also applies to USE statements, which don't have to have
1949+
// openers, so a following USE statement can cause an incorrect brace match.
1950+
if (($currType === T_IF || $currType === T_ELSE || $currType === T_USE)
19461951
&& $opener === null
19471952
&& $tokens[$i]['code'] === T_SEMICOLON
19481953
) {
@@ -2645,25 +2650,23 @@ private static function _createLevelMap(&$tokens, $tokenizer, $eolChar)
26452650

26462651

26472652
/**
2648-
* Returns the declaration names for T_CLASS, T_INTERFACE and T_FUNCTION tokens.
2653+
* Returns the declaration names for classes, interfaces, and functions.
26492654
*
26502655
* @param int $stackPtr The position of the declaration token which
26512656
* declared the class, interface or function.
26522657
*
26532658
* @return string|null The name of the class, interface or function.
2654-
* or NULL if the function is a closure.
2659+
* or NULL if the function or class is anonymous.
26552660
* @throws PHP_CodeSniffer_Exception If the specified token is not of type
2656-
* T_FUNCTION, T_CLASS or T_INTERFACE.
2661+
* T_FUNCTION, T_CLASS, T_ANON_CLASS,
2662+
* or T_INTERFACE.
26572663
*/
26582664
public function getDeclarationName($stackPtr)
26592665
{
26602666
$tokenCode = $this->_tokens[$stackPtr]['code'];
2661-
if ($tokenCode !== T_FUNCTION
2662-
&& $tokenCode !== T_CLASS
2663-
&& $tokenCode !== T_INTERFACE
2664-
&& $tokenCode !== T_TRAIT
2665-
) {
2666-
throw new PHP_CodeSniffer_Exception('Token type "'.$this->_tokens[$stackPtr]['type'].'" is not T_FUNCTION, T_CLASS, T_INTERFACE or T_TRAIT');
2667+
2668+
if ($tokenCode === T_ANON_CLASS) {
2669+
return null;
26672670
}
26682671

26692672
if ($tokenCode === T_FUNCTION
@@ -2672,6 +2675,14 @@ public function getDeclarationName($stackPtr)
26722675
return null;
26732676
}
26742677

2678+
if ($tokenCode !== T_FUNCTION
2679+
&& $tokenCode !== T_CLASS
2680+
&& $tokenCode !== T_INTERFACE
2681+
&& $tokenCode !== T_TRAIT
2682+
) {
2683+
throw new PHP_CodeSniffer_Exception('Token type "'.$this->_tokens[$stackPtr]['type'].'" is not T_FUNCTION, T_CLASS, T_INTERFACE or T_TRAIT');
2684+
}
2685+
26752686
$content = null;
26762687
for ($i = $stackPtr; $i < $this->numTokens; $i++) {
26772688
if ($this->_tokens[$i]['code'] === T_STRING) {
@@ -2731,46 +2742,53 @@ public function isAnonymousFunction($stackPtr)
27312742

27322743

27332744
/**
2734-
* Returns the method parameters for the specified T_FUNCTION token.
2745+
* Returns the method parameters for the specified function token.
27352746
*
27362747
* Each parameter is in the following format:
27372748
*
27382749
* <code>
27392750
* 0 => array(
2751+
* 'token' => int, // The position of the var in the token stack.
27402752
* 'name' => '$var', // The variable name.
2741-
* 'pass_by_reference' => false, // Passed by reference.
2742-
* 'type_hint' => string, // Type hint for array or custom type
2753+
* 'content' => string, // The full content of the variable definition.
2754+
* 'pass_by_reference' => boolean, // Is the variable passed by reference?
2755+
* 'type_hint' => string, // The type hint for the variable.
2756+
* 'nullable_type' => boolean, // Is the variable using a nullable type?
27432757
* )
27442758
* </code>
27452759
*
27462760
* Parameters with default values have an additional array index of
27472761
* 'default' with the value of the default as a string.
27482762
*
2749-
* @param int $stackPtr The position in the stack of the T_FUNCTION token
2763+
* @param int $stackPtr The position in the stack of the function token
27502764
* to acquire the parameters for.
27512765
*
27522766
* @return array
27532767
* @throws PHP_CodeSniffer_Exception If the specified $stackPtr is not of
2754-
* type T_FUNCTION.
2768+
* type T_FUNCTION or T_CLOSURE.
27552769
*/
27562770
public function getMethodParameters($stackPtr)
27572771
{
2758-
if ($this->_tokens[$stackPtr]['code'] !== T_FUNCTION) {
2759-
throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION');
2772+
if ($this->_tokens[$stackPtr]['code'] !== T_FUNCTION
2773+
&& $this->_tokens[$stackPtr]['code'] !== T_CLOSURE
2774+
) {
2775+
throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION or T_CLOSURE');
27602776
}
27612777

27622778
$opener = $this->_tokens[$stackPtr]['parenthesis_opener'];
27632779
$closer = $this->_tokens[$stackPtr]['parenthesis_closer'];
27642780

27652781
$vars = array();
27662782
$currVar = null;
2783+
$paramStart = ($opener + 1);
27672784
$defaultStart = null;
27682785
$paramCount = 0;
27692786
$passByReference = false;
27702787
$variableLength = false;
27712788
$typeHint = '';
2789+
$nullableType = false;
27722790

2773-
for ($i = ($opener + 1); $i <= $closer; $i++) {
2791+
for ($i = $paramStart; $i <= $closer; $i++) {
27742792
// Check to see if this token has a parenthesis or bracket opener. If it does
27752793
// it's likely to be an array which might have arguments in it. This
27762794
// could cause problems in our parsing below, so lets just skip to the
@@ -2801,7 +2819,15 @@ public function getMethodParameters($stackPtr)
28012819
break;
28022820
case T_ARRAY_HINT:
28032821
case T_CALLABLE:
2804-
$typeHint = $this->_tokens[$i]['content'];
2822+
$typeHint .= $this->_tokens[$i]['content'];
2823+
break;
2824+
case T_SELF:
2825+
case T_PARENT:
2826+
case T_STATIC:
2827+
// Self is valid, the others invalid, but were probably intended as type hints.
2828+
if (isset($defaultStart) === false) {
2829+
$typeHint .= $this->_tokens[$i]['content'];
2830+
}
28052831
break;
28062832
case T_STRING:
28072833
// This is a string, so it may be a type hint, but it could
@@ -2838,6 +2864,12 @@ public function getMethodParameters($stackPtr)
28382864
$typeHint .= $this->_tokens[$i]['content'];
28392865
}
28402866
break;
2867+
case T_NULLABLE:
2868+
if ($defaultStart === null) {
2869+
$nullableType = true;
2870+
$typeHint .= $this->_tokens[$i]['content'];
2871+
}
2872+
break;
28412873
case T_CLOSE_PARENTHESIS:
28422874
case T_COMMA:
28432875
// If it's null, then there must be no parameters for this
@@ -2846,26 +2878,27 @@ public function getMethodParameters($stackPtr)
28462878
continue;
28472879
}
28482880

2849-
$vars[$paramCount] = array();
2850-
$vars[$paramCount]['name'] = $this->_tokens[$currVar]['content'];
2881+
$vars[$paramCount] = array();
2882+
$vars[$paramCount]['token'] = $currVar;
2883+
$vars[$paramCount]['name'] = $this->_tokens[$currVar]['content'];
2884+
$vars[$paramCount]['content'] = trim($this->getTokensAsString($paramStart, ($i - $paramStart)));
28512885

28522886
if ($defaultStart !== null) {
2853-
$vars[$paramCount]['default']
2854-
= $this->getTokensAsString(
2855-
$defaultStart,
2856-
($i - $defaultStart)
2857-
);
2887+
$vars[$paramCount]['default'] = trim($this->getTokensAsString($defaultStart, ($i - $defaultStart)));
28582888
}
28592889

28602890
$vars[$paramCount]['pass_by_reference'] = $passByReference;
28612891
$vars[$paramCount]['variable_length'] = $variableLength;
28622892
$vars[$paramCount]['type_hint'] = $typeHint;
2893+
$vars[$paramCount]['nullable_type'] = $nullableType;
28632894

28642895
// Reset the vars, as we are about to process the next parameter.
28652896
$defaultStart = null;
2897+
$paramStart = ($i + 1);
28662898
$passByReference = false;
28672899
$variableLength = false;
28682900
$typeHint = '';
2901+
$nullableType = false;
28692902

28702903
$paramCount++;
28712904
break;
@@ -3000,6 +3033,7 @@ public function getMemberProperties($stackPtr)
30003033
$ptr = array_pop($conditions);
30013034
if (isset($this->_tokens[$ptr]) === false
30023035
|| ($this->_tokens[$ptr]['code'] !== T_CLASS
3036+
&& $this->_tokens[$ptr]['code'] !== T_ANON_CLASS
30033037
&& $this->_tokens[$ptr]['code'] !== T_TRAIT)
30043038
) {
30053039
if (isset($this->_tokens[$ptr]) === true
@@ -3683,7 +3717,9 @@ public function findExtendedClassName($stackPtr)
36833717
return false;
36843718
}
36853719

3686-
if ($this->_tokens[$stackPtr]['code'] !== T_CLASS) {
3720+
if ($this->_tokens[$stackPtr]['code'] !== T_CLASS
3721+
&& $this->_tokens[$stackPtr]['code'] !== T_ANON_CLASS
3722+
) {
36873723
return false;
36883724
}
36893725

@@ -3732,7 +3768,9 @@ public function findImplementedInterfaceNames($stackPtr)
37323768
return false;
37333769
}
37343770

3735-
if ($this->_tokens[$stackPtr]['code'] !== T_CLASS) {
3771+
if ($this->_tokens[$stackPtr]['code'] !== T_CLASS
3772+
&& $this->_tokens[$stackPtr]['code'] !== T_ANON_CLASS
3773+
) {
37363774
return false;
37373775
}
37383776

phpcs/CodeSniffer/Fixer.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,13 @@ public function generateDiff($filePath=null, $colors=true)
243243
$filePath = $this->_currentFile->getFilename();
244244
}
245245

246-
$cwd = getcwd().DIRECTORY_SEPARATOR;
247-
$filename = str_replace($cwd, '', $filePath);
246+
$cwd = getcwd().DIRECTORY_SEPARATOR;
247+
if (strpos($filePath, $cwd) === 0) {
248+
$filename = substr($filePath, strlen($cwd));
249+
} else {
250+
$filename = $filePath;
251+
}
252+
248253
$contents = $this->getContents();
249254

250255
if (function_exists('sys_get_temp_dir') === true) {
@@ -261,7 +266,9 @@ public function generateDiff($filePath=null, $colors=true)
261266

262267
// We must use something like shell_exec() because whitespace at the end
263268
// of lines is critical to diff files.
264-
$cmd = "diff -u -L\"$filename\" -LPHP_CodeSniffer \"$filename\" \"$tempName\"";
269+
$filename = escapeshellarg($filename);
270+
$cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\"";
271+
265272
$diff = shell_exec($cmd);
266273

267274
fclose($fixedFile);

phpcs/CodeSniffer/Reports/Gitblame.php

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,7 @@ protected function getBlameContent($filename)
8383
{
8484
$cwd = getcwd();
8585

86-
if (PHP_CODESNIFFER_VERBOSITY > 0) {
87-
echo 'Getting GIT blame info for '.basename($filename).'... ';
88-
}
89-
90-
$fileParts = explode(DIRECTORY_SEPARATOR, $filename);
91-
$found = false;
92-
$location = '';
93-
while (empty($fileParts) === false) {
94-
array_pop($fileParts);
95-
$location = implode($fileParts, DIRECTORY_SEPARATOR);
96-
if (is_dir($location.DIRECTORY_SEPARATOR.'.git') === true) {
97-
$found = true;
98-
break;
99-
}
100-
}
101-
102-
if ($found === true) {
103-
chdir($location);
104-
} else {
105-
echo 'ERROR: Could not locate .git directory '.PHP_EOL.PHP_EOL;
106-
exit(2);
107-
}
108-
86+
chdir(dirname($filename));
10987
$command = 'git blame --date=short "'.$filename.'" 2>&1';
11088
$handle = popen($command, 'r');
11189
if ($handle === false) {
@@ -116,10 +94,6 @@ protected function getBlameContent($filename)
11694
$rawContent = stream_get_contents($handle);
11795
fclose($handle);
11896

119-
if (PHP_CODESNIFFER_VERBOSITY > 0) {
120-
echo 'DONE'.PHP_EOL;
121-
}
122-
12397
$blames = explode("\n", $rawContent);
12498
chdir($cwd);
12599

phpcs/CodeSniffer/Reports/Hgblame.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ protected function getBlameContent($filename)
8484
{
8585
$cwd = getcwd();
8686

87-
if (PHP_CODESNIFFER_VERBOSITY > 0) {
88-
echo 'Getting MERCURIAL blame info for '.basename($filename).'... ';
89-
}
90-
9187
$fileParts = explode(DIRECTORY_SEPARATOR, $filename);
9288
$found = false;
9389
$location = '';
@@ -117,10 +113,6 @@ protected function getBlameContent($filename)
117113
$rawContent = stream_get_contents($handle);
118114
fclose($handle);
119115

120-
if (PHP_CODESNIFFER_VERBOSITY > 0) {
121-
echo 'DONE'.PHP_EOL;
122-
}
123-
124116
$blames = explode("\n", $rawContent);
125117
chdir($cwd);
126118

phpcs/CodeSniffer/Reports/Notifysend.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function __construct()
8282
{
8383
$path = PHP_CodeSniffer::getConfigData('notifysend_path');
8484
if ($path !== null) {
85-
$this->path = $path;
85+
$this->path = escapeshellcmd($path);
8686
}
8787

8888
$timeout = PHP_CodeSniffer::getConfigData('notifysend_timeout');
@@ -246,7 +246,7 @@ protected function notifyErrors($msg)
246246
*/
247247
protected function getBasicCommand()
248248
{
249-
$cmd = escapeshellcmd($this->path);
249+
$cmd = $this->path;
250250
$cmd .= ' --category dev.validate';
251251
$cmd .= ' -h int:transient:1';
252252
$cmd .= ' -t '.(int) $this->timeout;

phpcs/CodeSniffer/Reports/Svnblame.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ protected function getAuthor($line)
7070
*/
7171
protected function getBlameContent($filename)
7272
{
73-
if (PHP_CODESNIFFER_VERBOSITY > 0) {
74-
echo 'Getting SVN blame info for '.basename($filename).'... ';
75-
}
76-
7773
$command = 'svn blame "'.$filename.'" 2>&1';
7874
$handle = popen($command, 'r');
7975
if ($handle === false) {
@@ -84,10 +80,6 @@ protected function getBlameContent($filename)
8480
$rawContent = stream_get_contents($handle);
8581
fclose($handle);
8682

87-
if (PHP_CODESNIFFER_VERBOSITY > 0) {
88-
echo 'DONE'.PHP_EOL;
89-
}
90-
9183
$blames = explode("\n", $rawContent);
9284

9385
return $blames;

0 commit comments

Comments
 (0)