Passed
Push — master ( 3a35da...a738f6 )
by Nikolaos
02:35
created

Header::serialize()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 9.0036

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 28
c 1
b 0
f 0
nc 16
nop 1
dl 0
loc 52
ccs 27
cts 28
cp 0.9643
crap 9.0036
rs 8.0555

How to fix   Long Method   

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
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Html\Link\Serializer;
15
16
use function implode;
17
use function is_array;
18
use function is_bool;
19
20
/**
21
 * Class Phalcon\Http\Link\Serializer\Header
22
 */
23
class Header implements SerializerInterface
24
{
25
    /**
26
     * Serializes all the passed links to a HTTP link header
27
     *
28
     * @param array $links
29
     *
30
     * @return string|null
31
     */
32 2
    public function serialize(array $links): ?string
33
    {
34 2
        $elements = [];
35 2
        $result   = null;
36 2
        foreach ($links as $link) {
37
            /**
38
             * Leave templated links alone
39
             */
40 1
            if ($link->isTemplated()) {
41
                continue;
42
            }
43
44
            /**
45
             * Split the parts of the attributes so that we can parse them
46
             */
47 1
            $attributes = $link->getAttributes();
48 1
            $rels       = $link->getRels();
49
            $parts      = [
50 1
                "",
51 1
                "rel=\"" . implode(" ", $rels) . "\""
52
            ];
53
54 1
            foreach ($attributes as $key => $value) {
55 1
                if (is_array($value)) {
56 1
                    foreach ($value as $subValue) {
57 1
                        $parts[] = $key . "=\"" . $subValue . "\"";
58
                    }
59 1
                    continue;
60
                }
61
62 1
                if (!is_bool($value)) {
63 1
                    $parts[] = $key . "=\"" . $value . "\"";
64 1
                    continue;
65
                }
66
67 1
                if (true === $value) {
68 1
                    $parts[] = $key;
69 1
                    continue;
70
                }
71
            }
72
73 1
            $elements[] = "<"
74 1
                . $link->getHref()
75 1
                . ">"
76 1
                . implode("; ", $parts);
77
        }
78
79 2
        if (count($elements) > 0) {
80 1
            $result = implode(",", $elements);
81
        }
82
83 2
        return $result;
84
    }
85
}
86