QuestionJsonEnricher   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A enrich() 0 11 2
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Question\Serializers;
4
5
use Ramsey\Uuid\UuidFactoryInterface;
6
use VSV\GVQ_API\Common\Serializers\JsonEnricher;
7
8
class QuestionJsonEnricher implements JsonEnricher
9
{
10
    /**
11
     * @var UuidFactoryInterface
12
     */
13
    private $uuidFactory;
14
15
    /**
16
     * @param UuidFactoryInterface $uuidFactory
17
     */
18
    public function __construct(UuidFactoryInterface $uuidFactory)
19
    {
20
        $this->uuidFactory = $uuidFactory;
21
    }
22
23
    /**
24
     * @param string $json
25
     * @return string
26
     */
27
    public function enrich(string $json): string
28
    {
29
        $questionAsArray = json_decode($json, true);
30
31
        $questionAsArray['id'] = $this->uuidFactory->uuid4()->toString();
32
33
        for ($index = 0; $index < count($questionAsArray['answers']); $index++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
34
            $questionAsArray['answers'][$index]['id'] = $this->uuidFactory->uuid4()->toString();
35
        }
36
37
        return json_encode($questionAsArray);
38
    }
39
}
40