ArgumentsTrait   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 105
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0
wmc 15

5 Methods

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