OptionsMethodDocBolck::docBlock()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 20
rs 9.8666
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
use function is_string;
12
13
/** @psalm-import-type OptionsDocBlock from Types */
14
final class OptionsMethodDocBolck
15
{
16
    /**
17
     * Return docBloc and parameter metas of method
18
     *
19
     * @return OptionsDocBlock
0 ignored issues
show
Bug introduced by
The type BEAR\Resource\OptionsDocBlock was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
     */
21
    public function __invoke(ReflectionMethod $method): array
22
    {
23
        $docComment = $method->getDocComment();
24
        $doc = $paramDoc = [];
25
        if (is_string($docComment)) {
0 ignored issues
show
introduced by
The condition is_string($docComment) is always true.
Loading history...
26
            [$doc, $paramDoc] = $this->docBlock($docComment);
27
        }
28
29
        return [$doc, $paramDoc];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($doc, $paramDoc) returns the type array<integer,array|array<string,array>> which is incompatible with the documented return type BEAR\Resource\OptionsDocBlock.
Loading history...
30
    }
31
32
    /**
33
     * @return (string|string[])[][]
34
     * @psalm-return OptionsDocBlock
35
     */
36
    private function docBlock(string $docComment): array
37
    {
38
        $factory = DocBlockFactory::createInstance();
39
        $docblock = $factory->create($docComment);
40
        $summary = $docblock->getSummary();
41
        $docs = $params = [];
42
        if ($summary) {
43
            $docs['summary'] = $summary;
44
        }
45
46
        $description = (string) $docblock->getDescription();
47
        if ($description) {
48
            $docs['description'] = $description;
49
        }
50
51
        /** @var Param[] $tags */
52
        $tags = $docblock->getTagsByName('param');
53
        $params = $this->docBlogTags($tags, $params);
54
55
        return [$docs, $params];
56
    }
57
58
    /**
59
     * @param Param[]                                                  $tags
60
     * @param array<string, array{type: string, description?: string}> $params
61
     *
62
     * @return array<string, array{type: string, description?: string}>
63
     */
64
    private function docBlogTags(array $tags, array $params): array
65
    {
66
        foreach ($tags as $tag) {
67
            $varName = (string) $tag->getVariableName();
68
            $tagType = (string) $tag->getType();
69
            $type = $tagType === 'int' ? 'integer' : $tagType;
70
            $params[$varName] = ['type' => $type];
71
            $description = (string) $tag->getDescription();
72
            if (! $description) {
73
                continue;
74
            }
75
76
            $params[$varName]['description'] = $description;
77
        }
78
79
        return $params;
80
    }
81
}
82