Completed
Push — master ( 985ca9...e0892e )
by David
18s
created

ParameterDebugContext::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\PHPStan\Rules\TypeHints;
5
6
7
use PhpParser\Node\FunctionLike;
8
use PhpParser\Node\Name;
9
use PhpParser\Node\Stmt\ClassMethod;
10
use PhpParser\Node\Stmt\Function_;
11
use PHPStan\Analyser\Scope;
12
use PHPStan\Reflection\Php\PhpParameterReflection;
13
14
class ParameterDebugContext implements DebugContextInterface
15
{
16
    /**
17
     * @var FunctionLike
18
     */
19
    private $function;
20
    /**
21
     * @var PhpParameterReflection
22
     */
23
    private $parameter;
24
    /**
25
     * @var Scope
26
     */
27
    private $scope;
28
29
    public function __construct(Scope $scope, FunctionLike $function, PhpParameterReflection $parameter)
30
    {
31
32
        $this->function = $function;
33
        $this->parameter = $parameter;
34
        $this->scope = $scope;
35
    }
36
37
    public function __toString()
38
    {
39
        if ($this->function instanceof ClassMethod) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Stmt\ClassMethod does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
40
            if (!$this->scope->isInClass()) {
41
                return 'Should not happen';
42
            }
43
44
            return sprintf('In method "%s::%s", parameter $%s', $this->scope->getClassReflection()->getDisplayName(), $this->function->name->name, $this->parameter->getName());
45
        }
46
        elseif ($this->function instanceof Function_) {
0 ignored issues
show
Bug introduced by
The class PhpParser\Node\Stmt\Function_ does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47
            return sprintf('In function "%s", parameter $%s', $this->function->name->name, $this->parameter->getName());
48
        }
49
        return 'Should not happen';
50
    }
51
52
    public function getName(): string
53
    {
54
        return $this->parameter->getName();
55
    }
56
}