GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Service::getMethodArgs()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.0534
cc 4
eloc 12
nc 4
nop 3
1
<?php
2
3
namespace Saltwater\Salt;
4
5
use Saltwater\Server as S;
6
7
/**
8
 * Services encapsulate application logic
9
 *
10
 * @package Saltwater\Salt
11
 */
12
class Service
13
{
14
    /** @var Context */
15
    protected $context = null;
16
17
    /** @var string */
18
    protected $module = null;
19
20
    /**
21
     * @param Context $context
22
     * @param string  $module
23
     *
24
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
25
     */
26
    public function __construct($context = null, $module = null)
27
    {
28
        $this->setContext($context);
0 ignored issues
show
Bug introduced by
It seems like $context defined by parameter $context on line 26 can be null; however, Saltwater\Salt\Service::setContext() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
29
30
        if (is_null($module) && !empty($context->module)) {
31
            $module = $context->module->getName();
32
        }
33
34
        $this->setModule($module);
35
    }
36
37
    /**
38
     * @param Context $context
39
     *
40
     * @return void
41
     */
42
    public function setContext($context)
43
    {
44
        $this->context = $context;
45
    }
46
47
    /**
48
     * @param string $module
49
     *
50
     * @return void
51
     */
52
    public function setModule($module)
53
    {
54
        $this->module = $module;
55
    }
56
57
    /**
58
     * Ensure that a method can be called within this service
59
     *
60
     * @param string $method
61
     *
62
     * @return bool
63
     */
64
    public function isCallable($method)
65
    {
66
        return method_exists($this, $method);
67
    }
68
69
    /**
70
     * Prepare the calling of a method
71
     *
72
     * @param object $call
73
     *
74
     * @return bool
75
     */
76
    public function prepareCall($call)
77
    {
78
        return $this->isCallable($call->function);
79
    }
80
81
    /**
82
     * Attempt to execute a call on this service
83
     *
84
     * @param object $call
85
     * @param mixed  $data
86
     *
87
     * @return mixed
88
     */
89
    public function call($call, $data = null)
90
    {
91
        if (!$this->isCallable($call->function)) {
92
            return null;
93
        }
94
95
        return $this->executeCall($call, $call->function, $data);
96
    }
97
98
    /**
99
     * Execute a call
100
     *
101
     * @param object $call
102
     * @param string $method
103
     * @param mixed  $data
104
     *
105
     * @return mixed
106
     */
107
    protected function executeCall($call, $method, $data)
108
    {
109
        $reflect = new \ReflectionMethod($this, $method);
110
111
        // Check whether we need to inject parameters
112
        if ($reflect->getNumberOfParameters()) {
113
            return call_user_func_array(
114
                array($this, $method),
115
                $this->getMethodArgs($reflect, $call->path, $data)
116
            );
117
        }
118
119
        // No parameter assembly necessary
120
        return call_user_func(array($this, $method));
121
    }
122
123
    /**
124
     * Assemble injected method parameters
125
     *
126
     * Note: $path and $data are reserved parameters
127
     *
128
     * @param \ReflectionMethod $reflect
129
     * @param string            $path
130
     * @param mixed             $data
131
     *
132
     * @return array
133
     */
134
    private function getMethodArgs($reflect, $path, $data)
135
    {
136
        $args = array();
137
        foreach ($reflect->getParameters() as $parameter) {
138
            $name = $parameter->getName();
139
140
            if ($name == 'path') {
141
                $args[] = $path;
142
                continue;
143
            }
144
145
            if ($name == 'data') {
146
                $args[] = $data;
147
                continue;
148
            }
149
150
            $args[] = S::$n->provider($name, $this->module);
151
        }
152
153
        return $args;
154
    }
155
}
156