OptionsMethodDocBolck   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 66
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A docBlogTags() 0 16 4
A docBlock() 0 20 3
A __invoke() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use phpDocumentor\Reflection\DocBlock\Tags\Param;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BEAR\Resource\Param. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use phpDocumentor\Reflection\DocBlockFactory;
9
use ReflectionMethod;
10
11
final class OptionsMethodDocBolck
12
{
13
    /**
14
     * Return docBloc and parameter metas of method
15
     *
16
     * @return array{0: array{summary?: string, description?: string}, 1: array<string, array{type: string, description?: string}>}
17
     */
18
    public function __invoke(ReflectionMethod $method): array
19
    {
20
        $docComment = $method->getDocComment();
21
        $doc = $paramDoc = [];
22
        if ($docComment) {
23
            [$doc, $paramDoc] = $this->docBlock($docComment);
24
        }
25
26
        return [$doc, $paramDoc];
27
    }
28
29
    /**
30
     * @return (string|string[])[][]
31
     * @psalm-return array{0: array{summary?: string, description?: string}, 1: array<string, array{type: string, description?: string}>}
32
     */
33
    private function docBlock(string $docComment): array
34
    {
35
        $factory = DocBlockFactory::createInstance();
36
        $docblock = $factory->create($docComment);
37
        $summary = $docblock->getSummary();
38
        $docs = $params = [];
39
        if ($summary) {
40
            $docs['summary'] = $summary;
41
        }
42
43
        $description = (string) $docblock->getDescription();
44
        if ($description) {
45
            $docs['description'] = $description;
46
        }
47
48
        /** @var Param[] $tags */
49
        $tags = $docblock->getTagsByName('param');
50
        $params = $this->docBlogTags($tags, $params);
51
52
        return [$docs, $params];
53
    }
54
55
    /**
56
     * @param Param[]                                                  $tags
57
     * @param array<string, array{type: string, description?: string}> $params
58
     *
59
     * @return array<string, array{type: string, description?: string}>
60
     */
61
    private function docBlogTags(array $tags, array $params): array
62
    {
63
        foreach ($tags as $tag) {
64
            $varName = (string) $tag->getVariableName();
65
            $tagType = (string) $tag->getType();
66
            $type = $tagType === 'int' ? 'integer' : $tagType;
67
            $params[$varName] = ['type' => $type];
68
            $description = (string) $tag->getDescription();
69
            if (! $description) {
70
                continue;
71
            }
72
73
            $params[$varName]['description'] = $description;
74
        }
75
76
        return $params;
77
    }
78
}
79