Completed
Pull Request — master (#570)
by Marcel
01:55
created

Utils::getRouteActionUses()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.1111
c 0
b 0
f 0
cc 6
nc 6
nop 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Tools;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Routing\Route;
7
use League\Flysystem\Filesystem;
8
use League\Flysystem\Adapter\Local;
9
use Symfony\Component\Console\Output\ConsoleOutput;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class Utils
13
{
14
    public static function getFullUrl(Route $route, array $bindings = []): string
15
    {
16
        $uri = $route->uri();
17
18
        return self::replaceUrlParameterBindings($uri, $bindings);
19
    }
20
21
    /**
22
     * @param array|Route $routeOrAction
23
     *
24
     * @return array|null
25
     */
26
    public static function getRouteClassAndMethodNames($routeOrAction)
27
    {
28
        $action = $routeOrAction instanceof Route ? $routeOrAction->getAction() : $routeOrAction;
0 ignored issues
show
Bug introduced by
The class Illuminate\Routing\Route does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
29
30
        if ($action['uses'] !== null) {
31
            if (is_array($action['uses'])) {
32
                return $action['uses'];
33
            } elseif (is_string($action['uses'])) {
34
                return explode('@', $action['uses']);
35
            }
36
        }
37
        if (array_key_exists(0, $action) && array_key_exists(1, $action)) {
38
            return [
39
                0 => $action[0],
40
                1 => $action[1],
41
            ];
42
        }
43
    }
44
45
    /**
46
     * Transform parameters in URLs into real values (/users/{user} -> /users/2).
47
     * Uses bindings specified by caller, otherwise just uses '1'.
48
     *
49
     * @param string $uri
50
     * @param array $bindings
51
     *
52
     * @return mixed
53
     */
54
    public static function replaceUrlParameterBindings(string $uri, array $bindings)
55
    {
56
        foreach ($bindings as $path => $binding) {
57
            // So we can support partial bindings like
58
            // 'bindings' => [
59
            //  'foo/{type}' => 4,
60
            //  'bar/{type}' => 2
61
            //],
62
            if (Str::is("*$path*", $uri)) {
63
                preg_match('/({.+?})/', $path, $parameter);
64
                $uri = str_replace("{$parameter['1']}", $binding, $uri);
65
            }
66
        }
67
        // Replace any unbound parameters with '1'
68
        $uri = preg_replace('/{(.+?)}/', '1', $uri);
69
70
        return $uri;
71
    }
72
73
    public static function dumpException(\Exception $e)
74
    {
75
        if (class_exists(\NunoMaduro\Collision\Handler::class)) {
76
            $output = new ConsoleOutput(OutputInterface::VERBOSITY_VERBOSE);
77
            $handler = new \NunoMaduro\Collision\Handler(new \NunoMaduro\Collision\Writer($output));
78
            $handler->setInspector(new \Whoops\Exception\Inspector($e));
79
            $handler->setException($e);
80
            $handler->handle();
81
        } else {
82
            dump($e);
83
            echo "You can get better exception output by installing the library \nunomaduro/collision (PHP 7.1+ only).\n";
84
        }
85
    }
86
87
    public static function deleteDirectoryAndContents($dir)
88
    {
89
        $adapter = new Local(realpath(__DIR__.'/../../'));
90
        $fs = new Filesystem($adapter);
91
        $fs->deleteDir($dir);
92
    }
93
}
94