aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/Remote/OpenApiDoc/DocBlock.php
blob: 879b026c99c2bf3a4cc4feffa8dd0ba8b785bf64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php

namespace dokuwiki\Remote\OpenApiDoc;

use Reflector;

class DocBlock
{
    /** @var Reflector The reflected object */
    protected $reflector;

    /** @var string The first line of the decription */
    protected $summary = '';

    /** @var string The description */
    protected $description = '';

    /** @var string The parsed tags */
    protected $tags = [];

    /**
     * Parse the given docblock
     *
     * The docblock can be of a method, class or property.
     *
     * @param Reflector $reflector
     */
    public function __construct(Reflector $reflector)
    {
        $this->reflector = $reflector;
        $docblock = $reflector->getDocComment();

        // strip asterisks and leading spaces
        $docblock = trim(preg_replace(
            ['/^[ \t]*\/\*+[ \t]*/m', '/[ \t]*\*+[ \t]*/m', '/\*+\/\s*$/m', '/\s*\/\s*$/m'],
            ['', '', '', ''],
            $docblock
        ));

        // get all tags
        $tags = [];
        if (preg_match_all('/^@(\w+)\s+(.*)$/m', $docblock, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $tags[$match[1]][] = trim($match[2]);
            }
        }

        // strip the tags from the docblock
        $docblock = preg_replace('/^@(\w+)\s+(.*)$/m', '', $docblock);

        // what remains is summary and description
        [$summary, $description] = sexplode("\n\n", $docblock, 2, '');

        // store everything
        $this->summary = trim($summary);
        $this->description = trim($description);
        $this->tags = $tags;
    }

    /**
     * The class name of the declaring class
     *
     * @return string
     */
    protected function getContext()
    {
        return $this->reflector->getDeclaringClass()->getName();
    }

    /**
     * Get the first line of the description
     *
     * @return string
     */
    public function getSummary()
    {
        return $this->summary;
    }

    /**
     * Get the full description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Get all tags
     *
     * @return array
     */
    public function getTags()
    {
        return $this->tags;
    }

    /**
     * Get a specific tag
     *
     * @param string $tag
     * @return array
     */
    public function getTag($tag)
    {
        if (!isset($this->tags[$tag])) return [];
        return $this->tags[$tag];
    }
}