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.
Completed
Push — develop ( c3bf76...dc595a )
by Baptiste
03:31
created

Delegate::read()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\AMQP\Transport\Protocol;
5
6
use Innmind\AMQP\{
7
    Transport\Protocol,
8
    Transport\Frame\Method,
9
    Exception\VersionNotUsable
10
};
11
use Innmind\Immutable\{
12
    Sequence,
13
    Str,
14
    StreamInterface
15
};
16
17
final class Delegate implements Protocol
18
{
19
    private $protocols;
20
    private $inUse;
21
22
    public function __construct(Protocol $first, Protocol ...$protocols)
23
    {
24 4
        $protocols = (new Sequence($first, ...$protocols))->sort(static function(Protocol $a, Protocol $b): bool {
25 3
            return $b->version()->higherThan($a->version());
0 ignored issues
show
Documentation introduced by
$a->version() is of type object<Innmind\AMQP\Transport\Protocol\Version>, but the function expects a object<self>.

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...
26 4
        });
27 4
        $this->inUse = $protocols->first();
28 4
        $this->protocols = $protocols;
29 4
    }
30
31 3
    public function version(): Version
32
    {
33 3
        return $this->inUse->version();
34
    }
35
36 2
    public function use(Version $version): Protocol
37
    {
38
        $protocols = $this
39 2
            ->protocols
40 2
            ->filter(static function(Protocol $protocol) use ($version): bool {
41
                try {
42 2
                    $protocol->use($version);
43
44 2
                    return true;
45 2
                } catch (VersionNotUsable $e) {
46 2
                    return false;
47
                }
48 2
            });
49
50 2
        if ($protocols->size() === 0) {
51
            throw new VersionNotUsable($version);
52
        }
53
54 2
        $this->inUse = $protocols->first();
55
56 2
        return $this;
1 ignored issue
show
Bug Best Practice introduced by
The return type of return $this; (Innmind\AMQP\Transport\Protocol\Delegate) is incompatible with the return type declared by the interface Innmind\AMQP\Transport\Protocol::use of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function read(Method $method, Str $arguments): StreamInterface
63
    {
64 1
        return $this->inUse->read($method, $arguments);
65
    }
66
67 1
    public function method(string $name): Method
68
    {
69 1
        return $this->inUse->method($name);
70
    }
71
72 1
    public function connection(): Connection
73
    {
74 1
        return $this->inUse->connection();
75
    }
76
77 1
    public function channel(): Channel
78
    {
79 1
        return $this->inUse->channel();
80
    }
81
82
    public function exchange(): Exchange
83
    {
84
        return $this->inUse->exchange();
85
    }
86
87
    public function queue(): Queue
88
    {
89
        return $this->inUse->queue();
90
    }
91
92
    public function basic(): Basic
93
    {
94
        return $this->inUse->basic();
95
    }
96
97
    public function transaction(): Transaction
98
    {
99
        return $this->inUse->transaction();
100
    }
101
}
102