RedisHashKeyItemForm   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 54.05 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 20
loc 37
rs 10
c 0
b 0
f 0
ccs 0
cts 19
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addFieldsToForm() 13 13 2
A submit() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace UniMan\Drivers\Redis\Forms;
4
5
use UniMan\Core\Forms\ItemForm\ItemFormInterface;
6
use Nette\Application\UI\Form;
7
use Nette\Utils\ArrayHash;
8
use RedisProxy\RedisProxy;
9
10
class RedisHashKeyItemForm implements ItemFormInterface
11
{
12
    private $connection;
13
14
    private $hash;
15
16
    private $key;
17
18
    public function __construct(RedisProxy $connection, $hash, $key)
19
    {
20
        $this->connection = $connection;
21
        $this->hash = $hash;
22
        $this->key = $key;
23
    }
24
25 View Code Duplication
    public function addFieldsToForm(Form $form)
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...
26
    {
27
        $form->addText('key', 'redis.key_form.key.label')
28
            ->setRequired('redis.key_form.key.required');
0 ignored issues
show
Documentation introduced by
'redis.key_form.key.required' is of type string, but the function expects a boolean.

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...
29
        $form->addText('value', 'redis.key_form.value.label');
30
31
        if ($this->key) {
32
            $form->setDefaults([
33
                'key' => $this->key,
34
                'value' => $this->connection->hget($this->hash, $this->key),
35
            ]);
36
        }
37
    }
38
39 View Code Duplication
    public function submit(Form $form, ArrayHash $values)
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...
40
    {
41
        if ($this->key) {
42
            $this->connection->hdel($this->hash, $this->key);
43
        }
44
        $this->connection->hset($this->hash, $values['key'], $values['value']);
45
    }
46
}
47