• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / bind-routing / 4501100496

pending completion
4501100496

push

github

Olivier Laviale
Set tag 'action_responder' automatically

15 of 15 new or added lines in 1 file covered. (100.0%)

89 of 146 relevant lines covered (60.96%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.25
/lib/ActionResolver.php
1
<?php
2

3
namespace ICanBoogie\Binding\Routing;
4

5
use ICanBoogie\HTTP\RequestMethod;
6
use ICanBoogie\Routing\RouteMaker;
7
use RuntimeException;
8

9
use function ICanBoogie\hyphenate;
10
use function ICanBoogie\pluralize;
11
use function in_array;
12
use function strlen;
13
use function strrpos;
14
use function strtolower;
15
use function substr;
16

17
final class ActionResolver
18
{
19
    private const CONTROLLER_SUFFIX = 'Controller';
20
    private const ACTIONS = [
21

22
        RouteMaker::ACTION_LIST,
23
        RouteMaker::ACTION_NEW,
24
        RouteMaker::ACTION_CREATE,
25
        RouteMaker::ACTION_SHOW,
26
        RouteMaker::ACTION_EDIT,
27
        RouteMaker::ACTION_UPDATE,
28
        RouteMaker::ACTION_DELETE,
29

30
    ];
31

32
    /**
33
     * @param class-string $class
34
     */
35
    public static function resolve_action(string $class, string $method): string
36
    {
37
        $name = self::extract_action_name($method);
2✔
38

39
        if (!$name) {
2✔
40
            throw new RuntimeException("Unable to resolve action name from $class::$method");
×
41
        }
42

43
        $unqualified_class = substr($class, strrpos($class, '\\') + 1);
2✔
44

45
        if (str_ends_with($unqualified_class, self::CONTROLLER_SUFFIX)) {
2✔
46
            $unqualified_class = substr($unqualified_class, 0, -strlen(self::CONTROLLER_SUFFIX));
2✔
47
        }
48

49
        $base = pluralize(hyphenate($unqualified_class));
2✔
50

51
        return "$base:$name";
2✔
52
    }
53

54
    private static function extract_action_name(string $method): string
55
    {
56
        if (in_array($method, self::ACTIONS)) {
2✔
57
            return $method;
2✔
58
        }
59

60
        foreach (RequestMethod::cases() as $case) {
×
61
            $try = strtolower($case->value) . '_';
×
62

63
            if (str_starts_with($try, $method)) {
×
64
                $method = substr($method, strlen($try));
×
65

66
                break;
×
67
            }
68
        }
69

70
        return $method;
×
71
    }
72
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc