Skip to content

Commit 6ee0896

Browse files
committed
Fix code and tests for Multiple Domains configuration.
1 parent 7829428 commit 6ee0896

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

WPForms/Sniffs/PHP/ValidateDomainSniff.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,17 @@ public function process( File $phpcsFile, $stackPtr ) {
103103
$currentDomain = $this->getCurrentDomain( $phpcsFile, $stackPtr );
104104
$expectedDomain = $this->getExpectedDomain( $phpcsFile );
105105

106-
if ( ! $expectedDomain ) {
106+
if ( $currentDomain === false ) {
107+
$phpcsFile->addError(
108+
'Domain name must be string.',
109+
$stackPtr,
110+
'NotStringDomain'
111+
);
112+
113+
return;
114+
}
115+
116+
if ( ! $currentDomain || ! $expectedDomain ) {
107117
return;
108118
}
109119

@@ -113,7 +123,7 @@ public function process( File $phpcsFile, $stackPtr ) {
113123

114124
$phpcsFile->addError(
115125
sprintf(
116-
"You are using invalid domain name. Use '%s' instead of '%s'",
126+
"You are using invalid domain name. Use '%s' instead of '%s'.",
117127
$expectedDomain,
118128
$currentDomain
119129
),
@@ -130,7 +140,7 @@ public function process( File $phpcsFile, $stackPtr ) {
130140
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
131141
* @param int $stackPtr The position in the PHP_CodeSniffer file's token stack where the token was found.
132142
*
133-
* @return string
143+
* @return string|false
134144
*/
135145
private function getCurrentDomain( $phpcsFile, $stackPtr ) {
136146

@@ -148,7 +158,7 @@ private function getCurrentDomain( $phpcsFile, $stackPtr ) {
148158

149159
// Last argument is not a string, but something else.
150160
if ( $lastArgument ) {
151-
return '';
161+
return false;
152162
}
153163

154164
$lastArgument = $phpcsFile->findNext( T_CONSTANT_ENCAPSED_STRING, $commaPtr + 1, $closePtr );
@@ -192,10 +202,9 @@ private function getExpectedDomain( $phpcsFile ) {
192202
return $basename;
193203
}
194204

195-
$relativeDir = str_replace( [ '\\', '/', '.' ], [ '-', '-', '' ], dirname( $filePath ) );
196-
$relativeDir = strtolower( trim( $relativeDir, '-' ) );
205+
preg_match( '/[\w.-]+/', dirname( $filePath ), $domain );
197206

198-
return $relativeDir ? $relativeDir : $basename;
207+
return ! empty( $domain[0] ) ? strtolower( $domain[0] ) : $basename;
199208
}
200209

201210
/**
@@ -209,7 +218,7 @@ private function getExpectedDomain( $phpcsFile ) {
209218
*/
210219
private function findDomainByProperty( $filePath ) {
211220

212-
$fileDir = dirname( $filePath );
221+
$fileDir = DIRECTORY_SEPARATOR . ltrim( dirname( $filePath ), DIRECTORY_SEPARATOR );
213222
$currentDomain = '';
214223
$currentPath = '';
215224

WPForms/Tests/TestFiles/PHP/ValidateDomain.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,29 @@
1818

1919
__( 'Invalid', 'wpforms' );
2020
_e( 'Invalid', 'wpforms-geolocation' );
21-
_x( 'Invalid', 'context', 'wpforms-lite' );
22-
esc_html__( 'Invalid', 'wpforms-lite' );
23-
esc_html_e( 'Invalid', 'wpforms-lite' );
24-
esc_html_x( 'Invalid', 'context', 'wpforms-lite' );
25-
esc_attr__( 'Invalid', 'wpforms-lite' );
26-
esc_attr_e( 'Invalid', 'wpforms-lite' );
21+
_x( 'Valid for lite only', 'context', 'wpforms-lite' );
22+
esc_html__( 'Valid for lite only', 'wpforms-lite' );
23+
esc_html_e( 'Valid for lite only', 'wpforms-lite' );
24+
esc_html_x( 'Valid for lite only', 'context', 'wpforms-lite' );
25+
esc_attr__( 'Valid for lite only', 'wpforms-lite' );
26+
esc_attr_e( 'Valid for lite only', 'wpforms-lite' );
2727
esc_attr_x( 'Invalid', 'context', 'wpforms' );
2828
_n( 'Invalid', 'Invalid', 10, 'wpforms' );
2929
_ex( 'Invalid', 'Invalid', 'wpforms' );
3030
_nx( 'Invalid', 'Invalid', 10, 'Invalid', 'wpforms' );
3131

3232

33-
// Additional two cases of valid syntax when text domain is not specified.
33+
// Valid syntax when the text domain is not specified.
3434
esc_html__( 'Valid' );
3535

36-
// Not a string as the last argument.
36+
// Invalid - not a string as the last argument.
3737
$bulk_counts['read'] = 25;
3838
_n( '%d entry.', '%d entries.', $bulk_counts['read'] );
3939

40-
// Nested parenthesis with domain specified as the last argument.
40+
// Invalid - nested parenthesis inside the gettext function with domain specified as the last argument.
4141
_n(
4242
'Found <strong>%s entry</strong>',
4343
'Found <strong>%s entries</strong>',
4444
absint( count( $this->entries->items ) ),
4545
'wpforms'
4646
);
47-
48-
// phpcs:enable WordPress.Security.EscapeOutput.UnsafePrintingFunction

WPForms/Tests/TestRulesets/ValidateDomainWithRewrites/ruleset.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<config name="multi_domains" value="true"/>
55
<rule ref="WPForms.PHP.ValidateDomain">
66
<properties>
7-
<property name="wpforms-lite" value="WPForms/"/>
7+
<property name="wpforms-lite" value="WPForms"/>
88
</properties>
99
</rule>
1010
</ruleset>

WPForms/Tests/Tests/PHP/ValidateDomainTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public function testProcess() {
2121

2222
$phpcsFile = $this->process( new ValidateDomainSniff() );
2323

24-
$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 34, 38, 41 ] );
24+
$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 41 ] );
25+
$this->fileHasErrors( $phpcsFile, 'NotStringDomain', [ 38 ] );
2526
}
2627

2728
/**
@@ -33,7 +34,7 @@ public function testProcessWithMultiDomains() {
3334

3435
$phpcsFile = $this->process( new ValidateDomainSniff(), 'MultiDomains' );
3536

36-
$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 21, 22, 23, 24, 25, 26, 34, 38 ] );
37+
$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 21, 22, 23, 24, 25, 26 ] );
3738
}
3839

3940
/**
@@ -45,6 +46,6 @@ public function testProcessWithRewrittenPaths() {
4546

4647
$phpcsFile = $this->process( new ValidateDomainSniff(), 'ValidateDomainWithRewrites' );
4748

48-
$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19, 20, 27, 28, 29, 30, 34, 38, 41 ] );
49+
$this->fileHasErrors( $phpcsFile, 'InvalidDomain', [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19, 20, 27, 28, 29, 30, 41 ] );
4950
}
5051
}

0 commit comments

Comments
 (0)