Skip to content

Commit 9093883

Browse files
committed
Refactor method matching logic in InlineQueryHandler
Extracted the inline command matching logic from the main loop in `InlineQueryHandler` into a new private method called `GetMethodsWithArguments`. This change simplifies the main loop by delegating the responsibility of finding matching methods to this helper method, improving code readability and maintainability.
1 parent 1d35714 commit 9093883

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

Sources/TelegramBot/Handlers/InlineQueryHandler.cs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,8 @@ public InlineQueryHandler(IReadOnlyCollection<MethodInfo> controllerMethods, Upd
2929
List<MethodInfo> methods = new List<MethodInfo>();
3030
foreach (var method in controllerMethods)
3131
{
32-
var attributes = method.GetCustomAttributes(typeof(InlineCommandAttribute), false);
33-
foreach (var attribute in attributes)
34-
{
35-
if (attribute is InlineCommandAttribute botCommandAttribute)
36-
{
37-
string[] controllerCommandParts = botCommandAttribute.Command.Split('/');
38-
string[] incomingCommandParts = command.Split('/');
39-
if (controllerCommandParts.Length != incomingCommandParts.Length)
40-
{
41-
continue;
42-
}
43-
bool match = true;
44-
for (int i = 0; i < controllerCommandParts.Length; i++)
45-
{
46-
if (controllerCommandParts[i] != incomingCommandParts[i]
47-
&& !controllerCommandParts[i].StartsWith('{')
48-
&& !controllerCommandParts[i].EndsWith('}'))
49-
{
50-
match = false;
51-
break;
52-
}
53-
if (controllerCommandParts[i].StartsWith('{')
54-
&& controllerCommandParts[i].EndsWith('}'))
55-
{
56-
_args.Add(incomingCommandParts[i]);
57-
}
58-
}
59-
if (!match)
60-
{
61-
continue;
62-
}
63-
methods.Add(method);
64-
}
65-
}
32+
var foundMethods = GetMethodsWithArguments(method, command);
33+
methods.AddRange(foundMethods);
6634
}
6735
if (methods.Count == 1)
6836
{
@@ -79,6 +47,46 @@ public InlineQueryHandler(IReadOnlyCollection<MethodInfo> controllerMethods, Upd
7947
return null;
8048
}
8149

50+
private IEnumerable<MethodInfo> GetMethodsWithArguments(MethodInfo method, string command)
51+
{
52+
List<MethodInfo> methods = new List<MethodInfo>();
53+
var attributes = method.GetCustomAttributes(typeof(InlineCommandAttribute), false);
54+
foreach (var attribute in attributes)
55+
{
56+
if (attribute is InlineCommandAttribute botCommandAttribute)
57+
{
58+
string[] controllerCommandParts = botCommandAttribute.Command.Split('/');
59+
string[] incomingCommandParts = command.Split('/');
60+
if (controllerCommandParts.Length != incomingCommandParts.Length)
61+
{
62+
continue;
63+
}
64+
bool match = true;
65+
for (int i = 0; i < controllerCommandParts.Length; i++)
66+
{
67+
if (controllerCommandParts[i] != incomingCommandParts[i]
68+
&& !controllerCommandParts[i].StartsWith('{')
69+
&& !controllerCommandParts[i].EndsWith('}'))
70+
{
71+
match = false;
72+
break;
73+
}
74+
if (controllerCommandParts[i].StartsWith('{')
75+
&& controllerCommandParts[i].EndsWith('}'))
76+
{
77+
_args.Add(incomingCommandParts[i]);
78+
}
79+
}
80+
if (!match)
81+
{
82+
continue;
83+
}
84+
methods.Add(method);
85+
}
86+
}
87+
return methods;
88+
}
89+
8290
public object[]? GetArguments()
8391
{
8492
return _args.Count > 0 ? _args.ToArray() : null;

0 commit comments

Comments
 (0)