ActionResponse   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 7.5 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 6
loc 80
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B create() 6 33 3
A hasRedirect() 0 4 1
A getRedirect() 0 4 1
A getRedirectParams() 0 4 1
A getErrors() 0 4 1
A hasErrors() 0 4 1
A getAffectedRecordCount() 0 4 1
A hasAffectedRecords() 0 4 1

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
declare(strict_types=1);
4
5
namespace Psi\Component\Grid;
6
7
final class ActionResponse implements ActionResponseInterface
8
{
9
    private $redirect;
10
    private $redirectParams;
11
    private $affected;
12
    private $errors;
13
14
    private function __construct()
15
    {
16
    }
17
18
    public static function create(array $options): ActionResponse
19
    {
20
        $defaults = [
21
            'redirect' => null,
22
            'redirectParams' => [],
23
            'errors' => [],
24
            'affected' => 0,
25
        ];
26
27 View Code Duplication
        if ($diff = array_diff(array_keys($options), array_keys($defaults))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
28
            throw new \InvalidArgumentException(sprintf(
29
                'Unexpected keys for action response: "%s", valid keys: "%s"',
30
                implode('", "', $diff), implode('", "', array_keys($defaults))
31
            ));
32
        }
33
34
        $options = array_merge($defaults, $options);
35
36
        if ($options['affected'] < 0) {
37
            throw new \InvalidArgumentException(sprintf(
38
                'Number of affected records cannot be negative (got "%s")',
39
                $options['affected']
40
            ));
41
        }
42
43
        $response = new self();
44
        $response->redirect = $options['redirect'];
45
        $response->redirectParams = $options['redirectParams'];
46
        $response->errors = $options['errors'];
47
        $response->affected = $options['affected'];
48
49
        return $response;
50
    }
51
52
    public function hasRedirect(): bool
53
    {
54
        return isset($this->redirect);
55
    }
56
57
    public function getRedirect(): string
58
    {
59
        return $this->redirect;
60
    }
61
62
    public function getRedirectParams(): array
63
    {
64
        return $this->redirectParams;
65
    }
66
67
    public function getErrors(): array
68
    {
69
        return $this->errors;
70
    }
71
72
    public function hasErrors(): bool
73
    {
74
        return false === empty($this->errors);
75
    }
76
77
    public function getAffectedRecordCount(): int
78
    {
79
        return $this->affected;
80
    }
81
82
    public function hasAffectedRecords(): bool
83
    {
84
        return $this->affected > 0;
85
    }
86
}
87