Routing::render()   C
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 5
nop 2
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      https://fastdlabs.com
8
 */
9
10
namespace FastD\Console;
11
12
use FastD\Http\ServerRequest;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Helper\Table;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * Class Route.
22
 */
23
class Routing extends Command
24
{
25
    public function configure()
26
    {
27
        $this
28
            ->setName('route')
29
            ->setHelp('Show all route')
30
            ->setDescription('Show you defined routes.')
31
            ->addArgument('method', InputArgument::OPTIONAL, 'Route request method.')
32
            ->addArgument('path', InputArgument::OPTIONAL, 'Route path.')
33
            ->addOption('data', 'd', InputOption::VALUE_OPTIONAL, 'Path request data.')
34
            ->addOption('all', 'a', InputOption::VALUE_OPTIONAL, 'Test all routes')
35
        ;
36
    }
37
38
    /**
39
     * @param InputInterface  $input
40
     * @param OutputInterface $output
41
     *
42
     * @return mixed
43
     */
44
    public function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        if (empty($path = $input->getArgument('path'))) {
47
            $this->render($input, $output);
48
        } else {
49
            $this->test($input->getArgument('method'), $path, $input, $output);
50
        }
51
52
        return 0;
53
    }
54
55
    /**
56
     * @param $method
57
     * @param $path
58
     * @param InputInterface  $input
59
     * @param OutputInterface $output
60
     */
61
    protected function test($method, $path, InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        $request = new ServerRequest($method, $path, [
64
            'User-Agent' => 'FastD Console/'.version(),
65
        ]);
66
67
        $response = app()->handleRequest($request);
68
69
        $output->writeln(sprintf('Method: <info>%s</info>', $method));
70
        $output->writeln(sprintf('Path: <info>%s</info>', $path).PHP_EOL);
71
72
        $headersLIne = '';
73
74 View Code Duplication
        foreach ($response->getHeaders() as $name => $header) {
0 ignored issues
show
Bug introduced by
The method getHeaders does only exist in FastD\Http\Response, but not in Symfony\Component\HttpFoundation\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
            $headersLIne .= $name.': '.$response->getHeaderLine($name).PHP_EOL;
0 ignored issues
show
Bug introduced by
The method getHeaderLine does only exist in FastD\Http\Response, but not in Symfony\Component\HttpFoundation\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76
        }
77
78
        $body = (string) $response->getBody();
0 ignored issues
show
Bug introduced by
The method getBody does only exist in FastD\Http\Response, but not in Symfony\Component\HttpFoundation\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
79
        $body = json_encode(json_decode($body, true), JSON_PRETTY_PRINT);
80
        $output->writeln($headersLIne.PHP_EOL.$body);
81
    }
82
83
    /**
84
     * @param InputInterface  $input
85
     * @param OutputInterface $output
86
     */
87
    protected function render(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
        $table = new Table($output);
90
        $rows = [];
91
        $table->setHeaders(array('Path', 'Method', 'Callback', 'Middleware'));
92
        foreach (route()->aliasMap as $routes) {
93
            foreach ($routes as $route) {
94
                $m = [];
95
                $middleware = $route->getMiddleware();
96
                if (is_array($middleware)) {
97
                    foreach ($middleware as $value) {
98
                        if (is_object($value)) {
99
                            $m[] = get_class($value);
100
                        } else {
101
                            $m[] = $value;
102
                        }
103
                    }
104
                } elseif (is_object($middleware)) {
105
                    $m[] = get_class($middleware);
106
                }
107
                $rows[] = [
108
                    $route->getPath(),
109
                    $route->getMethod(),
110
                    $route->getCallback(),
111
                    implode(',', $m),
112
                ];
113
            }
114
        }
115
116
        $table->setRows($rows);
117
118
        $table->render();
119
    }
120
}
121