Passed
Push — master ( 70e25c...4344d4 )
by Martijn
03:00
created

ArgumentsTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 87
ccs 29
cts 30
cp 0.9667
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getArrayArgument() 0 13 4
A getArgument() 0 22 6
A getStringArgument() 0 11 3
A getArguments() 0 5 1
1
<?php
2
3
namespace Vanderlee\Comprehend\Core;
4
5
use Vanderlee\Comprehend\Parser\Parser;
6
use Vanderlee\Comprehend\Parser\Structure\Choice;
7
use Vanderlee\Comprehend\Parser\Structure\Sequence;
8
use Vanderlee\Comprehend\Parser\Terminal\Char;
9
use Vanderlee\Comprehend\Parser\Terminal\Text;
10
11
/**
12
 * Process arguments
13
 *
14
 * @author Martijn
15
 */
16
trait ArgumentsTrait
17
{
18
19
    /**
20
     * Parser array argument
21
     *
22
     * @param mixed $argument
23
     * @param bool $arrayToSequence
24
     * @return Parser|Choice|Sequence
25
     */
26 14
    private static function getArrayArgument($argument, $arrayToSequence)
27
    {
28 14
        if (empty($argument)) {
29 1
            throw new \InvalidArgumentException('Empty array argument');
30
        }
31
32 13
        if (count($argument) === 1) {
33 2
            return self::getArgument(reset($argument));
34
        }
35
36 13
        return $arrayToSequence
37 13
            ? new Sequence(...$argument)
38 13
            : new Choice(...$argument);
39
    }
40
41
    /**
42
     * Parse string argument
43
     *
44
     * @param mixed $argument
45
     * @return Char|Text
46
     */
47 71
    private static function getStringArgument($argument)
48
    {
49 71
        if (strlen($argument) === 0) {
50 2
            throw new \InvalidArgumentException('Empty argument');
51
        }
52
53 70
        if (strlen($argument) === 1) {
54 62
            return new Char($argument);
55
        }
56
57 28
        return new Text($argument);
58
    }
59
60
    /**
61
     * Convert the argument to a parser
62
     *
63
     * @param mixed $argument
64
     * @param bool $arrayToSequence if argument is an array, convert to Sequence (`true`) or Choice (`false`)
65
     * @return Parser
66
     */
67 112
    protected static function getArgument($argument, $arrayToSequence = true)
68
    {
69 112
        if (is_array($argument)) {
70 14
            return self::getArrayArgument($argument, $arrayToSequence);
71
        }
72
73 111
        if (is_string($argument)) {
74 71
            return self::getStringArgument($argument);
75
        }
76
77 66
        if (is_int($argument)) {
78 2
            return new Char($argument);
79
        }
80
81 66
        if ($argument instanceof Parser) {
82 64
            return $argument;
83
        }
84
85 2
        throw new \InvalidArgumentException(sprintf('Invalid argument type `%1$s`',
86 2
            is_object($argument)
87
                ? get_class($argument)
88 2
                : gettype($argument)));
89
    }
90
91
    /**
92
     * Parse an array of arguments
93
     *
94
     * @param array $arguments
95
     * @param bool $arrayToSequence
96
     * @return array
97
     */
98
    protected static function getArguments($arguments, $arrayToSequence = true)
99
    {
100 51
        return array_map(function ($argument) use ($arrayToSequence) {
101 51
            return self::getArgument($argument, $arrayToSequence);
102 51
        }, $arguments);
103
    }
104
}
105