JsonTemplate   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 18
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A map() 0 14 2
1
<?php
2
/**
3
 * This file is part of sitemap-common.
4
 *
5
 * (c) 2016 Daniele Moraschi
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SiteMap\Template;
12
13
14
use SiteMap\Schema\SiteMapUrl;
15
use SiteMap\Schema\SiteMapUrlCollection;
16
17
class JsonTemplate implements Template
18
{
19
20
    public function map(SiteMapUrlCollection $collection)
21
    {
22
        $base = $collection[0]->getUrl()->getHost();
23
        $jsonOutput[$base] = array();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$jsonOutput was never initialized. Although not strictly required by PHP, it is generally a good practice to add $jsonOutput = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
24
25
        /** @var SiteMapUrl $item */
26
        foreach ($collection as $item) {
27
            $jsonOutput[$base]['urls'][] = $item->toArray();
28
        }
29
30
        $jsonOutput[$base]['total'] = $collection->count();
31
32
        return json_encode($jsonOutput);
33
    }
34
}