Skip to content

Commit 7829428

Browse files
committed
Fix expected domain calculation for the same dir where phpcs.xml is.
1 parent 922cad3 commit 7829428

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

WPForms/Sniffs/PHP/ValidateDomainSniff.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ private function getCurrentDomain( $phpcsFile, $stackPtr ) {
170170
* @param File $phpcsFile The PHP_CodeSniffer file where the token was found.
171171
*
172172
* @return string
173+
* @noinspection PhpTernaryExpressionCanBeReducedToShortVersionInspection
174+
* @noinspection ElvisOperatorCanBeUsedInspection
173175
*/
174176
private function getExpectedDomain( $phpcsFile ) {
175177

@@ -207,16 +209,16 @@ private function getExpectedDomain( $phpcsFile ) {
207209
*/
208210
private function findDomainByProperty( $filePath ) {
209211

210-
$filePath = ltrim( $filePath, DIRECTORY_SEPARATOR );
212+
$fileDir = dirname( $filePath );
211213
$currentDomain = '';
212214
$currentPath = '';
213215

214216
foreach ( $this->domains as $domain => $paths ) {
215217
foreach ( $paths as $path ) {
216-
$path = rtrim( $path, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
218+
$pathDir = $this->normalizePath( $path );
217219

218220
if (
219-
strpos( $filePath, $path ) === 0 &&
221+
0 === strpos( $fileDir, $pathDir ) &&
220222
strlen( $path ) > strlen( $currentPath )
221223
) {
222224
$currentDomain = $domain;
@@ -227,4 +229,37 @@ private function findDomainByProperty( $filePath ) {
227229

228230
return $currentDomain;
229231
}
232+
233+
/**
234+
* Get normalized path, like realpath() for non-existing path or file.
235+
*
236+
* @since 1.0.5
237+
*
238+
* @param string $path Path to be normalized.
239+
*
240+
* @return string
241+
*/
242+
private function normalizePath( $path ) {
243+
244+
return (string) array_reduce(
245+
explode( DIRECTORY_SEPARATOR, $path ),
246+
static function ( $a, $b ) {
247+
if ( $a === null ) {
248+
$a = DIRECTORY_SEPARATOR;
249+
}
250+
if ( $b === '' || $b === '.' ) {
251+
return $a;
252+
}
253+
if ( $b === '..' ) {
254+
return dirname( $a );
255+
}
256+
257+
$sep = DIRECTORY_SEPARATOR;
258+
$escSep = '\\' . $sep;
259+
$pattern = "/$escSep+/";
260+
261+
return (string) preg_replace( $pattern, DIRECTORY_SEPARATOR, "$a$sep$b" );
262+
}
263+
);
264+
}
230265
}

0 commit comments

Comments
 (0)