Skip to content

Commit b67b5d4

Browse files
committed
refactor: lint & rector (partial)
1 parent a8bb786 commit b67b5d4

54 files changed

Lines changed: 1051 additions & 983 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/Autoloader.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* PSR-4 Autoloader for PHP classes inside plugin.
4+
*
5+
* Ensures that autoloaders are present, and logs an Admin notice if not.
6+
*
7+
* Can be bypassed by defining the WP_MCP_AUTOLOAD constant to false.
8+
*
9+
* @package WP\MCP
10+
*/
11+
12+
declare( strict_types = 1 );
13+
14+
namespace WP\MCP;
15+
16+
/**
17+
* Class - Autoloader
18+
*/
19+
class Autoloader {
20+
/**
21+
* Whether the autoloader has been loaded.
22+
*
23+
* @var bool
24+
*/
25+
protected static bool $is_loaded = false;
26+
27+
/**
28+
* Attempt to autoload the Composer dependencies.
29+
*/
30+
public static function autoload(): bool {
31+
// If we're not *supposed* to autoload anything, then return true.
32+
if ( defined( 'WP_MCP_AUTOLOAD' ) && false === WP_MCP_AUTOLOAD ) {
33+
return true;
34+
}
35+
36+
if ( self::$is_loaded ) {
37+
return self::$is_loaded;
38+
}
39+
40+
$autoloader = WP_MCP_DIR . '/vendor/autoload.php';
41+
self::$is_loaded = self::require_autoloader( $autoloader );
42+
43+
return self::$is_loaded;
44+
}
45+
46+
/**
47+
* Attempts to load the autoloader file, if it exists.
48+
*
49+
* @param string $autoloader_file The path to the autoloader file.
50+
*/
51+
protected static function require_autoloader( string $autoloader_file ): bool {
52+
if ( ! is_readable( $autoloader_file ) ) {
53+
self::missing_autoloader_notice();
54+
return false;
55+
}
56+
57+
return (bool) require_once $autoloader_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable -- Autoloader is a Composer file.
58+
}
59+
60+
/**
61+
* Displays a notice if the autoloader is missing.
62+
*/
63+
protected static function missing_autoloader_notice(): void {
64+
65+
$hooks = array(
66+
'admin_notices',
67+
'network_admin_notices',
68+
);
69+
70+
foreach ( $hooks as $hook ) {
71+
add_action(
72+
$hook,
73+
static function () {
74+
$error_message = __( 'MCP Adapter: The Composer autoloader was not found. If you installed the plugin from the GitHub source code, make sure to run `composer install`.', 'mcp-adapter' );
75+
76+
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
77+
error_log( esc_html( $error_message ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- This is a development notice.
78+
}
79+
?>
80+
<div class="error notice">
81+
<p>
82+
<?php echo esc_html( $error_message ); ?>
83+
</p>
84+
</div>
85+
<?php
86+
}
87+
);
88+
}
89+
}
90+
}

src/Core/McpAdapter.php

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99

1010
namespace WP\MCP\Core;
1111

12-
use Exception;
1312
use WP\MCP\Infrastructure\ErrorHandling\Contracts\McpErrorHandlerInterface;
1413
use WP\MCP\Infrastructure\ErrorHandling\NullMcpErrorHandler;
1514
use WP\MCP\Infrastructure\Observability\Contracts\McpObservabilityHandlerInterface;
1615
use WP\MCP\Infrastructure\Observability\NullMcpObservabilityHandler;
17-
use WP\MCP\Core\McpServer;
1816

1917
/**
2018
* WordPress MCP Registry - Main class for managing multiple MCP servers.
@@ -23,9 +21,9 @@ class McpAdapter {
2321
/**
2422
* Registry instance
2523
*
26-
* @var McpAdapter|null
24+
* @var \WP\MCP\Core\McpAdapter|null
2725
*/
28-
private static ?McpAdapter $instance = null;
26+
private static ?self $instance = null;
2927

3028
/**
3129
* The initialized flag.
@@ -51,7 +49,7 @@ class McpAdapter {
5149
/**
5250
* Registered servers
5351
*
54-
* @var McpServer[]
52+
* @var \WP\MCP\Core\McpServer[]
5553
*/
5654
private array $servers = array();
5755

@@ -66,14 +64,16 @@ class McpAdapter {
6664
* Constructor
6765
*/
6866
private function __construct() {
69-
if ( ! self::$initialized && ! self::$initialization_failed ) {
70-
if ( ! $this->check_dependencies() ) {
71-
self::$initialization_failed = true;
72-
return;
73-
}
74-
add_action( 'rest_api_init', array( $this, 'mcp_adapter_init' ), 20000 );
75-
self::$initialized = true;
67+
if ( self::$initialized || self::$initialization_failed ) {
68+
return;
69+
}
70+
71+
if ( ! $this->check_dependencies() ) {
72+
self::$initialization_failed = true;
73+
return;
7674
}
75+
add_action( 'rest_api_init', array( $this, 'mcp_adapter_init' ), 20000 );
76+
self::$initialized = true;
7777
}
7878

7979
/**
@@ -97,12 +97,9 @@ private function check_dependencies(): bool {
9797
// Store errors for later retrieval.
9898
self::$initialization_errors = $errors;
9999

100-
// Log errors if any.
101-
if ( ! empty( $errors ) ) {
102-
return false;
103-
}
100+
// @todo Log errors if any.
104101

105-
return true;
102+
return empty( $errors );
106103
}
107104

108105
/**
@@ -114,18 +111,20 @@ public function mcp_adapter_init(): void {
114111
return;
115112
}
116113

117-
if ( ! $this->has_triggered_init ) {
118-
do_action( 'mcp_adapter_init', $this );
119-
$this->has_triggered_init = true;
114+
if ( $this->has_triggered_init ) {
115+
return;
120116
}
117+
118+
do_action( 'mcp_adapter_init', $this );
119+
$this->has_triggered_init = true;
121120
}
122121

123122
/**
124123
* Get the registry instance
125124
*
126-
* @return McpAdapter|null Returns null if initialization failed due to missing dependencies.
125+
* @return ?\WP\MCP\Core\McpAdapter Returns null if initialization failed due to missing dependencies.
127126
*/
128-
public static function instance(): ?McpAdapter {
127+
public static function instance(): ?self {
129128
if ( null === self::$instance && ! self::$initialization_failed ) {
130129
self::$instance = new self();
131130
}
@@ -188,8 +187,8 @@ public static function get_dependency_status(): array {
188187
* @param array $prompts Prompts to register.
189188
* @param callable|null $transport_permission_callback Optional custom permission callback for transport-level authentication. If null, defaults to is_user_logged_in().
190189
*
191-
* @return McpAdapter
192-
* @throws Exception If the server already exists or if called outside of the mcp_adapter_init action.
190+
* @return \WP\MCP\Core\McpAdapter
191+
* @throws \Exception If the server already exists or if called outside of the mcp_adapter_init action.
193192
*/
194193
public function create_server( string $server_id, string $server_route_namespace, string $server_route, string $server_name, string $server_description, string $server_version, array $mcp_transports, ?string $error_handler, ?string $observability_handler = null, array $tools = array(), array $resources = array(), array $prompts = array(), ?callable $transport_permission_callback = null ): self {
195194

@@ -200,7 +199,7 @@ public function create_server( string $server_id, string $server_route_namespace
200199

201200
// Validate error handler class implements McpErrorHandlerInterface.
202201
if ( ! in_array( McpErrorHandlerInterface::class, class_implements( $error_handler ) ?: array(), true ) ) {
203-
throw new Exception(
202+
throw new \Exception(
204203
esc_html__( 'Error handler class must implement the McpErrorHandlerInterface.', 'mcp-adapter' )
205204
);
206205
}
@@ -212,18 +211,18 @@ public function create_server( string $server_id, string $server_route_namespace
212211

213212
// Validate observability handler class implements McpObservabilityHandlerInterface.
214213
if ( ! in_array( McpObservabilityHandlerInterface::class, class_implements( $observability_handler ) ?: array(), true ) ) {
215-
throw new Exception(
214+
throw new \Exception(
216215
esc_html__( 'Observability handler class must implement the McpObservabilityHandlerInterface interface.', 'mcp-adapter' )
217216
);
218217
}
219218

220219
if ( ! doing_action( 'mcp_adapter_init' ) ) {
221-
throw new Exception(
220+
throw new \Exception(
222221
esc_html__( 'MCP Server creation must be done during mcp_adapter_init action.', 'mcp-adapter' )
223222
);
224223
}
225224
if ( isset( $this->servers[ $server_id ] ) ) {
226-
throw new Exception(
225+
throw new \Exception(
227226
// translators: %s: server ID.
228227
sprintf( esc_html__( 'Server with ID "%s" already exists.', 'mcp-adapter' ), esc_html( $server_id ) )
229228
);
@@ -269,7 +268,7 @@ public function create_server( string $server_id, string $server_route_namespace
269268
*
270269
* @param string $server_id Server ID.
271270
*
272-
* @return McpServer|null
271+
* @return \WP\MCP\Core\McpServer|null
273272
*/
274273
public function get_server( string $server_id ): ?McpServer {
275274
return $this->servers[ $server_id ] ?? null;
@@ -278,7 +277,7 @@ public function get_server( string $server_id ): ?McpServer {
278277
/**
279278
* Get all registered servers
280279
*
281-
* @return McpServer[]
280+
* @return \WP\MCP\Core\McpServer[]
282281
*/
283282
public function get_servers(): array {
284283
return $this->servers;

0 commit comments

Comments
 (0)