Passed
Pull Request — master (#5)
by Serhii
12:37 queued 05:03
created

transformFromArrayToHtmlAttributes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 17
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Yves\FactFinderWebComponents\Form;
9
10
use Symfony\Component\Form\DataTransformerInterface;
11
12
class WebComponentConfigToTwigConfigTransformer implements DataTransformerInterface
13
{
14
    public const ITEMS_PARAMETER = 'items';
15
16
    /**
17
     * @inheritdoc
18
     *
19
     * @param mixed $value
20
     *
21
     * @return mixed|void
22
     */
23
    public function transform($value)
24
    {
25
        $result = [];
26
        $properties = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $properties is dead and can be removed.
Loading history...
27
28
        if (is_array($value)) {
29
            $result = $value;
30
31
            if (isset($value['properties'])) {
32
                $result['component'] = $this->transformFromArrayToHtmlAttributes($result['properties'] ?? []);
33
                unset($result['properties']);
34
            }
35
        }
36
37
        return $result;
38
    }
39
40
    /**
41
     * @param array $collection
42
     *
43
     * @return string
44
     */
45
    protected function transformFromArrayToHtmlAttributes(array $collection): string
46
    {
47
        $htmlAttributesString = '';
48
49
        foreach ($collection as $itemKey => $itemValue) {
50
            if (is_bool($itemValue) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
51
                $htmlAttributesString .= sprintf('%s="%s" ', $itemKey, $itemValue);
52
53
                continue;
54
            }
55
56
            if ($itemValue === true) {
57
                $htmlAttributesString .= sprintf('%s ', $itemKey);
58
            }
59
        }
60
61
        return trim($htmlAttributesString);
62
    }
63
64
    /**
65
     * @inheritdoc
66
     *
67
     * @param mixed $value
68
     *
69
     * @return mixed|void
70
     */
71
    public function reverseTransform($value)
72
    {
73
        $result = [];
74
75
        if (is_array($value)) {
76
            $result = $value;
77
78
            if (isset($value['component'])) {
79
                $result['properties'] = $this->transformFropmHtmlAttributesToArray($result['component'] ?? []);
0 ignored issues
show
Bug introduced by
It seems like $result['component'] ?? array() can also be of type array; however, parameter $attributes of SprykerEco\Yves\FactFind...HtmlAttributesToArray() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
                $result['properties'] = $this->transformFropmHtmlAttributesToArray(/** @scrutinizer ignore-type */ $result['component'] ?? []);
Loading history...
80
                unset($result['component']);
81
            }
82
        }
83
84
        return $result;
85
    }
86
87
    /**
88
     * @param string $attributes
89
     *
90
     * @return array
91
     */
92
    protected function transformFropmHtmlAttributesToArray(string $attributes): array
93
    {
94
        $properties = [];
95
96
        if (is_string($attributes)) {
0 ignored issues
show
introduced by
The condition is_string($attributes) is always true.
Loading history...
97
            $attributeDataCollection = explode(' ', trim($attributes));
98
            foreach ($attributeDataCollection as $attributeDataString) {
99
                $varData = explode('=', $attributeDataString);
100
                if ($varData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $varData of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
101
                    $properties[$varData[0]] = trim($varData[1], '"\'');
102
                }
103
            }
104
        }
105
106
        return $properties;
107
    }
108
}
109