Passed
Push — master ( 4344d4...81d6c9 )
by Martijn
02:43
created

ArgumentsTrait::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 72
    private static function getStringArgument($argument)
48
    {
49 72
        if (strlen($argument) === 0) {
50 2
            throw new \InvalidArgumentException('Empty argument');
51
        }
52
53 71
        if (strlen($argument) === 1) {
54 63
            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 115
    protected static function getArgument($argument, $arrayToSequence = true)
68
    {
69 115
        if (is_array($argument)) {
70 14
            return self::getArrayArgument($argument, $arrayToSequence);
71
        }
72
73 114
        if (is_string($argument)) {
74 72
            return self::getStringArgument($argument);
75
        }
76
77 68
        if (is_int($argument)) {
78 2
            return new Char($argument);
79
        }
80
81 68
        if ($argument instanceof Parser) {
82 64
            return $argument;
83
        }
84
85 4
        throw new \InvalidArgumentException(sprintf('Invalid argument type `%1$s`',
86 4
            is_object($argument)
87 1
                ? get_class($argument)
88 4
                : 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