Passed
Push — master ( d94ce5...1e8201 )
by Sávio
11:18
created

SocketHandler::maybeSetServer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 3
nc 2
nop 1
1
<?php
2
3
namespace Conveyor\SocketHandlers\Abstractions;
4
5
use Slim\Container;
0 ignored issues
show
Bug introduced by
The type Slim\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Conveyor\SocketHandlers\Interfaces\SocketHandlerInterface;
7
use Conveyor\Actions\Interfaces\ActionInterface;
8
9
abstract class SocketHandler implements SocketHandlerInterface
10
{
11
    /**
12
     * @param string $data
13
     * @param int $fd
14
     */
15
    public function __invoke(string $data, $fd = null)
16
    {
17
        /** @var ActionInterface */
18
        $action = $this->parseData($data);
19
20
        $this->maybeSetFd($fd);
21
        
22
        return $action->execute();
0 ignored issues
show
Bug introduced by
The call to Conveyor\Actions\Interfa...ionInterface::execute() has too few arguments starting with data. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
        return $action->/** @scrutinizer ignore-call */ execute();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
23
    }
24
25
    /**
26
     * Set $fd (File descriptor) if method "setFd" exists.
27
     *
28
     * @param int|null $fd File descriptor.
29
     *
30
     * @return void
31
     */
32
    public function maybeSetFd($fd = null): void {
33
        $parsedData = $this->getParsedData();
0 ignored issues
show
Bug introduced by
The method getParsedData() does not exist on Conveyor\SocketHandlers\Abstractions\SocketHandler. Since it exists in all sub-types, consider adding an abstract or default implementation to Conveyor\SocketHandlers\Abstractions\SocketHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        /** @scrutinizer ignore-call */ 
34
        $parsedData = $this->getParsedData();
Loading history...
34
        $action = $this->getAction($parsedData['action']);
0 ignored issues
show
Bug introduced by
The method getAction() does not exist on Conveyor\SocketHandlers\Abstractions\SocketHandler. Since it exists in all sub-types, consider adding an abstract or default implementation to Conveyor\SocketHandlers\Abstractions\SocketHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        /** @scrutinizer ignore-call */ 
35
        $action = $this->getAction($parsedData['action']);
Loading history...
35
36
        if ($fd && method_exists($action, 'setFd')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fd of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
37
            $action->setFd($fd);
38
        }
39
    }
40
41
    /**
42
     * Set $server if method "setServer" exists.
43
     *
44
     * @param mixed $server Server object, e.g. Swoole\WebSocket\Frame.
45
     *
46
     * @return void
47
     */
48
    public function maybeSetServer($server = null): void {
49
        $parsedData = $this->getParsedData();
50
        $action = $this->getAction($parsedData['action']);
51
52
        if ($server && method_exists($action, 'setServer')) {
53
            $action->setServer($server);
54
        }
55
    }
56
57
    /**
58
     * This method identifies the action to be executed
59
     *
60
     * @param string $data
61
     *
62
     * @return ActionInterface
63
     *
64
     * @throws InvalidArgumentException
65
     */
66
    abstract public function parseData(string $data) : ActionInterface;
67
68
    /**
69
     * @param array $data
70
     *
71
     * @return void
72
     *
73
     * @throws InvalidArgumentException
74
     */
75
    abstract public function validateData(array $data) : void;
76
}
77