Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ and fill in the contents as you go. This simplifies later release management. --

## 2.3.0

- Do not lint PHP within `build/` and `dist/` build directories
- Broaden relaxation of "must be named namespace.php" check for plugin entrypoint files to allow the patterns `<plugin-name>/plugin.php` or `<plugin-name>/<plugin-name>.php` in both regular and mu-plugin folders. #343
- Do not lint PHP within `build/` and `dist/` build directories #342

## 2.2.1

Expand Down
6 changes: 3 additions & 3 deletions HM/Sniffs/Files/FunctionFileNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Sniff to check for namespaced functions are in `namespace.php`.
*/
class FunctionFileNameSniff implements Sniff {
use MuPluginFileTrait;
use PluginEntryPointTrait;

public function register() {
return array( T_FUNCTION );
Expand All @@ -27,8 +27,8 @@ public function process( File $phpcsFile, $stackPtr ) {
return;
}

if ( $this->is_single_file_mu_plugin( $phpcsFile->getFileName() ) ) {
// Single-file plugins cannot be split into a namespace.php file.
if ( $this->is_plugin_entry_point( $phpcsFile ) ) {
// Plugin entry points cannot be split into a namespace.php file.
return;
}

Expand Down
27 changes: 0 additions & 27 deletions HM/Sniffs/Files/MuPluginFileTrait.php

This file was deleted.

6 changes: 3 additions & 3 deletions HM/Sniffs/Files/NamespaceDirectoryNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Namespaced things must be in directories matching the namespace.
*/
class NamespaceDirectoryNameSniff implements Sniff {
use MuPluginFileTrait;
use PluginEntryPointTrait;

/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -52,8 +52,8 @@ public function process( File $phpcsFile, $stackPtr ) {
$directory = str_replace( DIRECTORY_SEPARATOR, '/', $directory );
}

if ( $this->is_single_file_mu_plugin( $full ) ) {
// Single-file mu-plugins will naturally never have an inc/ directory.
if ( $this->is_plugin_entry_point( $phpcsFile ) ) {
// Plugin entry points will naturally never have an inc/ directory.
return;
}

Expand Down
60 changes: 60 additions & 0 deletions HM/Sniffs/Files/PluginEntryPointTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace HM\Sniffs\Files;

use PHP_CodeSniffer\Files\File;

/**
* Shared helper for detecting plugin entry points.
*
* We support two patterns:
*
* "Single-file mu-plugins" live as a direct child of a `mu-plugins/` directory
* (or `client-mu-plugins/`, on VIP) and encapsulate a complete plugin within
* one single PHP file.
*
* A plugin entrypoint may alternatively live nested within a plugin folder,
* conventionally named after its parent folder or else called plugin.php.
* These ordinary plugin entrypoints can live within `plugins/`, `mu-plugins/`
* or `client-mu-plugins/` alike.
*
* Both are useful patterns to support, and should be exempt from certain rules:
* entrypoint files can't be split into an inc/ directory and collide or lose
* meaning if renamed to namespace.php.
*/
trait PluginEntryPointTrait {
/**
* Is the file being checked a plugin entry point?
*
* @param File $phpcsFile The file being scanned.
* @return bool True if the file is a single-file mu-plugin or a nested plugin entry point.
*/
protected function is_plugin_entry_point( File $phpcsFile ) {
$path = $phpcsFile->getFilename();

// Normalize the directory separator across operating systems.
if ( DIRECTORY_SEPARATOR !== '/' ) {
$path = str_replace( DIRECTORY_SEPARATOR, '/', $path );
}

// Naming a file plugin.php conventionally signals it's a plugin entrypoint.
if ( basename( $path ) === 'plugin.php' ) {
return true;
}

// Single-file mu-plugin: a direct child of (client-)mu-plugins/.
if ( preg_match( '#/(?:client-)?mu-plugins/[^/]+\.php$#', $path ) ) {
return true;
}

// Nested entry point: the conventionally named main file directly
// inside a plugin's own folder, under plugins/, mu-plugins/ or
// client-mu-plugins/. Anything deeper is ordinary plugin code.
if ( ! preg_match( '#/(?:(?:client-)?mu-)?plugins/([^/]+)/([^/]+)\.php$#', $path, $matches ) ) {
return false;
}

// Check for conventional pattern <plugin-name>/<plugin-name>.php.
[ , $dir, $file ] = $matches;
return $file === $dir;
}
}
5 changes: 5 additions & 0 deletions HM/Tests/Files/FunctionFileNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function getErrorList() {
// Single-file mu-plugins can't all be named namespace.php.
'my-plugin.php',
'my-client-plugin.php',
// Nested plugin entry points are exempt too -- in plugins/ as well
// as mu-plugins/ -- when named <dir>/<dir>.php or <dir>/plugin.php.
'sub-plugin.php',
'plugin.php',
'plugin-name.php',
];
if ( in_array( $file, $pass, true ) ) {
return [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace HM\Coding\Standards\Plugin_Named_After_Dir;

function foo() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace HM\Coding\Standards\With_Plugin_File;

function foo() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace HM\Coding\Standards\Standalone;

function foo() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace HM\Coding\Standards\With_Plugin_File;

function foo() {}
4 changes: 4 additions & 0 deletions HM/Tests/Files/NamespaceDirectoryNameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public function getErrorList() {
// Single-file mu-plugins are exempt from the directory structure rules.
'mu-plugin.php',
'client-mu-plugin.php',
// Nested plugin entry points are exempt too -- in plugins/ as well
// as mu-plugins/ -- when named <dir>/<dir>.php or <dir>/plugin.php.
'plugin.php',
'plugin-name.php',
];
if ( in_array( $file, $pass, true ) ) {
return [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

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

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

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

namespace HM\Coding\Standards\With_Plugin_File;