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

RedisListForm   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 41.03 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 16
loc 39
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addFieldsToForm() 16 16 2
A submit() 0 9 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 Nette\Application\UI\Form;
6
use Nette\Utils\ArrayHash;
7
use RedisProxy\RedisProxy;
8
use UniMan\Core\Forms\TableForm\TableFormInterface;
9
10
class RedisListForm implements TableFormInterface
11
{
12
    private $connection;
13
14
    private $key;
15
16
    public function __construct(RedisProxy $connection, string $key = null)
17
    {
18
        $this->connection = $connection;
19
        $this->key = $key;
20
    }
21
22 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...
23
    {
24
        $form->addText('key', 'redis.list_form.key.label')
25
            ->setRequired('redis.list_form.key.required');
26
27
        $form->addText('elements', 'redis.list_form.elements.label')
28
            ->setRequired('redis.list_form.elements.required')
29
            ->setOption('description', 'redis.list_form.elements.description');
30
31
        if ($this->key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->key of type null|string 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...
32
            $form->setDefaults([
33
                'key' => $this->key,
34
                'elements' => implode(',', $this->connection->lrange($this->key, 0, -1)),
35
            ]);
36
        }
37
    }
38
39
    public function submit(Form $form, ArrayHash $values)
40
    {
41
        if ($this->key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->key of type null|string 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...
42
            $this->connection->del($this->key);
43
        }
44
45
        $elements = array_map('trim', explode(',', $values->elements));
46
        $this->connection->rpush($values->key, $elements);
47
    }
48
}
49