Completed
Push — master ( 344583...2d30d0 )
by Bogdan
11s
created

ArgumentValueBuilder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 1
dl 0
loc 54
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A wrappedWith() 0 8 3
C build() 0 39 11
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/js-sandbox PHP library.
5
 *
6
 * Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
7
 *
8
 * Licensed under the MIT license: http://opensource.org/licenses/MIT
9
 *
10
 * For the full copyright and license information, please view the
11
 * LICENSE file that was distributed with this source or visit
12
 * http://opensource.org/licenses/MIT
13
 */
14
15
16
namespace Pinepain\JsSandbox\Specs\Builder;
17
18
19
use Pinepain\JsSandbox\Specs\Builder\Exceptions\ArgumentValueBuilderException;
20
21
22
class ArgumentValueBuilder implements ArgumentValueBuilderInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 34
    public function build(string $definition, bool $with_literal)
28
    {
29 34
        if (is_numeric($definition)) {
30 4
            if (false !== strpos($definition, '.')) {
31 2
                return (float)$definition;
32
            }
33
34 2
            return (int)$definition;
35
        }
36
37 30
        switch (strtolower($definition)) {
38 30
            case 'null':
39 3
                return null;
40 27
            case 'true':
41 3
                return true;
42 24
            case 'false':
43 3
                return false;
44
        }
45
46 21
        if ($this->wrappedWith($definition, '[', ']')) {
47 2
            return [];
48
        }
49
50 19
        if ($this->wrappedWith($definition, '{', '}')) {
51 2
            return [];
52
        }
53
54 17
        foreach (['"', "'"] as $quote) {
55 17
            if ($this->wrappedWith($definition, $quote, $quote)) {
56 17
                return trim($definition, $quote);
57
            }
58
        }
59
60 3
        if (!$with_literal) {
61 2
            throw new ArgumentValueBuilderException("Unknown value format '{$definition}'");
62
        }
63
64 1
        return $definition;
65
    }
66
67 21
    private function wrappedWith(string $definition, string $starts, $ends)
68
    {
69 21
        if (strlen($definition) < 2) {
70 1
            return false;
71
        }
72
73 20
        return $starts == $definition[0] && $ends == $definition[-1];
74
    }
75
}
76