WordTag::getDefault()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\PhpdocParser\Tag;
6
7
use function Jasny\expect_type;
8
use function Jasny\str_before;
9
use function Jasny\str_starts_with;
10
11
/**
12
 * Only use the first word after the tag, ignoring the rest
13
 */
14
class WordTag extends AbstractTag
15
{
16
    /**
17
     * Default value if no value is given for tag
18
     * @var string|bool
19
     */
20
    protected $default;
21
22
    /**
23
     * WordTag constructor.
24
     *
25
     * @param string      $name
26
     * @param string|bool $default
27
     */
28 8
    public function __construct(string $name, $default = '')
29
    {
30 8
        parent::__construct($name);
31
32 8
        expect_type($default, ['string', 'bool']);
33 8
        $this->default = $default;
34
    }
35
36
    /**
37
     * Return default if no value is specified
38
     *
39
     * @return string|bool
40
     */
41 2
    public function getDefault()
42
    {
43 2
        return $this->default;
44
    }
45
46
47
    /**
48
     * Process a notation.
49
     *
50
     * @param array  $notations
51
     * @param string $value
52
     * @return array
53
     */
54 5
    public function process(array $notations, string $value): array
55
    {
56 5
        if ($value === '') {
57 1
            $notations[$this->name] = $this->default;
58 1
            return $notations;
59
        }
60
61 4
        $matches = [];
62 4
        $quoted = in_array($value[0], ['"', '\''], true) &&
63 4
            preg_match('/^(?|"((?:[^"]+|\\\\.)*)"|\'((?:[^\']+|\\\\.)*)\')/', $value, $matches);
64
65 4
        $word = $quoted ? $matches[1] : str_before($value, ' ');
66 4
        $notations[$this->name] = $word;
67
68 4
        return $notations;
69
    }
70
}
71