SplittingServiceActivator::callService()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 4 Features 0
Metric Value
c 5
b 4
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace PEIP\Service;
4
5
/*
6
 * This file is part of the PEIP package.
7
 * (c) 2009-2011 Timo Michna <timomichna/yahoo.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
/**
14
 * \PEIP\Service\ServiceActivator.
15
 *
16
 * Calls the service method with the content of the Message (array) as arguments
17
 *
18
 * @author Timo Michna <timomichna/yahoo.de>
19
 * @extends PEIP\ABS\Service\ServiceActivator
20
 * @implements \PEIP\INF\Message\MessageBuilder, \PEIP\INF\Handler\Handler, \PEIP\INF\Channel\Channel, \PEIP\INF\Channel\SubscribableChannel, \PEIP\INF\Event\Connectable
21
 */
22
class SplittingServiceActivator extends \PEIP\Service\ServiceActivator
23
{
24
    /**
25
     * Calls a method on a service (registered as a callable) with
26
     * content/payload (array) of given message as arguments.
27
     *
28
     * @param \PEIP\INF\Message\Message $message message to call the service with it�s content/payload
29
     *
30
     * @return mixed result of calling the registered service callable with message content/payload
31
     */
32
    public function callService(\PEIP\INF\Message\Message $message)
33
    {
34
        if (is_callable($this->serviceCallable)) {
35
            $res = call_user_func_array($this->serviceCallable, $message->getContent());
36
        } elseif (is_object($this->serviceCallable) && method_exists($this->serviceCallable, 'handle')) {
37
            $res = call_user_func_array([$this->serviceCallable, 'handle'], $message->getContent());
38
        }
39
40
        return $res;
0 ignored issues
show
Bug introduced by
The variable $res does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
41
    }
42
}
43