Skip to content

Commit cb0dff4

Browse files
authored
Apply WP Coding Standards 3.0, fix #issue-30
2 parents 5bd4700 + 0a79d0d commit cb0dff4

17 files changed

+1501
-652
lines changed

.github/workflows/wpforms.yml

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,16 @@ jobs:
1313

1414
steps:
1515
- name: Checkout
16-
uses: actions/checkout@v2
17-
18-
- name: Get Composer cache directory
19-
id: composer-cache
20-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
21-
22-
- name: Setup Composer caching
23-
uses: actions/cache@v2
24-
env:
25-
cache-name: cache-composer-dependencies
26-
with:
27-
path: ${{ steps.composer-cache.outputs.dir }}
28-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
29-
restore-keys: |
30-
${{ runner.os }}-composer-
16+
uses: actions/checkout@v3
3117

3218
- name: Setup PHP
3319
uses: shivammathur/setup-php@v2
3420
with:
3521
php-version: ${{ matrix.php-versions }}
3622

23+
- name: Setup Composer caching
24+
uses: ramsey/composer-install@v2
25+
3726
- name: Validate composer.json and composer.lock
3827
run: composer validate
3928

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea/
2+
WPForms/Tests/.phpunit.result.cache
23
vendor/

WPForms/Sniffs/BaseSniff.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,30 +241,30 @@ protected function getFullyQualifiedClassName( $phpcsFile ) {
241241
*
242242
* @since 1.0.2
243243
*
244-
* @param string $class Class name.
244+
* @param string $className Class name.
245245
*
246246
* @return string
247247
*/
248-
private function convertClassName( $class ) {
248+
private function convertClassName( $className ) {
249249

250-
if ( strpos( $class, '_' ) !== false ) {
251-
return strtolower( $class );
250+
if ( strpos( $className, '_' ) !== false ) {
251+
return strtolower( $className );
252252
}
253253

254-
return $this->camelToSnake( $class );
254+
return $this->camelToSnake( $className );
255255
}
256256

257257
/**
258258
* Convert string from CamelCase to snake_case.
259259
*
260260
* @since 1.0.2
261261
*
262-
* @param string $string A string.
262+
* @param string $s A string.
263263
*
264264
* @return string
265265
*/
266-
private function camelToSnake( $string ) {
266+
private function camelToSnake( $s ) {
267267

268-
return strtolower( preg_replace( [ '/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/' ], '$1_$2', $string ) );
268+
return strtolower( preg_replace( [ '/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/' ], '$1_$2', $s ) );
269269
}
270270
}

WPForms/Sniffs/Comments/DeprecatedTagSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function process( File $phpcsFile, $stackPtr ) {
9191
$phpcsFile->addError(
9292
sprintf(
9393
'Add empty line after @deprecated tag for %s.',
94-
$this->getEntityName( $phpcsFile, $stackPtr, $tokens )
94+
$this->getEntityFullName( $phpcsFile, $stackPtr, $tokens )
9595
),
9696
$deprecated['tag'],
9797
'MissingEmptyLineAfterDeprecated'

WPForms/Sniffs/Comments/DescriptionStopSymbolSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function process( File $phpcsFile, $stackPtr ) {
5656
}
5757

5858
$firstDescriptionLine = $this->findFirstDescriptionLine( $phpcsFile, $commentStart );
59-
$entity = $this->getEntityName( $phpcsFile, $stackPtr, $tokens );
59+
$entity = $this->getEntityFullName( $phpcsFile, $stackPtr, $tokens );
6060

6161
if ( $tokens[ $commentStart ]['line'] - $tokens[ $firstDescriptionLine ]['line'] === 0 ) {
6262
$phpcsFile->addError(

WPForms/Sniffs/Comments/ParamTagHooksSniff.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,32 +123,32 @@ private function countArguments( $phpcsFile, $stackPtr ) { // phpcs:ignore Gener
123123

124124
while ( $currentPosition < $lastPosition ) {
125125
if ( $tokens[ $currentPosition ]['code'] === T_WHITESPACE ) {
126-
$currentPosition ++;
126+
++$currentPosition;
127127
continue;
128128
}
129129

130130
if ( $tokens[ $currentPosition ]['code'] === T_COMMA ) {
131131
$lookingForComma = false;
132132

133-
$currentPosition ++;
133+
++$currentPosition;
134134
continue;
135135
}
136136

137137
if ( in_array( $tokens[ $currentPosition ]['code'], [ T_ARRAY, T_OPEN_SHORT_ARRAY, T_CLOSURE ], true ) ) {
138-
$quantity ++;
138+
++$quantity;
139139
}
140140

141141
if ( $this->skip( $phpcsFile, $currentPosition ) ) {
142142
continue;
143143
}
144144

145-
$currentPosition ++;
145+
++$currentPosition;
146146

147147
if ( $lookingForComma ) {
148148
continue;
149149
}
150150

151-
$quantity ++;
151+
++$quantity;
152152

153153
$lookingForComma = true;
154154
}

WPForms/Sniffs/Comments/SinceTagSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function process( File $phpcsFile, $stackPtr ) {
5858
}
5959

6060
$since = $this->findTag( '@since', $commentStart, $tokens );
61-
$entity = $this->getEntityName( $phpcsFile, $stackPtr, $tokens );
61+
$entity = $this->getEntityFullName( $phpcsFile, $stackPtr, $tokens );
6262

6363
/*
6464
* @since is required.
@@ -122,7 +122,7 @@ public function process( File $phpcsFile, $stackPtr ) {
122122
private function lineBetweenTags( $phpcsFile, $stackPtr, $since ) {
123123

124124
$tokens = $phpcsFile->getTokens();
125-
$entity = $this->getEntityName( $phpcsFile, $stackPtr, $tokens );
125+
$entity = $this->getEntityFullName( $phpcsFile, $stackPtr, $tokens );
126126
$nextAnnotation = $phpcsFile->findNext( T_DOC_COMMENT_TAG, $since['tag'] + 1 );
127127
$commentEnd = $phpcsFile->findPrevious( T_DOC_COMMENT_CLOSE_TAG, $stackPtr );
128128
$nextAnnotation = $nextAnnotation && $tokens[ $commentEnd ]['line'] > $tokens[ $nextAnnotation ]['line'] ? $nextAnnotation : false;

WPForms/Sniffs/Formatting/EmptyLineAfterAssigmentVariablesSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private function getTokenNextLine( $phpcsFile, $semicolon ) {
111111
$nextLineTokens = [];
112112
$count = count( $tokens );
113113

114-
for ( $i = $semicolon; $i < $count; $i ++ ) {
114+
for ( $i = $semicolon; $i < $count; $i++ ) {
115115
if ( $tokens[ $i ]['line'] > $nextLine ) {
116116
break;
117117
}

WPForms/Sniffs/Formatting/SwitchSniff.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,15 @@ private function lineDistance( File $phpcsFile, $endPtr, $startPtr ) {
182182
],
183183
true
184184
) ||
185-
( $tokens[ $ptr ]['code'] === T_COMMENT &&
186-
$phpcsFile->findFirstOnLine( [ T_WHITESPACE, T_COMMENT ], $ptr, true ) === false )
185+
(
186+
$tokens[ $ptr ]['code'] === T_COMMENT &&
187+
$phpcsFile->findFirstOnLine( [ T_WHITESPACE, T_COMMENT ], $ptr, true ) === false
188+
)
187189
) {
188190
$commentLines[] = $tokens[ $ptr ]['line'];
189191
}
190192

191-
$ptr ++;
193+
++$ptr;
192194
}
193195

194196
return max(

WPForms/Sniffs/PHP/BackSlashSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private function processCode( $phpcsFile, $stackPtr ) {
7777
return;
7878
}
7979

80-
$startPtr ++;
80+
++$startPtr;
8181

8282
if ( $tokens[ $startPtr ]['code'] !== T_NS_SEPARATOR ) {
8383
return;

WPForms/Tests/TestCase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TestCase extends \PHPUnit\Framework\TestCase {
2525
* @return void
2626
* @throws DeepExitException DeepExitException.
2727
*/
28-
public function setUp() {
28+
public function setUp(): void {
2929

3030
parent::setUp();
3131

@@ -56,6 +56,7 @@ protected function process( Sniff $className, $rulesetName = '' ) {
5656
throw new RuntimeException(
5757
sprintf(
5858
'The %s file does not exist',
59+
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
5960
$localFile
6061
)
6162
);

WPForms/Tests/Tests/BaseSniffTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function testGetRelativePath() {
7474
$classFileName = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'PHP_CodeSniffertemp_folder0000' . DIRECTORY_SEPARATOR . $relativePath;
7575

7676
if ( file_exists( $classFileName ) ) {
77+
// phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
7778
unlink( $classFileName );
7879
}
7980

@@ -110,8 +111,10 @@ private function mkdirRecursive( $path ) {
110111
}
111112

112113
if ( $dir !== '' && ! is_dir( $dir ) && strpos( $dir, '.' ) === false ) {
114+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir
113115
mkdir( $dir );
114116
} elseif ( ! file_exists( $dir ) && strpos( $dir, '.' ) !== false ) {
117+
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_touch
115118
touch( $dir );
116119
}
117120
}

WPForms/Tests/phpunit.xml

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
<?xml version="1.0"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
bootstrap="./bootstrap.php"
4-
colors="true"
5-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd">
6-
<filter>
7-
<whitelist>
8-
<directory suffix=".php">../Sniffs</directory>
9-
<directory suffix=".php">../Traits</directory>
10-
</whitelist>
11-
</filter>
12-
<testsuites>
13-
<testsuite name="WPForms-tests">
14-
<directory suffix=".php">./Tests/</directory>
15-
</testsuite>
16-
</testsuites>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">../Sniffs</directory>
6+
<directory suffix=".php">../Traits</directory>
7+
</include>
8+
</coverage>
9+
<testsuites>
10+
<testsuite name="WPForms-tests">
11+
<directory suffix=".php">./Tests/</directory>
12+
</testsuite>
13+
</testsuites>
1714
</phpunit>

WPForms/Traits/GetEntityName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trait GetEntityName {
2222
*
2323
* @return string
2424
*/
25-
protected function getEntityName( $phpcsFile, $stackPtr, $tokens ) {
25+
protected function getEntityFullName( $phpcsFile, $stackPtr, $tokens ) {
2626

2727
$suffix = $this->getSuffix( $tokens[ $stackPtr ]['code'] );
2828

WPForms/ruleset.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,16 @@
5757
<exclude name="WordPress.WP.EnqueuedResourceParameters.MissingVersion"/>
5858
<!-- Database specific rules. -->
5959
<exclude name="WordPress.DB.DirectDatabaseQuery.DirectQuery"/>
60-
<!-- PSR4 yo. -->
60+
<!-- Rules to follow PSR4 -->
6161
<exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
6262
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase"/>
6363
<!-- Misc. -->
6464
<exclude name="WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents"/>
6565
<exclude name="WordPress.PHP.YodaConditions.NotYoda"/>
66-
<exclude name="Generic.Arrays.DisallowShortArraySyntax.Found"/>
66+
<!-- WPCS 3.0 -->
67+
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.Found"/>
68+
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed"/>
69+
<exclude name="Universal.Arrays.DisallowShortArraySyntax.Found"/>
6770
</rule>
6871

6972
<!-- Re-enable certain sniffs that were disabled by WordPress* standards. -->

composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
"config": {
77
"allow-plugins": {
88
"dealerdirect/phpcodesniffer-composer-installer": true
9+
},
10+
"platform": {
11+
"php": "7.4"
912
}
1013
},
1114
"require": {
12-
"php": "5.6 - 7.4",
13-
"phpcompatibility/php-compatibility": "^9.3",
14-
"squizlabs/php_codesniffer": "^3.7",
15-
"wp-coding-standards/wpcs": "^2.3",
16-
"dealerdirect/phpcodesniffer-composer-installer": "^v0.7"
15+
"php": "7.1 - 7.4",
16+
"phpcompatibility/php-compatibility": "^9.3.5",
17+
"squizlabs/php_codesniffer": "^3.7.2",
18+
"wp-coding-standards/wpcs": "^3.0.0"
1719
},
1820
"require-dev": {
19-
"phpunit/phpunit": "5.7 - 7.5"
21+
"roave/security-advisories": "dev-latest",
22+
"phpunit/phpunit": "6.5 - 9.5"
2023
},
2124
"autoload-dev": {
2225
"psr-4": {

0 commit comments

Comments
 (0)