Skip to content
Merged
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
55 changes: 54 additions & 1 deletion src/Rules/SinceVersionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,60 @@ private static function getRequiresAtLeastValue( ?string $pluginFile ): string {
return self::getRequiresAtLeastHeader( $path );
}

throw new \RuntimeException( 'No plugin or theme file found' );
if ( is_string( $pluginFile ) ) {
throw new \RuntimeException(
sprintf(
'WPCompat: The plugin file "%s" set in the WPCompat.pluginFile parameter was not found.',
$pluginFile
)
);
}

// The directory name doesn't always match the main plugin file name, for example
// in a git worktree, so fall back to the same discovery that WordPress uses.
if ( is_string( $cwd ) ) {
$path = self::findPluginFile( $cwd );

if ( is_string( $path ) ) {
return self::getRequiresAtLeastHeader( $path );
}
}

throw new \RuntimeException(
sprintf(
'WPCompat: Could not find a plugin or theme file in %s. Set the WPCompat.pluginFile or WPCompat.requiresAtLeast parameter in your PHPStan config file.',
is_string( $cwd ) ? $cwd : 'the current directory'
)
);
}

/**
* The logic in this method matches the logic in WordPress core's get_plugins() function.
*/
private static function findPluginFile( string $dir ): ?string {
$files = glob( "{$dir}/*.php" );

if ( ! is_array( $files ) ) {
return null;
}

sort( $files );

foreach ( $files as $file ) {
$contents = file_get_contents( $file, false, null, 0, 8 * 1024 );

if ( ! is_string( $contents ) ) {
continue;
}

$contents = str_replace( "\r", "\n", $contents );

if ( preg_match( '/^[ \t\/*#@]*Plugin Name:(.*)$/mi', $contents, $match ) === 1 && trim( $match[1] ) !== '' ) {
return $file;
}
}

return null;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,43 @@ public function dataMinimumVersions(): array {
],
];
}

public function testPluginFileIsFoundWhenDirectoryNameDoesNotMatch(): void {
$since = $this->createRuleInDirectory( __DIR__ . '/data/worktree' );

self::assertSame( '5.5.0', $since->getMinVersion() );
}

public function testMissingPluginFileThrowsHelpfulException(): void {
$this->expectException( \RuntimeException::class );
$this->expectExceptionMessage( 'Set the WPCompat.pluginFile or WPCompat.requiresAtLeast parameter' );

$this->createRuleInDirectory( __DIR__ . '/data/no-plugin' );
}

public function testMissingConfiguredPluginFileThrowsHelpfulException(): void {
$this->expectException( \RuntimeException::class );
$this->expectExceptionMessage( 'The plugin file "does-not-exist.php" set in the WPCompat.pluginFile parameter was not found' );

new SinceVersionRule(
null,
'does-not-exist.php',
self::$reflectionProvider
);
}

private function createRuleInDirectory( string $dir ): SinceVersionRule {
$cwd = (string) getcwd();
chdir( $dir );

try {
return new SinceVersionRule(
null,
null,
self::$reflectionProvider
);
} finally {
chdir( $cwd );
}
}
}
4 changes: 4 additions & 0 deletions tests/Unit/data/no-plugin/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
/**
* Not a plugin.
*/
4 changes: 4 additions & 0 deletions tests/Unit/data/worktree/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
/**
* Not the main plugin file.
*/
5 changes: 5 additions & 0 deletions tests/Unit/data/worktree/my-plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
/**
* Plugin Name: My Plugin
* Requires at least: 5.5
*/
Loading