Skip to content

Commit 657d5d5

Browse files
committed
PHPStan level 7
1 parent 7b49113 commit 657d5d5

File tree

6 files changed

+41
-12
lines changed

6 files changed

+41
-12
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 6
2+
level: 7
33
paths:
44
- ai-command.php
55
- src/

src/AI/AiClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ static function () {
142142

143143
$text = '';
144144

145-
$content = $candidates->get( 0 )->get_content() ?? [];
145+
$parts = $candidates->get( 0 )->get_content()?->get_parts() ?? new Parts();
146146

147-
foreach ( $content->get_parts() as $part ) {
147+
foreach ( $parts as $part ) {
148148
if ( $part instanceof Text_Part ) {
149149
if ( '' !== $text ) {
150150
$text .= "\n\n";

src/AiCommand.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ protected function get_tools( array $sessions ): array {
9999

100100
foreach ( $sessions as $name => $session ) {
101101
foreach ( $session->listTools()->tools as $mcp_tool ) {
102-
$parameters = json_decode( json_encode( $mcp_tool->inputSchema->jsonSerialize() ), true );
102+
$parameters = json_decode(
103+
json_encode( $mcp_tool->inputSchema->jsonSerialize(), JSON_THROW_ON_ERROR ),
104+
true,
105+
512,
106+
JSON_THROW_ON_ERROR
107+
);
103108
unset( $parameters['additionalProperties'], $parameters['$schema'] );
104109

105110
// Not having any properties doesn't seem to work.
@@ -127,7 +132,12 @@ protected function get_tools( array $sessions ): array {
127132
128133
// To trigger the jsonSerialize() methods.
129134
// TODO: Return all array items, not just first one.
130-
return json_decode( json_encode( $result->content[0] ), true );
135+
return json_decode(
136+
json_encode( $result->content[0], JSON_THROW_ON_ERROR ),
137+
true,
138+
512,
139+
JSON_THROW_ON_ERROR
140+
);
131141
},
132142
];
133143
}

src/MCP/Client.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public function connect(
6363
// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
6464
$url_parts = parse_url( $command_or_url );
6565

66-
if ( isset( $url_parts['scheme'] ) && in_array( strtolower( $url_parts['scheme'] ), [ 'http', 'https' ], true ) ) {
66+
if (
67+
isset( $url_parts['scheme'], $url_parts['host'] ) && in_array( strtolower( $url_parts['scheme'] ), [ 'http', 'https' ], true )
68+
) {
6769
$options = [
6870
// Just for local debugging.
6971
'verify' => false,
@@ -72,7 +74,7 @@ public function connect(
7274
$options['auth'] = [ $url_parts['user'], $url_parts['pass'] ];
7375
}
7476

75-
$url = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'];
77+
$url = $url_parts['scheme'] . '://' . $url_parts['host'] . ( $url_parts['path'] ?? '' );
7678

7779
$transport = new HttpTransport( $url, $options, $this->logger );
7880

src/MCP/Servers/WP_CLI/Tools/CliCommands.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ public function get_tools(): array {
4747

4848
$command_desc = $command->get_shortdesc();
4949
$command_synopsis = $command->get_synopsis();
50-
$synopsis_spec = SynopsisParser::parse( $command_synopsis );
50+
51+
/**
52+
* Parsed synopsys.
53+
*
54+
* @var array<int, array{optional?: bool, type: string, repeating: bool, name: string}> $synopsis_spec
55+
*/
56+
$synopsis_spec = SynopsisParser::parse( $command_synopsis );
5157

5258
$properties = [];
5359
$required = [];
@@ -64,7 +70,7 @@ public function get_tools(): array {
6470
$prop_name = str_replace( '-', '_', $arg['name'] );
6571
$properties[ $prop_name ] = [
6672
'type' => 'string',
67-
'description' => $arg['description'] ?? "Parameter {$arg['name']}",
73+
'description' => "Parameter {$arg['name']}",
6874
];
6975

7076
if ( ! isset( $arg['optional'] ) || ! $arg['optional'] ) {
@@ -133,7 +139,13 @@ public function get_tools(): array {
133139
$subcommand_name = $subcommand->get_name();
134140
$subcommand_desc = $subcommand->get_shortdesc() ?? "Runs WP-CLI command: $subcommand_name";
135141
$subcommand_synopsis = $subcommand->get_synopsis();
136-
$synopsis_spec = SynopsisParser::parse( $subcommand_synopsis );
142+
143+
/**
144+
* Parsed synopsys.
145+
*
146+
* @var array<int, array{optional?: bool, type: string, repeating: bool, name: string}> $synopsis_spec
147+
*/
148+
$synopsis_spec = SynopsisParser::parse( $subcommand_synopsis );
137149

138150
$properties = [];
139151
$required = [];
@@ -149,7 +161,7 @@ public function get_tools(): array {
149161
if ( 'positional' === $arg['type'] || 'assoc' === $arg['type'] ) {
150162
$properties[ $prop_name ] = [
151163
'type' => 'string',
152-
'description' => $arg['description'] ?? "Parameter {$arg['name']}",
164+
'description' => "Parameter {$arg['name']}",
153165
];
154166
}
155167

src/Utils/McpConfig.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ public function get_config() {
2525
}
2626

2727
$json_content = file_get_contents( $config_file );
28-
$config = json_decode( $json_content, true );
28+
29+
if ( false === $json_content ) {
30+
return [];
31+
}
32+
33+
$config = json_decode( $json_content, true, 512, JSON_THROW_ON_ERROR );
2934
return null !== $config ? (array) $config : [];
3035
}
3136

0 commit comments

Comments
 (0)