Completed
Push — master ( 90ccc1...22512f )
by Kamil
35:43
created

DynamicFormsChoicesMap::getFormsNamesByGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 1
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\MetadataBundle\DynamicForm;
13
14
/**
15
 * @author Kamil Kokot <[email protected]>
16
 */
17
final class DynamicFormsChoicesMap implements DynamicFormsChoicesMapInterface
18
{
19
    /**
20
     * Maps data class to form name.
21
     *
22
     * @var string[]
23
     */
24
    private $forms;
25
26
    /**
27
     * Maps form name to label.
28
     *
29
     * @var string[]
30
     */
31
    private $labels;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function addForm($group, $dataClass, $formName, $label)
37
    {
38
        $this->forms[$group][$dataClass] = $formName;
39
        $this->labels[$formName] = $label;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getFormsNamesByGroup($group)
46
    {
47
        return isset($this->forms[$group]) ? $this->forms[$group] : [];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getFormNameByGroupAndDataClass($group, $dataClass)
54
    {
55
        foreach ($this->getFormsNamesByGroup($group) as $currentDataClass => $formName) {
0 ignored issues
show
Bug introduced by
The expression $this->getFormsNamesByGroup($group) of type string|array 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...
56
            if ($currentDataClass === $dataClass) {
57
                return $formName;
58
            }
59
        }
60
61
        return null;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getDataClassByGroupAndFormName($group, $formName)
68
    {
69
        foreach ($this->getFormsNamesByGroup($group) as $dataClass => $currentFormName) {
0 ignored issues
show
Bug introduced by
The expression $this->getFormsNamesByGroup($group) of type string|array 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...
70
            if ($currentFormName === $formName) {
71
                return $dataClass;
72
            }
73
        }
74
75
        return null;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getLabelByFormName($formName)
82
    {
83
        return isset($this->labels[$formName]) ? $this->labels[$formName] : $formName;
84
    }
85
}
86