Issues (182)

src/OptionsMethodDocBolck.php (6 issues)

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