Completed
Push — master ( 90d5a0...2ce81b )
by Eugene
03:40
created

Queue::put()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Queue package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Queue;
15
16
use Tarantool\Client\Client;
17
18
final class Queue
19
{
20
    private $client;
21
    private $name;
22
23
    /**
24
     * @param \Tarantool|Client $client
25
     * @param string $name
26
     *
27
     * @throws \InvalidArgumentException
28
     */
29 172
    public function __construct($client, string $name)
30
    {
31 172
        if ($client instanceof \Tarantool) {
0 ignored issues
show
Bug introduced by
The class Tarantool does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
32
            $client = new TarantoolAdapter($client);
33 172
        } elseif (!$client instanceof Client) {
34 2
            throw new \InvalidArgumentException(\sprintf(
35 2
                '%s() expects parameter 1 to be %s or Tarantool, %s given.',
36 2
                __METHOD__, Client::class, \is_object($client) ? \get_class($client) : \gettype($client)
37
            ));
38
        }
39
40 170
        $this->client = $client;
41 170
        $this->name = $name;
42 170
    }
43
44
    public function getName() : string
45
    {
46
        return $this->name;
47
    }
48
49 32
    public function put($data, array $options = []) : Task
50
    {
51 32
        return Task::createFromTuple(
52 32
            $this->client->call("queue.tube.$this->name:put", $data, $options)[0]
53
        );
54
    }
55
56 24
    public function take(float $timeout = null) : ?Task
57
    {
58 24
        $result = $this->client->call("queue.tube.$this->name:take", $timeout);
59
60 24
        return empty($result[0]) ? null : Task::createFromTuple($result[0]);
61
    }
62
63 6
    public function touch(int $taskId, float $increment) : ?Task
64
    {
65 6
        $result = $this->client->call("queue.tube.$this->name:touch", $taskId, $increment);
66
67 6
        return empty($result[0]) ? null : Task::createFromTuple($result[0]);
68
    }
69
70 8
    public function ack(int $taskId) : Task
71
    {
72 8
        return Task::createFromTuple(
73 8
            $this->client->call("queue.tube.$this->name:ack", $taskId)[0]
74
        );
75
    }
76
77 12
    public function release(int $taskId, array $options = []) : Task
78
    {
79 12
        return Task::createFromTuple(
80 12
            $this->client->call("queue.tube.$this->name:release", $taskId, $options)[0]
81
        );
82
    }
83
84 12
    public function peek(int $taskId) : Task
85
    {
86 12
        return Task::createFromTuple(
87 12
            $this->client->call("queue.tube.$this->name:peek", $taskId)[0]
88
        );
89
    }
90
91 8
    public function bury(int $taskId) : Task
92
    {
93 8
        return Task::createFromTuple(
94 8
            $this->client->call("queue.tube.$this->name:bury", $taskId)[0]
95
        );
96
    }
97
98 8
    public function kick(int $count) : int
99
    {
100 8
        return $this->client->call("queue.tube.$this->name:kick", $count)[0];
101
    }
102
103 10
    public function delete(int $taskId) : Task
104
    {
105 10
        return Task::createFromTuple(
106 10
            $this->client->call("queue.tube.$this->name:delete", $taskId)[0]
107
        );
108
    }
109
110 8
    public function truncate() : void
111
    {
112 8
        $this->client->call("queue.tube.$this->name:truncate");
113 8
    }
114
115
    /**
116
     * @param string|null $path
117
     *
118
     * @throws \InvalidArgumentException
119
     *
120
     * @return array|int
121
     */
122 58
    public function stats(string $path = null)
123
    {
124 58
        [$stats] = $this->client->call('queue.stats', $this->name);
0 ignored issues
show
Bug introduced by
The variable $stats seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
125
126 58
        if (null === $path) {
127 8
            return $stats;
0 ignored issues
show
Bug introduced by
The variable $stats seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
128
        }
129
130 50
        foreach (\explode('.', $path) as $key) {
131 50
            if (!isset($stats[$key])) {
132 36
                throw new \InvalidArgumentException(\sprintf('Invalid path "%s".', $path));
133
            }
134 30
            $stats = $stats[$key];
135
        }
136
137 14
        return $stats;
0 ignored issues
show
Bug introduced by
The variable $stats 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...
138
    }
139
140 4
    public function call(string $methodName, ...$args) : array
141
    {
142 4
        return $this->client->call("queue.tube.$this->name:$methodName", ...$args);
143
    }
144
}
145