Completed
Push — master ( a18769...fd1d5b )
by Gianluca
09:21 queued 04:44
created

App::triggerWithException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 5
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Penny;
4
5
use Exception;
6
use RuntimeException;
7
use Penny\Config\Loader;
8
use Penny\Event\PennyEventInterface;
9
use Penny\Route\RouteInfoInterface;
10
use Interop\Container\ContainerInterface;
11
use Zend\EventManager\EventManager;
12
13
class App
14
{
15
    /**
16
     * Dependency Injection container.
17
     *
18
     * @var ContainerInterface
19
     */
20
    private $container;
21
22
    /**
23
     * Application initialization.
24
     *
25
     * @param ContainerInterface $container Dependency Injection container.
26
     *
27
     * @throws Exception If no router is defined.
28
     */
29 36
    public function __construct(ContainerInterface $container = null)
30
    {
31 36
        if ($container === null) {
32 4
            $container = Container\PHPDiFactory::buildContainer(Loader::load());
33 2
        }
34
35 36
        if ($container->has('router') === false) {
36 2
            throw new Exception('Define router config');
37
        }
38
39 36
        $this->container = $container;
40 36
    }
41
42
    /**
43
     * Container getter.
44
     *
45
     * @return ContainerInterface
46
     */
47 36
    public function getContainer()
48
    {
49 36
        return $this->container;
50
    }
51
52
    /**
53
     * Penny dispatcher getter.
54
     *
55
     * @return Dispatcher
56
     */
57 30
    private function getDispatcher()
58
    {
59 30
        $dispatcher = $this->container->get('dispatcher');
60 30
        if (!is_callable($dispatcher)) {
61 2
            throw new \RuntimeException('Dispatcher must be a callable');
62
        }
63
64 28
        return $dispatcher;
65
    }
66
67
    /**
68
     * Penny HTTP flow event getter.
69
     *
70
     * @return EventManager
71
     */
72 28
    private function getEventManager()
73
    {
74 28
        return $this->container->get('event_manager');
75 1
    }
76
77
    /**
78
     * Application execution.
79
     *
80
     * @param mixed|null $request  Representation of an outgoing,
81
     *  client-side request.
82
     * @param mixed|null $response Representation of an incoming,
83
     *  server-side response.
84
     *
85
     * @return mixed
86
     */
87 32
    public function run($request = null, $response = null)
88
    {
89 32
        $event = $this->getContainer()->get('http_flow_event');
90 32
        if (!($event instanceof PennyEventInterface)) {
91 2
            throw new RuntimeException('This event did not supported');
92
        }
93 30
        if ($request !== null) {
94 30
            $event->setRequest($request);
95 15
        }
96 30
        if ($response !== null) {
97 30
            $event->setResponse($response);
98 15
        }
99
100 30
        $dispatcher = $this->getDispatcher();
101 28
        $eventManager = $this->getEventManager();
102
103
        try {
104 28
            $routeInfo = call_user_func($dispatcher, $event->getRequest());
105
106 22
            if (!($routeInfo instanceof RouteInfoInterface)) {
107 2
                throw new RuntimeException('Dispatch does not return RouteInfo object');
108
            }
109
110 20
            $event->setRouteInfo($routeInfo);
111 20
            $event->setName($routeInfo->getName());
112 18
        } catch (Exception $exception) {
113 8
            return $this->triggerWithException($eventManager, $event, 'dispatch_error', $exception)
0 ignored issues
show
Bug introduced by
The method getResponse does only exist in Penny\Event\PennyEventInterface, but not in Cake\Event\Event and Zen...tManager\EventInterface.

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...
114 8
                        ->getResponse();
115
        }
116
117 20
        $eventManager->attach($event->getName(), function ($event) use ($routeInfo) {
118 18
            $event->setResponse(call_user_func_array(
119 18
                $routeInfo->getCallable(),
120 18
                [$event->getRequest(), $event->getResponse()] + $routeInfo->getParams()
121 9
            ));
122 20
        }, 0);
123
124
        try {
125 20
            $eventManager->trigger($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<Penny\Event\PennyEventInterface>, but the function expects a string|object<Zend\EventManager\EventInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126 11
        } catch (Exception $exception) {
127 2
            $this->triggerWithException($eventManager, $event, $routeInfo->getName().'_error', $exception);
128
        }
129
130 18
        return $event->getResponse();
131
    }
132
133
    /**
134
     * Event Manager trigger with exception
135
     *
136
     * @param \Zend\EventManager\EventManager|\Cake\Event\EventManager $eventManager
137
     * @param PennyEventInterface|\Zend\EventManager\EventInterface|\Cake\Event\Event $event
138
     * @param string $name
139
     * @param Exception $exception
140
     *
141
     * @return PennyEventInterface|\Zend\EventManager\EventInterface|\Cake\Event\Event
142
     */
143 10
    private function triggerWithException($eventManager, $event, $name, Exception $exception)
144
    {
145 10
        $event->setName($name);
0 ignored issues
show
Bug introduced by
The method setName does only exist in Penny\Event\PennyEventIn...tManager\EventInterface, but not in Cake\Event\Event.

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...
146 10
        $event->setException($exception);
0 ignored issues
show
Bug introduced by
The method setException does only exist in Penny\Event\PennyEventInterface, but not in Cake\Event\Event and Zen...tManager\EventInterface.

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...
147 10
        $eventManager->trigger($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by parameter $event on line 143 can also be of type object<Cake\Event\Event> or object<Penny\Event\PennyEventInterface>; however, Zend\EventManager\EventManager::trigger() does only seem to accept string|object<Zend\EventManager\EventInterface>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
The method trigger does only exist in Zend\EventManager\EventManager, but not in Cake\Event\EventManager.

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...
148
149 8
        return $event;
150
    }
151
}
152