-
Notifications
You must be signed in to change notification settings - Fork 19
Permit single-file mu-plugins #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
| namespace HM\Sniffs\Files; | ||
|
|
||
| /** | ||
| * Shared helper for detecting single-file must-use plugins. | ||
| * | ||
| * Single-file mu-plugins live as direct children of a `mu-plugins/` directory | ||
| * (or `client-mu-plugins/`, on VIP). This is a useful pattern to support, but | ||
| * by definition can't be split into an inc/ directory or named namespace.php | ||
| * (multiple plugins would collide), and should allow side effects. | ||
| */ | ||
| trait MuPluginFileTrait { | ||
| /** | ||
| * Is the given file a single-file mu-plugin? | ||
| * | ||
| * @param string $path Full path to the file being checked. | ||
| * @return bool True if the file is a direct child of mu-plugins/ or client-mu-plugins/. | ||
| */ | ||
| protected function is_single_file_mu_plugin( $path ) { | ||
| // Normalize the directory separator across operating systems. | ||
| if ( DIRECTORY_SEPARATOR !== '/' ) { | ||
| $path = str_replace( DIRECTORY_SEPARATOR, '/', $path ); | ||
| } | ||
|
|
||
| return (bool) preg_match( '#/(?:client-)?mu-plugins/[^/]+\.php$#', $path ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
HM/Tests/Files/FunctionFileNameUnitTest/client-mu-plugins/my-client-plugin.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?php | ||
|
|
||
| namespace HM\Coding\Standards\Tests; | ||
|
|
||
| function foo() {} |
5 changes: 5 additions & 0 deletions
5
HM/Tests/Files/FunctionFileNameUnitTest/mu-plugins/my-plugin.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?php | ||
|
|
||
| namespace HM\Coding\Standards\Tests; | ||
|
|
||
| function foo() {} |
5 changes: 5 additions & 0 deletions
5
HM/Tests/Files/FunctionFileNameUnitTest/mu-plugins/nested/nested-plugin.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?php | ||
|
|
||
| namespace HM\Coding\Standards\Tests; | ||
|
|
||
| function foo() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
HM/Tests/Files/NamespaceDirectoryNameUnitTest/client-mu-plugins/client-mu-plugin.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <?php | ||
|
|
||
| namespace HM\Coding\Standards\Client_Mu_Plugin; |
3 changes: 3 additions & 0 deletions
3
HM/Tests/Files/NamespaceDirectoryNameUnitTest/mu-plugins/mu-plugin.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <?php | ||
|
|
||
| namespace HM\Coding\Standards\Mu_Plugin; |
3 changes: 3 additions & 0 deletions
3
HM/Tests/Files/NamespaceDirectoryNameUnitTest/mu-plugins/nested/nested-mu-plugin.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <?php | ||
|
|
||
| namespace HM\Coding\Standards\Nested; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
| /** | ||
| * Test that PSR1.Files.SideEffects is excluded for single-file mu-plugins. | ||
| * | ||
| * The two HM file-structure sniffs handle their own mu-plugin exemption in | ||
| * code (covered by their sniff unit tests). PSR1.Files.SideEffects is a | ||
| * third-party sniff, so its exemption lives as an <exclude-pattern> in | ||
| * HM/ruleset.xml. | ||
| * | ||
| * We exercise it by running the phpcs binary in a subprocess rather than | ||
| * building the ruleset in-process: loading the full HM standard alongside the | ||
| * other test suites triggers sniff class redeclaration errors, and restricting | ||
| * the sniffs in-process makes PHPCS skip the ruleset processing that loads the | ||
| * exclude-patterns in the first place. | ||
| */ | ||
|
|
||
| namespace HM\CodingStandards\Tests; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * Class MuPluginSideEffectsTest | ||
| * | ||
| * @group fixtures | ||
| */ | ||
| class MuPluginSideEffectsTest extends TestCase { | ||
| const PSR1_SIDE_EFFECTS = 'PSR1.Files.SideEffects.FoundWithSymbols'; | ||
|
|
||
| /** | ||
| * Run PSR1.Files.SideEffects over a fixture and return the message sources. | ||
| * | ||
| * @param string $relative_file Fixture path relative to the tests directory. | ||
| * @return string[] List of reported message sources. | ||
| */ | ||
| protected function get_sources( string $relative_file ) { | ||
| $root = dirname( __DIR__ ); | ||
| $phpcs = $root . '/vendor/bin/phpcs'; | ||
| $file = __DIR__ . '/' . $relative_file; | ||
|
|
||
| $command = sprintf( | ||
| '%s %s --standard=HM --sniffs=PSR1.Files.SideEffects --report=json %s', | ||
| escapeshellarg( PHP_BINARY ), | ||
| escapeshellarg( $phpcs ), | ||
| escapeshellarg( $file ) | ||
| ); | ||
|
|
||
| $output = shell_exec( $command ); | ||
| $report = json_decode( $output, true ); | ||
|
|
||
| $this->assertSame( JSON_ERROR_NONE, json_last_error(), 'phpcs should return valid JSON' ); | ||
|
|
||
| $sources = []; | ||
| foreach ( $report['files'] as $details ) { | ||
| foreach ( $details['messages'] as $message ) { | ||
| $sources[] = $message['source']; | ||
| } | ||
| } | ||
|
|
||
| return $sources; | ||
| } | ||
|
|
||
| /** | ||
| * Single-file mu-plugins should be exempt from the side-effects rule. | ||
| */ | ||
| public function test_mu_plugin_is_exempt() { | ||
| $sources = $this->get_sources( 'fixtures/sideeffects/mu-plugins/example-plugin.php' ); | ||
| $this->assertNotContains( static::PSR1_SIDE_EFFECTS, $sources ); | ||
| } | ||
|
|
||
| /** | ||
| * The same code outside mu-plugins/ should still get flagged, to prove correct scoping. | ||
| */ | ||
| public function test_regular_file_is_flagged() { | ||
| $sources = $this->get_sources( 'fixtures/sideeffects/regular/example-plugin.php' ); | ||
| $this->assertContains( static::PSR1_SIDE_EFFECTS, $sources ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
| /** | ||
| * Plugin Name: Example single-file mu-plugin. | ||
| * | ||
| * Single-file mu-plugins routinely declare symbols and add hooks in the same | ||
| * file, so PSR1.Files.SideEffects should not flag them. | ||
| */ | ||
|
|
||
| namespace HM\Coding\Standards\Example; | ||
|
|
||
| add_action( 'init', __NAMESPACE__ . '\\bootstrap' ); | ||
|
|
||
| /** | ||
| * Bootstrap the plugin. | ||
| */ | ||
| function bootstrap() { | ||
| // ... | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
| /** | ||
| * Plugin Name: Example regular plugin file. | ||
| * | ||
| * Identical to the mu-plugin fixture, but not a direct child of mu-plugins/, | ||
| * so PSR1.Files.SideEffects should still flag it. | ||
| */ | ||
|
|
||
| namespace HM\Coding\Standards\Example; | ||
|
|
||
| add_action( 'init', __NAMESPACE__ . '\\bootstrap' ); | ||
|
|
||
| /** | ||
| * Bootstrap the plugin. | ||
| */ | ||
| function bootstrap() { | ||
| // ... | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
plugins-mualso used?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in any projects that I've worked on in the past 3–4 years, my inclination is to leave that one out as it's a confusing pattern