Passed
Push — master ( 3388c8...01e728 )
by Iman
08:15 queued 02:52
created

RouteController::routeController()   C

Complexity

Conditions 8
Paths 22

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 22
nop 3
dl 0
loc 23
rs 6.1403
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\CBCoreModule;
4
5
use Route;
6
7
class RouteController
8
{
9
    public static function routeController($prefix, $controller, $namespace = null)
10
    {
11
        $prefix = trim($prefix, '/').'/';
12
13
        $namespace = ($namespace) ?: ctrlNamespace();
14
15
        try {
16
            Route::get($prefix, ['uses' => $controller.'@getIndex', 'as' => $controller.'GetIndex']);
17
18
            $controller_methods = (new \ReflectionClass($namespace.'\\'.$controller))->getMethods(\ReflectionMethod::IS_PUBLIC);
19
            $wildcards = '/{one?}/{two?}/{three?}/{four?}/{five?}';
20
            foreach ($controller_methods as $method) {
21
22
                if ($method->class == 'Illuminate\Routing\Controller' || $method->name == 'getIndex') {
23
                    continue;
24
                }
25
                if (substr($method->name, 0, 3) == 'get') {
26
                    self::routeGet($prefix, $controller, $method, $wildcards);
27
                } elseif (substr($method->name, 0, 4) == 'post') {
28
                    self::routePost($prefix, $controller, $method, $wildcards);
29
                }
30
            }
31
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
32
33
        }
34
    }
35
36
    /**
37
     * @param $prefix
38
     * @param $controller
39
     * @param $method
40
     * @param $wildcards
41
     * @return array
42
     */
43
    private static function routePost($prefix, $controller, $method, $wildcards)
44
    {
45
        $methodName = substr($method->name, 4);
46
        $slug = array_filter(preg_split('/(?=[A-Z])/', $methodName));
0 ignored issues
show
Bug introduced by
It seems like preg_split('/(?=[A-Z])/', $methodName) can also be of type false; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        $slug = array_filter(/** @scrutinizer ignore-type */ preg_split('/(?=[A-Z])/', $methodName));
Loading history...
47
        $slug = strtolower(implode('-', $slug));
48
        Route::post($prefix.$slug.$wildcards, [
49
            'uses' => $controller.'@'.$method->name,
50
            'as' => $controller.'Post'.$methodName,
51
        ]);
52
    }
53
54
    /**
55
     * @param $prefix
56
     * @param $controller
57
     * @param $method
58
     * @param $wildcards
59
     */
60
    private static function routeGet($prefix, $controller, $method, $wildcards)
61
    {
62
        $methodName = substr($method->name, 3);
63
        $slug = array_filter(preg_split('/(?=[A-Z])/', $methodName));
0 ignored issues
show
Bug introduced by
It seems like preg_split('/(?=[A-Z])/', $methodName) can also be of type false; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        $slug = array_filter(/** @scrutinizer ignore-type */ preg_split('/(?=[A-Z])/', $methodName));
Loading history...
64
        $slug = strtolower(implode('-', $slug));
65
        $slug = ($slug == 'index') ? '' : $slug;
66
        Route::get($prefix.$slug.$wildcards,
67
            ['uses' => $controller.'@'.$method->name,
68
            'as' => $controller.'Get'.$methodName]);
69
    }
70
}