Skip to content
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

Warn for missing composer json file #845

Merged
merged 2 commits into from
Jan 3, 2025
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ build
############

node_modules/
vendor/
/vendor/

############
## OSes
Expand Down
46 changes: 45 additions & 1 deletion includes/Checker/Checks/Plugin_Repo/File_Type_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
const TYPE_APPLICATION = 16;
const TYPE_BADLY_NAMED = 32;
const TYPE_LIBRARY_CORE = 64;
const TYPE_ALL = 127; // Same as all of the above with bitwise OR.
const TYPE_COMPOSER = 128;
const TYPE_ALL = 255; // Same as all of the above with bitwise OR.

/**
* Bitwise flags to control check behavior.
Expand Down Expand Up @@ -75,6 +76,8 @@
*
* @throws Exception Thrown when the check fails with a critical error (unrelated to any errors detected as part of
* the check).
*
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function check_files( Check_Result $result, array $files ) {
if ( $this->flags & self::TYPE_COMPRESSED ) {
Expand All @@ -99,6 +102,9 @@
if ( $this->flags & self::TYPE_LIBRARY_CORE ) {
$this->look_for_library_core_files( $result, $files );
}
if ( $this->flags & self::TYPE_COMPOSER ) {
$this->look_for_composer_files( $result, $files );
}
}

/**
Expand Down Expand Up @@ -372,6 +378,44 @@
}
}

/**
* Looks for composer files.
*
* @since 1.4.0
*
* @param Check_Result $result The check result to amend, including the plugin context to check.
* @param array $files List of absolute file paths.
*/
protected function look_for_composer_files( Check_Result $result, array $files ) {
if ( $result->plugin()->is_single_file_plugin() ) {
return;
}

$plugin_path = $result->plugin()->path();

if (
is_dir( $plugin_path . 'vendor' ) &&
file_exists( $plugin_path . 'vendor/autoload.php' ) &&
! file_exists( $plugin_path . 'composer.json' )
) {
$this->add_result_warning_for_file(
$result,
sprintf(

Check warning on line 403 in includes/Checker/Checks/Plugin_Repo/File_Type_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/File_Type_Check.php#L401-L403

Added lines #L401 - L403 were not covered by tests
/* translators: 1: directory, 2: filename */
esc_html__( 'The "%1$s" directory using composer exists, but "%2$s" file is missing.', 'plugin-check' ),
'/vendor',
'composer.json'
),
'missing_composer_json_file',
'composer.json',
0,
0,
'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#included-unneeded-folders',
6
);

Check warning on line 415 in includes/Checker/Checks/Plugin_Repo/File_Type_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/File_Type_Check.php#L405-L415

Added lines #L405 - L415 were not covered by tests
}
}

/**
* Gets the description for the check.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Plugin Name: Test Plugin File Type Composer Files
* Plugin URI: https://github.com/WordPress/plugin-check
* Description: Some plugin description.
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/plugins/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-file-type-composer-errors
*
* @package test-plugin-file-type-composer-errors
*/

/**
* Plugin folder contains a vendor folder without composer.json file.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

// autoload.php @generated by Composer
14 changes: 14 additions & 0 deletions tests/phpunit/tests/Checker/Checks/File_Type_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,18 @@ public function test_run_with_library_core_errors() {
$this->assertArrayHasKey( 0, $errors['jquery.js'][0] );
$this->assertCount( 1, wp_list_filter( $errors['jquery.js'][0][0], array( 'code' => 'library_core_files' ) ) );
}

public function test_run_with_composer_errors() {
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-composer-errors/load.php' );
$check_result = new Check_Result( $check_context );

$check = new File_Type_Check( File_Type_Check::TYPE_COMPOSER );
$check->run( $check_result );

$warnings = $check_result->get_warnings();

$this->assertNotEmpty( $warnings );

$this->assertCount( 1, wp_list_filter( $warnings['composer.json'][0][0], array( 'code' => 'missing_composer_json_file' ) ) );
}
}
Loading