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

EnabledGuesser::guess()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 21
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 21
loc 21
rs 9.0534
cc 4
eloc 10
nc 4
nop 1
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