Completed
Push — master ( 9afc27...035c9e )
by Chris
04:26
created

SafeNodeFactory::createNodeFromValue()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 10.5454

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 7
cts 12
cp 0.5833
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 14
nc 15
nop 3
crap 10.5454
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\Jom;
4
5
use DaveRandom\Jom\Exceptions\InvalidNodeValueException;
6
7
final class SafeNodeFactory extends NodeFactory
8
{
9
    /**
10
     * @inheritdoc
11
     */
12 64
    public function createNodeFromValue($value, ?Document $doc, int $flags): ?Node
13
    {
14
        try {
15 64
            if (null !== $node = $this->createScalarOrNullNodeFromValue($value, $doc)) {
16 64
                return $node;
17
            }
18
19 64
            if (\is_object($value)) {
20 64
                return $this->createObjectNodeFromPropertyMap($value, $doc, $flags);
21
            }
22
23 64
            if (\is_array($value)) {
24 64
                return $this->createArrayNodeFromPackedArray($value, $doc, $flags);
25
            }
26
27
            if ($flags & Node::IGNORE_INVALID_VALUES) {
28
                return null;
29
            }
30
        } catch (InvalidNodeValueException $e) {
31
            throw $e;
32
        //@codeCoverageIgnoreStart
33
        } catch (\Exception $e) {
34
            throw unexpected($e);
35
        }
36
        //@codeCoverageIgnoreEnd
37
38
        throw new InvalidNodeValueException("Failed to create node from value of type '" . \gettype($value) . "'");
39
    }
40
}
41