Completed
Push — master ( b59779...e6aa34 )
by Olivier
07:44
created

ReferenceManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 57
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 15 4
A findAndReplace() 0 12 4
A isReference() 0 4 1
A resolveReference() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Datagen;
6
7
use Shapin\Datagen\Exception\DuplicateReferenceException;
8
use Shapin\Datagen\Exception\UnknownReferenceException;
9
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
10
use Symfony\Component\PropertyAccess\PropertyAccess;
11
12
class ReferenceManager
13
{
14
    private $references;
15 10
16
    public function __construct()
17 10
    {
18 10
        $this->references = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS);
19
    }
20 5
21
    public function add(string $fixture, string $name, array $data): void
22 5
    {
23 5
        if (!array_key_exists($fixture, $this->references)) {
24
            $this->references[$fixture] = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS);
25 5
        }
26 1
        if (array_key_exists($name, $this->references[$fixture])) {
27
            throw new DuplicateReferenceException($fixture, $name);
28
        }
29 5
30 5
        if (!is_object($data)) {
31
            $data = new \ArrayObject($data, \ArrayObject::ARRAY_AS_PROPS);
32
        }
33 5
34 5
        $this->references[$fixture][$name] = $data;
35
    }
36 2
37
    public function findAndReplace(array $data): array
38 2
    {
39 1
        $keys = array_keys($data);
40
        for ($i = 0; $i < count($data); ++$i) {
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...
41
            $value = $data[$keys[$i]];
42 1
            if (is_string($value) && $this->isReference($value)) {
43
                $data[$keys[$i]] = $this->resolveReference($value);
44
            }
45 6
        }
46
47 6
        return $data;
48 6
    }
49 6
50 6
    private function isReference(string $value): bool
51 2
    {
52
        return 'REF:' === substr($value, 0, 4);
53
    }
54
55 5
    private function resolveReference(string $value)
56
    {
57
        $propertyAccessor = PropertyAccess::createPropertyAccessor();
58 6
        $parts = explode(':', $value);
59
60 6
        $ref = $parts[1];
61
62
        try {
63 2
            return $propertyAccessor->getValue($this->references, $ref);
64
        } catch (NoSuchPropertyException $e) {
65 2
            throw new UnknownReferenceException("Unable to resolve Reference \"$value\".", 0, $e);
66 2
        }
67
    }
68
}
69