ExampleTag   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 44
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeValues() 0 13 4
A process() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jasny\PhpdocParser\Tag\PhpDocumentor;
6
7
use Jasny\PhpdocParser\Tag\AbstractTag;
8
use Jasny\PhpdocParser\PhpdocException;
9
use function Jasny\array_only;
10
11
/**
12
 * Custom logic for PhpDocumentor 'example' tag
13
 */
14
class ExampleTag extends AbstractTag
15
{
16
    /**
17
     * Process a notation.
18
     *
19
     * @param array  $notations
20
     * @param string $value
21
     * @return array
22
     */
23 9
    public function process(array $notations, string $value): array
24
    {
25 9
        $regexp = '/^(?<location>(?:[^"]\S*|"[^"]+"))(?:\s*(?<start_line>\d+)'
26 9
          . '(?:\s*(?<number_of_lines>\d+))?)?(?:\s*(?<description>.+))?/';
27
28 9
        if (!preg_match($regexp, $value, $matches)) {
29 2
            throw new PhpdocException("Failed to parse '@{$this->name} $value': invalid syntax");
30
        }
31
32 7
        $this->normalizeValues($matches);
33 7
        $matches = array_only($matches, ['location', 'start_line', 'number_of_lines', 'description']);
34
35 7
        $notations[$this->name] = $matches;
36
37 7
        return $notations;
38
    }
39
40
    /**
41
     * Normalize parsed values
42
     *
43
     * @param array $values
44
     */
45 7
    protected function normalizeValues(array &$values): void
46
    {
47 7
        $values['location'] = trim($values['location'], '"');
48
49 7
        foreach (['start_line', 'number_of_lines'] as $name) {
50 7
            if (!isset($values[$name])) {
51 2
                continue;
52
            }
53
54 6
            if ($values[$name] === '') {
55 2
                unset($values[$name]);
56
            } else {
57 5
                $values[$name] = (int)$values[$name];
58
            }
59
        }
60
    }
61
}
62