Completed
Push — master ( d96e51...7456c2 )
by Ryan
07:31
created

EnabledGuesser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 5
c 1
b 1
f 0
lcom 0
cbo 1
dl 46
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A guess() 20 20 4

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 namespace Anomaly\Streams\Platform\Ui\Form\Component\Button\Guesser;
2
3
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
4
use Illuminate\Http\Request;
5
6
/**
7
 * Class EnabledGuesser
8
 *
9
 * @link          http://anomaly.is/streams-platform
10
 * @author        AnomalyLabs, Inc. <[email protected]>
11
 * @author        Ryan Thompson <[email protected]>
12
 * @package       Anomaly\Streams\Platform\Ui\Form\Component\Button\Guesser
13
 */
14 View Code Duplication
class EnabledGuesser
0 ignored issues
show
Duplication introduced by
This class 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...
15
{
16
17
    /**
18
     * The request object.
19
     *
20
     * @var Request
21
     */
22
    protected $request;
23
24
    /**
25
     * Create a new EnabledGuesser instance.
26
     *
27
     * @param Request $request
28
     */
29
    public function __construct(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
30
    {
31
        $this->request = $request;
32
    }
33
34
    /**
35
     * Guess the HREF for a button.
36
     *
37
     * @param FormBuilder $builder
38
     */
39
    public function guess(FormBuilder $builder)
40
    {
41
        $buttons = $builder->getButtons();
42
        $mode    = $builder->getFormMode();
43
44
        foreach ($buttons as &$button) {
0 ignored issues
show
Bug introduced by
The expression $buttons of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
45
46
            if (!isset($button['enabled'])) {
47
                continue;
48
            }
49
50
            if (is_bool($button['enabled'])) {
51
                continue;
52
            }
53
54
            $button['enabled'] = ($mode === $button['enabled']);
55
        }
56
57
        $builder->setButtons($buttons);
58
    }
59
}
60