Completed
Pull Request — master (#18)
by Michal
02:12
created

RabbitMQHeaderManager::itemsHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 0
cts 21
cp 0
rs 9.0856
cc 2
eloc 20
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Adminerng\Drivers\RabbitMQ;
4
5
use Adminerng\Core\Column;
6
use Adminerng\Core\ListingHeaders\HeaderManagerInterface;
7
8
class RabbitMQHeaderManager implements HeaderManagerInterface
9
{
10 View Code Duplication
    public function databasesHeaders()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
    {
12
        $columns = [];
13
        $columns[] = (new Column('vhost', 'rabbitmq.headers.vhosts.vhost'))
14
            ->setIsSortable(true);
15
        $columns[] = (new Column('queues', 'rabbitmq.headers.vhosts.queues'))
16
            ->setIsSortable(true)
17
            ->setIsNumeric(true);
18
        $columns[] = (new Column('messages', 'rabbitmq.headers.vhosts.messages'))
19
            ->setIsSortable(true)
20
            ->setIsNumeric(true);
21
        return $columns;
22
    }
23
24 View Code Duplication
    public function tablesHeaders()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        $columns = [];
27
        $columns[] = (new Column('queue', 'rabbitmq.headers.queues.queue'))
28
            ->setIsSortable(true);
29
        $columns[] = (new Column('number_of_items', 'rabbitmq.headers.queues.number_of_items'))
30
            ->setIsSortable(true)
31
            ->setIsNumeric(true);
32
        $columns[] = (new Column('size', 'rabbitmq.headers.queues.size'))
33
            ->setIsSortable(true)
34
            ->setIsNumeric(true)
35
            ->setIsSize(true);
36
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(\Adminerng\...YPE_QUEUE => $columns); (array<*,Adminerng\Core\Column[]>) is incompatible with the return type declared by the interface Adminerng\Core\ListingHe...nterface::tablesHeaders of type Adminerng\Core\Column[].

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...
37
            RabbitMQDriver::TYPE_QUEUE => $columns,
38
        ];
39
    }
40
41
    public function itemsHeaders($type, $table)
42
    {
43
        $columns = [];
44
        if ($type == RabbitMQDriver::TYPE_QUEUE) {
45
            $columns[] = (new Column('message_body', 'rabbitmq.columns.' . $type . '.message_body'))
46
                ->setIsSortable(true)
47
                ->setIsFilterable(true);
48
            $columns[] = (new Column('length', 'rabbitmq.columns.' . $type . '.length'))
49
                ->setIsNumeric(true)
50
                ->setIsSortable(true)
51
                ->setIsFilterable(true);
52
            $columns[] = (new Column('is_truncated', 'rabbitmq.columns.' . $type . '.is_truncated'))
53
                ->setIsSortable(true)
54
                ->setIsFilterable(true);
55
            $columns[] = (new Column('content_encoding', 'rabbitmq.columns.' . $type . '.content_encoding'))
56
                ->setIsSortable(true)
57
                ->setIsFilterable(true);
58
            $columns[] = (new Column('redelivered', 'rabbitmq.columns.' . $type . '.redelivered'))
59
                ->setIsSortable(true)
60
                ->setIsFilterable(true);
61
        }
62
        return $columns;
63
    }
64
}
65