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

EnabledGuesser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A guess() 21 21 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\Action\Guesser;
2
3
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
4
5
/**
6
 * Class EnabledGuesser
7
 *
8
 * @link          http://anomaly.is/streams-platform
9
 * @author        AnomalyLabs, Inc. <[email protected]>
10
 * @author        Ryan Thompson <[email protected]>
11
 * @package       Anomaly\Streams\Platform\Ui\Form\Component\Action\Guesser
12
 */
13 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...
14
{
15
16
    /**
17
     * Guess the action's enabled parameter.
18
     *
19
     * @param FormBuilder $builder
20
     */
21
    public function guess(FormBuilder $builder)
22
    {
23
        $actions = $builder->getActions();
24
25
        $mode = $builder->getFormMode();
26
27
        foreach ($actions as &$action) {
0 ignored issues
show
Bug introduced by
The expression $actions 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...
28
29
            if (!isset($action['enabled'])) {
30
                continue;
31
            }
32
33
            if (is_bool($action['enabled'])) {
34
                continue;
35
            }
36
37
            $action['enabled'] = ($mode === $action['enabled']);
38
        }
39
40
        $builder->setActions($actions);
41
    }
42
}
43