Passed
Pull Request — main (#4)
by Peter
05:56 queued 03:11
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
use AbterPhp\Framework\Html\INode;
8
9
class ArrayHelper
10
{
11
    protected const ERROR_INVALID_ATTRIBUTES = 'invalid attributes (array of string[] and null items)';
12
13
    /**
14
     * @param array<string,string> $styles
15
     *
16
     * @return string
17
     */
18
    public static function toStyles(array $styles): string
19
    {
20
        $tmp = [];
21
        foreach ($styles as $k => $v) {
22
            $tmp[] = sprintf('%s: %s', $k, $v);
23
        }
24
25
        return implode('; ', $tmp);
26
    }
27
28
    /**
29
     * @param array<string,string> $parts
30
     *
31
     * @return string
32
     */
33
    public static function toQuery(array $parts): string
34
    {
35
        if (empty($parts)) {
36
            return '';
37
        }
38
39
        $tmp = [];
40
        foreach ($parts as $k => $v) {
41
            $tmp[] = sprintf('%s=%s', $k, urlencode($v));
42
        }
43
44
        return '?' . implode('&', $tmp);
45
    }
46
47
    /**
48
     * @param array  $items
49
     * @param string $className
50
     *
51
     * @return bool
52
     */
53
    public static function allInstanceOf(array $items, string $className): bool
54
    {
55
        if (empty($className)) {
56
            return true;
57
        }
58
59
        foreach ($items as $item) {
60
            if (!($item instanceof $className)) {
61
                return false;
62
            }
63
        }
64
65
        return true;
66
    }
67
68
    /**
69
     * @param array $items
70
     *
71
     * @return bool
72
     */
73
    public static function allNodes(array $items): bool
74
    {
75
        foreach ($items as $item) {
76
            if (!is_scalar($item) && !($item instanceof INode)) {
77
                return false;
78
            }
79
        }
80
81
        return true;
82
    }
83
}
84