Completed
Push — master ( 0b540b...eed11b )
by Mike
04:38
created

UrlGenerator/Standard/ConstantDescriptor.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Transformer\Router\UrlGenerator\Standard;
17
18
use phpDocumentor\Descriptor;
19
use phpDocumentor\Transformer\Router\UrlGenerator\UrlGeneratorInterface as UrlGenerator;
20
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21
22
class ConstantDescriptor implements UrlGenerator
23
{
24
    private $urlGenerator;
25
    private $converter;
26
27 3
    public function __construct(UrlGeneratorInterface $urlGenerator, QualifiedNameToUrlConverter $converter)
28
    {
29 3
        $this->urlGenerator = $urlGenerator;
30 3
        $this->converter = $converter;
31 3
    }
32
33
    /**
34
     * Generates a URL from the given node or returns false if unable.
35
     *
36
     * @param string|Descriptor\ConstantDescriptor $node
37
     *
38
     * @return string
39
     */
40 3
    public function __invoke($node)
41
    {
42 3
        if ($this->isGlobalConstant($node)) {
0 ignored issues
show
It seems like $node defined by parameter $node on line 40 can also be of type string; however, phpDocumentor\Transforme...tor::isGlobalConstant() does only seem to accept object<phpDocumentor\Des...tor\ConstantDescriptor>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
43 2
            return $this->generateUrlForGlobalConstant($node);
44
        }
45
46 1
        return $this->generateUrlForClassConstant($node);
47
    }
48
49 2
    private function generateUrlForGlobalConstant(Descriptor\ConstantDescriptor $node): string
50
    {
51 2
        return $this->urlGenerator->generate(
52 2
            'global_constant',
53
            [
54 2
                'namespaceName' => $this->converter->fromNamespace($node->getNamespace()),
55 2
                'constantName' => $node->getName()
56
            ]
57
        );
58
    }
59
60 1
    private function generateUrlForClassConstant(Descriptor\ConstantDescriptor $node): string
61
    {
62 1
        return $this->urlGenerator->generate(
63 1
            'class_constant',
64
            [
65 1
                'className' => $this->converter->fromNamespace(
66 1
                    $node->getParent()->getFullyQualifiedStructuralElementName()
67
                ),
68 1
                'constantName' => $node->getName()
69
            ]
70
        );
71
    }
72
73 3
    private function isGlobalConstant(Descriptor\ConstantDescriptor $node): bool
74
    {
75 3
        return ($node->getParent() instanceof Descriptor\FileDescriptor || !$node->getParent());
76
    }
77
}
78