Update codestyle based on updated Symfony rules (#229)

This commit is contained in:
Christopher Hertel 2026-02-01 11:54:31 +01:00 • committed by GitHub
commit c081b90d74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 165 additions and 168 deletions

View file

@ -33,7 +33,7 @@ class EnumCompletionProvider implements ProviderInterface
}
$this->values = array_map(
fn ($case) => isset($case->value) && \is_string($case->value) ? $case->value : $case->name,
static fn ($case) => isset($case->value) && \is_string($case->value) ? $case->value : $case->name,
$enumClass::cases()
);
}
@ -46,7 +46,7 @@ class EnumCompletionProvider implements ProviderInterface
return array_values(array_filter(
$this->values,
fn (string $value) => str_starts_with($value, $currentValue)
static fn (string $value) => str_starts_with($value, $currentValue)
));
}
}

View file

@ -32,7 +32,7 @@ class ListCompletionProvider implements ProviderInterface
return array_values(array_filter(
$this->values,
fn (string $value) => str_starts_with($value, $currentValue)
static fn (string $value) => str_starts_with($value, $currentValue)
));
}
}

View file

@ -311,7 +311,7 @@ final class SchemaGenerator implements SchemaGeneratorInterface
// If no items specified by Schema attribute, infer from type
if (!isset($paramSchema['items'])) {
$itemJsonTypes = $this->mapPhpTypeToJsonSchemaType($paramInfo['type_string']);
$nonNullItemTypes = array_filter($itemJsonTypes, fn ($t) => 'null' !== $t);
$nonNullItemTypes = array_filter($itemJsonTypes, static fn ($t) => 'null' !== $t);
if (1 === \count($nonNullItemTypes)) {
$paramSchema['items'] = ['type' => $nonNullItemTypes[0]];
@ -574,7 +574,7 @@ final class SchemaGenerator implements SchemaGeneratorInterface
$types[] = $this->getTypeStringFromReflection($innerType, $innerType->allowsNull());
}
if ($nativeAllowsNull) {
$types = array_filter($types, fn ($t) => 'null' !== strtolower($t));
$types = array_filter($types, static fn ($t) => 'null' !== strtolower($t));
}
$typeString = implode('|', array_unique(array_filter($types)));
} elseif ($type instanceof \ReflectionIntersectionType) {
@ -619,7 +619,7 @@ final class SchemaGenerator implements SchemaGeneratorInterface
// Remove leading backslash from class names, but handle built-ins like 'int' or unions like 'int|string'
if (str_contains($typeString, '\\')) {
$parts = preg_split('/([|&])/', $typeString, -1, \PREG_SPLIT_DELIM_CAPTURE);
$processedParts = array_map(fn ($part) => str_starts_with($part, '\\') ? ltrim($part, '\\') : $part, $parts);
$processedParts = array_map(static fn ($part) => str_starts_with($part, '\\') ? ltrim($part, '\\') : $part, $parts);
$typeString = implode('', $processedParts);
}

View file

@ -145,10 +145,10 @@ class SchemaValidator
}
return $obj;
} else {
// It's a list (sequential array), convert items recursively
return array_map([$this, 'convertDataForValidator'], $data);
}
// It's a list (sequential array), convert items recursively
return array_map([$this, 'convertDataForValidator'], $data);
} elseif (\is_object($data) && $data instanceof \stdClass) {
// Deep copy/convert stdClass objects as well
$obj = new \stdClass();
@ -194,7 +194,7 @@ class SchemaValidator
if (empty($pathComponents)) {
return '/';
}
$escapedComponents = array_map(function ($component) {
$escapedComponents = array_map(static function ($component) {
$componentStr = (string) $component;
return str_replace(['~', '/'], ['~0', '~1'], $componentStr);
@ -215,7 +215,7 @@ class SchemaValidator
switch (strtolower($keyword)) {
case 'required':
$missing = $args['missing'] ?? [];
$formattedMissing = implode(', ', array_map(fn ($p) => "`{$p}`", $missing));
$formattedMissing = implode(', ', array_map(static fn ($p) => "`{$p}`", $missing));
$message = "Missing required properties: {$formattedMissing}.";
break;
case 'type':
@ -236,7 +236,7 @@ class SchemaValidator
if (empty($allowedValues)) {
$message = 'Value does not match the allowed enumeration.';
} else {
$formattedAllowed = array_map(function ($v) { /* ... formatting logic ... */
$formattedAllowed = array_map(static function ($v) { /* ... formatting logic ... */
if (\is_string($v)) {
return '"'.$v.'"';
}
@ -309,7 +309,7 @@ class SchemaValidator
break;
case 'additionalProperties': // Corrected casing
$unexpected = $args['properties'] ?? [];
$formattedUnexpected = implode(', ', array_map(fn ($p) => "`{$p}`", $unexpected));
$formattedUnexpected = implode(', ', array_map(static fn ($p) => "`{$p}`", $unexpected));
$message = "Object contains unexpected additional properties: {$formattedUnexpected}.";
break;
case 'format':
@ -320,7 +320,7 @@ class SchemaValidator
$builtInMessage = $error->message();
if ($builtInMessage && 'The data must match the schema' !== $builtInMessage) {
$placeholders = $args;
$builtInMessage = preg_replace_callback('/\{(\w+)\}/', function ($match) use ($placeholders) {
$builtInMessage = preg_replace_callback('/\{(\w+)\}/', static function ($match) use ($placeholders) {
$key = $match[1];
$value = $placeholders[$key] ?? '{'.$key.'}';

View file

@ -86,7 +86,7 @@ final class ResourceResultFormatter
}
if ($allAreEmbeddedResource && $hasEmbeddedResource) {
return array_map(fn ($item) => $item->resource, $readResult);
return array_map(static fn ($item) => $item->resource, $readResult);
}
if ($hasResourceContents || $hasEmbeddedResource) {

View file

@ -344,10 +344,10 @@ final class Registry implements RegistryInterface
public function getDiscoveryState(): DiscoveryState
{
return new DiscoveryState(
tools: array_filter($this->tools, fn ($tool) => !$tool->isManual),
resources: array_filter($this->resources, fn ($resource) => !$resource->isManual),
prompts: array_filter($this->prompts, fn ($prompt) => !$prompt->isManual),
resourceTemplates: array_filter($this->resourceTemplates, fn ($template) => !$template->isManual),
tools: array_filter($this->tools, static fn ($tool) => !$tool->isManual),
resources: array_filter($this->resources, static fn ($resource) => !$resource->isManual),
prompts: array_filter($this->prompts, static fn ($prompt) => !$prompt->isManual),
resourceTemplates: array_filter($this->resourceTemplates, static fn ($template) => !$template->isManual),
);
}

View file

@ -185,19 +185,17 @@ final class ReferenceHandler implements ReferenceHandlerInterface
}
return $value;
} else {
if (\is_string($argument)) {
foreach ($typeName::cases() as $case) {
if ($case->name === $argument) {
return $case;
}
}
$validNames = array_map(fn ($c) => $c->name, $typeName::cases());
throw new InvalidArgumentException("Invalid value '{$argument}' for unit enum {$typeName}. Expected one of: ".implode(', ', $validNames).'.');
} else {
throw new InvalidArgumentException("Invalid value type '{$argument}' for unit enum {$typeName}. Expected a string matching a case name.");
}
}
if (\is_string($argument)) {
foreach ($typeName::cases() as $case) {
if ($case->name === $argument) {
return $case;
}
}
$validNames = array_map(static fn ($c) => $c->name, $typeName::cases());
throw new InvalidArgumentException("Invalid value '{$argument}' for unit enum {$typeName}. Expected one of: ".implode(', ', $validNames).'.');
}
throw new InvalidArgumentException("Invalid value type '{$argument}' for unit enum {$typeName}. Expected a string matching a case name.");
}
try {

View file

@ -46,7 +46,7 @@ class Annotations implements \JsonSerializable
}
if (null !== $this->audience) {
foreach ($this->audience as $role) {
if (!($role instanceof Role)) {
if (!$role instanceof Role) {
throw new InvalidArgumentException('All audience members must be instances of Role enum.');
}
}
@ -60,7 +60,7 @@ class Annotations implements \JsonSerializable
{
$audience = null;
if (isset($data['audience']) && \is_array($data['audience'])) {
$audience = array_map(fn (string $r) => Role::from($r), $data['audience']);
$audience = array_map(static fn (string $r) => Role::from($r), $data['audience']);
}
return new self(
@ -76,7 +76,7 @@ class Annotations implements \JsonSerializable
{
$data = [];
if (null !== $this->audience) {
$data['audience'] = array_map(fn (Role $r) => $r->value, $this->audience);
$data['audience'] = array_map(static fn (Role $r) => $r->value, $this->audience);
}
if (null !== $this->priority) {
$data['priority'] = $this->priority;

View file

@ -47,7 +47,7 @@ class Prompt implements \JsonSerializable
) {
if (null !== $this->arguments) {
foreach ($this->arguments as $arg) {
if (!($arg instanceof PromptArgument)) {
if (!$arg instanceof PromptArgument) {
throw new InvalidArgumentException('All items in Prompt "arguments" must be PromptArgument instances.');
}
}
@ -64,7 +64,7 @@ class Prompt implements \JsonSerializable
}
$arguments = null;
if (isset($data['arguments']) && \is_array($data['arguments'])) {
$arguments = array_map(fn (array $argData) => PromptArgument::fromArray($argData), $data['arguments']);
$arguments = array_map(static fn (array $argData) => PromptArgument::fromArray($argData), $data['arguments']);
}
if (!empty($data['_meta']) && !\is_array($data['_meta'])) {

View file

@ -50,7 +50,7 @@ class ListPromptsResult implements ResultInterface
}
return new self(
array_map(fn (array $prompt) => Prompt::fromArray($prompt), $data['prompts']),
array_map(static fn (array $prompt) => Prompt::fromArray($prompt), $data['prompts']),
$data['nextCursor'] ?? null
);
}

View file

@ -50,7 +50,7 @@ class ListResourceTemplatesResult implements ResultInterface
}
return new self(
array_map(fn (array $resourceTemplate) => ResourceTemplate::fromArray($resourceTemplate), $data['resourceTemplates']),
array_map(static fn (array $resourceTemplate) => ResourceTemplate::fromArray($resourceTemplate), $data['resourceTemplates']),
$data['nextCursor'] ?? null
);
}

View file

@ -50,7 +50,7 @@ class ListResourcesResult implements ResultInterface
}
return new self(
array_map(fn (array $resource) => ResourceSchema::fromArray($resource), $data['resources']),
array_map(static fn (array $resource) => ResourceSchema::fromArray($resource), $data['resources']),
$data['nextCursor'] ?? null
);
}

View file

@ -50,7 +50,7 @@ class ListToolsResult implements ResultInterface
}
return new self(
array_map(fn (array $tool) => Tool::fromArray($tool), $data['tools']),
array_map(static fn (array $tool) => Tool::fromArray($tool), $data['tools']),
$data['nextCursor'] ?? null
);
}

View file

@ -173,7 +173,7 @@ class Protocol
try {
/** @var McpFiber $fiber */
$fiber = new \Fiber(fn () => $handler->handle($request, $session));
$fiber = new \Fiber(static fn () => $handler->handle($request, $session));
$result = $fiber->start();
@ -192,11 +192,10 @@ class Protocol
$transport->attachFiberToSession($fiber, $session->getId());
return;
} else {
$finalResult = $fiber->getReturn();
$this->sendResponse($transport, $finalResult, $session);
}
$finalResult = $fiber->getReturn();
$this->sendResponse($transport, $finalResult, $session);
} catch (\InvalidArgumentException $e) {
$this->logger->warning(\sprintf('Invalid argument: %s', $e->getMessage()), ['exception' => $e]);
@ -581,7 +580,7 @@ class Protocol
if (!empty($deletedSessions)) {
$this->logger->debug('Garbage collected expired sessions.', [
'count' => \count($deletedSessions),
'session_ids' => array_map(fn (Uuid $id) => $id->toRfc4122(), $deletedSessions),
'session_ids' => array_map(static fn (Uuid $id) => $id->toRfc4122(), $deletedSessions),
]);
}
}