Completed
Push — master ( d5b935...21e4f3 )
by Michal
03:23
created

RedisFormManager::tableForm()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 14
cp 0
rs 8.8333
c 0
b 0
f 0
cc 7
nc 7
nop 3
crap 56
1
<?php
2
3
namespace UniMan\Drivers\Redis;
4
5
use RedisProxy\RedisProxy;
6
use UniMan\Core\Forms\DefaultFormManager;
7
use UniMan\Drivers\Redis\Forms\RedisCreateHashForm;
8
use UniMan\Drivers\Redis\Forms\RedisCreateSetForm;
9
use UniMan\Drivers\Redis\Forms\RedisEditDatabaseForm;
10
use UniMan\Drivers\Redis\Forms\RedisEditSetForm;
11
use UniMan\Drivers\Redis\Forms\RedisHashKeyItemForm;
12
use UniMan\Drivers\Redis\Forms\RedisKeyItemForm;
13
use UniMan\Drivers\Redis\Forms\RedisListForm;
14
use UniMan\Drivers\Redis\Forms\RedisListElementForm;
15
use UniMan\Drivers\Redis\Forms\RedisRenameHashForm;
16
use UniMan\Drivers\Redis\Forms\RedisSetMemberForm;
17
use UniMan\Drivers\Redis\RedisDatabaseAliasStorage;
18
19
class RedisFormManager extends DefaultFormManager
20
{
21
    private $connection;
22
23
    private $databaseAliasStorage;
24
25 2
    public function __construct(RedisProxy $connection, RedisDatabaseAliasStorage $databaseAliasStorage)
26
    {
27 2
        $this->connection = $connection;
28 2
        $this->databaseAliasStorage = $databaseAliasStorage;
29 2
    }
30
31
    public function itemForm($database, $type, $table, $item)
32
    {
33
        if ($type === RedisDriver::TYPE_HASH) {
34
            return new RedisHashKeyItemForm($this->connection, $table, $item);
35
        } elseif ($type === RedisDriver::TYPE_KEY) {
36
            return new RedisKeyItemForm($this->connection, $item);
37
        } elseif ($type === RedisDriver::TYPE_SET) {
38
            return new RedisSetMemberForm($this->connection, $table, $item);
39
        } elseif ($type === RedisDriver::TYPE_LIST) {
40
            return new RedisListElementForm($this->connection, $table, $item);
41
        }
42
    }
43
44
    public function tableForm($database, $type, $table)
45
    {
46
        if ($type === RedisDriver::TYPE_HASH) {
47
            if ($table) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $table of type string|null is loosely compared to true; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
48
                return new RedisRenameHashForm($this->connection, $table);
49
            }
50
            return new RedisCreateHashForm($this->connection);
51
        } elseif ($type === RedisDriver::TYPE_KEY) {
52
            return new RedisKeyItemForm($this->connection, $table);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \UniMan\Drive...s->connection, $table); (UniMan\Drivers\Redis\Forms\RedisKeyItemForm) is incompatible with the return type declared by the interface UniMan\Core\Forms\FormManagerInterface::tableForm of type UniMan\Core\Forms\TableForm\TableFormInterface.

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...
53
        } elseif ($type === RedisDriver::TYPE_SET) {
54
            if (!$table) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $table of type string|null is loosely compared to false; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
55
                return new RedisCreateSetForm($this->connection);
56
            }
57
            return new RedisEditSetForm($this->connection, $table);
58
        } elseif ($type === RedisDriver::TYPE_LIST) {
59
            return new RedisListForm($this->connection, $table);
60
        }
61
    }
62
63
    public function databaseForm($database)
64
    {
65
        return new RedisEditDatabaseForm($database, $this->databaseAliasStorage);
66
    }
67
}
68