ClosureDispatcher   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 23.81 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 10
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A call() 10 21 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace JetFire\Routing\Dispatcher;
4
5
use JetFire\Routing\Response;
6
use JetFire\Routing\ResponseInterface;
7
use JetFire\Routing\Route;
8
use JetFire\Routing\RouteCollection;
9
use JetFire\Routing\Router;
10
11
/**
12
 * Class ClosureDispatcher
13
 * @package JetFire\Routing\Dispatcher
14
 */
15
class ClosureDispatcher implements DispatcherInterface
16
{
17
    /**
18
     * @var Router
19
     */
20
    private $router;
21
22
    /**
23
     * @param Router $router
24
     */
25
    public function __construct(Router $router)
26
    {
27
        $this->router = $router;
28
    }
29
30
31
    /**
32
     * @description call anonymous function
33
     */
34
    public function call()
35
    {
36
        $classInstance = [
37
            Response::class => $this->router->response,
38
            Route::class => $this->router->route,
39
            RouteCollection::class => $this->router->collection,
40
        ];
41
42
        $params = empty($this->router->route->getParameters()) ? $classInstance : array_merge($this->router->route->getParameters(), $classInstance);
43
        $content = call_user_func_array($this->router->route->getTarget('closure'), $params);
44 View Code Duplication
        if ($content instanceof ResponseInterface) {
0 ignored issues
show
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...
45
            $this->router->response = $content;
46
        } else {
47
            if (is_array($content)) {
48
                $this->router->route->addTarget('data', $content);
49
                $content = json_encode($content);
50
            }
51
            $this->router->callMiddleware('between');
52
            $this->router->response->setContent($content);
53
        }
54
    }
55
56
}
57