Skip to content

Commit d0a4427

Browse files
authored
feat(doctrine): enhance getLinksHandler with method validation and typo suggestions (#6874)
* feat: enhance getLinksHandler with method validation and typo suggestions - Add strict validation for method existence when handling callable arrays ([ClassName, MethodName]). - Introduce `findSimilarMethod` to suggest the closest matching method name if a typo is detected. - Improve error messaging by including suggestions for likely intended methods. - Ensure robust error handling for `handleLinks` in `getLinksHandler`. * Coding: apply CsFixer * Coding: apply CsFixer * Coding: apply CsFixer * Coding: apply CsFixer * Coding: apply CsFixer
1 parent 57f15cf commit d0a4427

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/Doctrine/Common/State/LinksHandlerLocatorTrait.php

+23
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,33 @@ private function getLinksHandler(Operation $operation): ?callable
3535
return $handleLinks;
3636
}
3737

38+
if (\is_array($handleLinks) && 2 === \count($handleLinks) && class_exists($handleLinks[0])) {
39+
[$className, $methodName] = $handleLinks;
40+
41+
if (method_exists($className, $methodName)) {
42+
return $handleLinks;
43+
}
44+
45+
$suggestedMethod = $this->findSimilarMethod($className, $methodName);
46+
47+
throw new RuntimeException(\sprintf('Method "%s" does not exist in class "%s".%s', $methodName, $className, $suggestedMethod ? \sprintf(' Did you mean "%s"?', $suggestedMethod) : ''));
48+
}
49+
3850
if ($this->handleLinksLocator && \is_string($handleLinks) && $this->handleLinksLocator->has($handleLinks)) {
3951
return [$this->handleLinksLocator->get($handleLinks), 'handleLinks'];
4052
}
4153

4254
throw new RuntimeException(\sprintf('Could not find handleLinks service "%s"', $handleLinks));
4355
}
56+
57+
private function findSimilarMethod(string $className, string $methodName): ?string
58+
{
59+
$methods = get_class_methods($className);
60+
61+
$similarMethods = array_filter($methods, function ($method) use ($methodName) {
62+
return levenshtein($methodName, $method) <= 3;
63+
});
64+
65+
return $similarMethods ? reset($similarMethods) : null;
66+
}
4467
}

0 commit comments

Comments
 (0)