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

ICanBoogie / bind-routing / 4495689867

pending completion
4495689867

push

github

Olivier Laviale
Add Route attributes

62 of 62 new or added lines in 8 files covered. (100.0%)

77 of 126 relevant lines covered (61.11%)

0.62 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

7
use ICanBoogie\Routing\RouteMaker;
8
use RuntimeException;
9

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

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

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

31
    ];
32

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

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

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

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

50
        $base = pluralize(hyphenate($unqualified_class));
1✔
51

52
        return "$base:$name";
1✔
53
    }
54

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

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

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

67
                break;
×
68
            }
69
        }
70

71
        return $method;
×
72
    }
73
}
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