Skip to content

Commit

Permalink
Merge pull request #112 from humanmade/92-tests-directory
Browse files Browse the repository at this point in the history
Add `NamespaceDirectoryName Exclusion` for `/tests` directories
  • Loading branch information
joehoyle committed Feb 15, 2019
2 parents 883c35b + 4687f3d commit 54d7b73
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 11 deletions.
9 changes: 6 additions & 3 deletions HM/Sniffs/Files/NamespaceDirectoryNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function process( File $phpcsFile, $stackPtr ) {
$filename = basename( $full );
$directory = dirname( $full );

// Normalize the directory seperator accross operating systems
// Normalize the directory separator across operating systems
if ( DIRECTORY_SEPARATOR !== '/' ) {
$directory = str_replace( DIRECTORY_SEPARATOR, '/', $directory );
}
Expand All @@ -55,13 +55,16 @@ public function process( File $phpcsFile, $stackPtr ) {
return;
}

if ( ! preg_match( '#/inc(?:/|$)#', $directory, $matches, PREG_OFFSET_CAPTURE ) ) {
if (
! preg_match( '#/inc(?:/|$)#', $directory, $matches, PREG_OFFSET_CAPTURE )
&& ! preg_match( '#/tests(?:/|$)#', $directory, $test_matches, PREG_OFFSET_CAPTURE )
) {
$error = 'Namespaced classes and functions should live inside an inc directory.';
$phpcsFile->addError( $error, $stackPtr, 'NoIncDirectory' );
return;
}

$inc_position = $matches[0][1];
$inc_position = $matches[0][1] ?? $test_matches[0][1];
$after_inc = substr( $directory, $inc_position + strlen( '/inc' ) );
if ( empty( $after_inc ) ) {
// Base inc directory, skip checks.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

namespace HM\Coding\Standards\Coffee;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

namespace HM\Coding\Standards\Coffee;

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions tests/fixtures/fail/not-namespace.php.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"6": [
{
"source": "HM.Files.NamespaceDirectoryName.NoIncDirectory",
"source": "HM.Files.NamespaceDirectoryName.NameMismatch",
"type": "error"
}
],
Expand All @@ -11,4 +11,4 @@
"type": "error"
}
]
}
}

0 comments on commit 54d7b73

Please sign in to comment.