Passed
Pull Request — main (#3)
by Peter
07:02 queued 03:23
created

ArrayHelper::formatAttribute()   B

Complexity

Conditions 11
Paths 14

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 18
nc 14
nop 1
dl 0
loc 35
rs 7.3166
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Html\Helper;
6
7
class ArrayHelper
8
{
9
    protected const ERROR_INVALID_ATTRIBUTES = 'invalid attributes (array of string[] and null items)';
10
11
    /**
12
     * @param array<string,string> $styles
13
     *
14
     * @return string
15
     */
16
    public static function toStyles(array $styles): string
17
    {
18
        $tmp = [];
19
        foreach ($styles as $k => $v) {
20
            $tmp[] = sprintf('%s: %s', $k, $v);
21
        }
22
23
        return implode('; ', $tmp);
24
    }
25
26
    /**
27
     * @param array<string,string> $parts
28
     *
29
     * @return string
30
     */
31
    public static function toQuery(array $parts): string
32
    {
33
        if (empty($parts)) {
34
            return '';
35
        }
36
37
        $tmp = [];
38
        foreach ($parts as $k => $v) {
39
            $tmp[] = sprintf('%s=%s', $k, urlencode($v));
40
        }
41
42
        return '?' . implode('&', $tmp);
43
    }
44
}
45