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

AbstractMissingTypeHintRule::analyzeWithTypehint()   D

Complexity

Conditions 24
Paths 24

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 4.1666
c 0
b 0
f 0
cc 24
nc 24
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace TheCodingMachine\PHPStan\Rules\TypeHints;
5
6
use PHPStan\Reflection\Php\PhpFunctionReflection;
7
use PHPStan\Reflection\Php\PhpMethodReflection;
8
use PHPStan\Type\ArrayType;
9
use PHPStan\Type\BooleanType;
10
use PHPStan\Type\CallableType;
11
use PHPStan\Type\FloatType;
12
use PHPStan\Type\IntegerType;
13
use PHPStan\Type\IterableType;
14
use PHPStan\Type\MixedType;
15
use PHPStan\Type\NullType;
16
use PHPStan\Type\ObjectType;
17
use PHPStan\Type\ObjectWithoutClassType;
18
use PHPStan\Type\StringType;
19
use PHPStan\Type\Type;
20
use PhpParser\Node;
21
use PHPStan\Analyser\Scope;
22
use PHPStan\Broker\Broker;
23
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
24
use PHPStan\Reflection\Php\PhpParameterReflection;
25
use PHPStan\Rules\Rule;
26
use PHPStan\Type\UnionType;
27
use PHPStan\Type\VerbosityLevel;
28
29
abstract class AbstractMissingTypeHintRule implements Rule
30
{
31
32
    /**
33
     * @var Broker
34
     */
35
    private $broker;
36
37
    public function __construct(Broker $broker)
38
    {
39
        $this->broker = $broker;
40
    }
41
42
    abstract public function getNodeType(): string;
43
44
    abstract public function isReturnIgnored(Node $node): bool;
45
46
    abstract protected function getReflection(Node\FunctionLike $function, Scope $scope, Broker $broker) : ParametersAcceptorWithPhpDocs;
47
48
    abstract protected function shouldSkip(Node\FunctionLike $function, Scope $scope): bool;
49
50
    /**
51
     * @param \PhpParser\Node\Stmt\Function_|\PhpParser\Node\Stmt\ClassMethod $node
52
     * @param \PHPStan\Analyser\Scope $scope
53
     * @return string[]
54
     */
55
    public function processNode(Node $node, Scope $scope): array
56
    {
57
        /*if ($node->getLine() < 0) {
58
            // Fixes some problems with methods in anonymous class (the line number is poorly reported).
59
            return [];
60
        }*/
61
62
        if ($this->shouldSkip($node, $scope)) {
63
            return [];
64
        }
65
66
        $parametersAcceptor = $this->getReflection($node, $scope, $this->broker);
67
68
        $errors = [];
69
70
        foreach ($parametersAcceptor->getParameters() as $parameter) {
71
            $debugContext = new ParameterDebugContext($scope, $node, $parameter);
72
            $result = $this->analyzeParameter($debugContext, $parameter);
73
74
            if ($result !== null) {
75
                $errors[] = $result;
76
            }
77
        }
78
79
        if (!$this->isReturnIgnored($node)) {
80
            $debugContext = new FunctionDebugContext($scope, $node);
81
            $returnTypeError = $this->analyzeReturnType($debugContext, $parametersAcceptor);
82
            if ($returnTypeError !== null) {
83
                $errors[] = $returnTypeError;
84
            }
85
        }
86
87
        return $errors;
88
    }
89
90
    /**
91
     * Analyzes a parameter and returns the error string if something goes wrong or null if everything is ok.
92
     *
93
     * @param PhpParameterReflection $parameter
94
     * @return null|string
95
     */
96
    private function analyzeParameter(DebugContextInterface $context, PhpParameterReflection $parameter): ?string
97
    {
98
        //$typeResolver = new \phpDocumentor\Reflection\TypeResolver();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
100
        $phpTypeHint = $parameter->getNativeType();
101
        //try {
102
            $docBlockTypeHints = $parameter->getPhpDocType();
103
        /*} catch (\InvalidArgumentException $e) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
            return sprintf('%s, for parameter $%s, invalid docblock @param encountered. %s',
105
                $this->getContext($parameter),
106
                $parameter->getName(),
107
                $e->getMessage()
108
            );
109
        }*/
110
111
        if ($phpTypeHint instanceof MixedType && $phpTypeHint->isExplicitMixed() === false) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\MixedType 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...
112
            return $this->analyzeWithoutTypehint($context, $docBlockTypeHints);
113
        } else {
114
            // If there is a type-hint, we have nothing to say unless it is an array.
115
            if ($parameter->isVariadic()) {
116
                // Hack: wrap the native type in an array is variadic
117
                $phpTypeHint = new ArrayType(new IntegerType(), $phpTypeHint);
118
            }
119
120
            return $this->analyzeWithTypehint($context, $phpTypeHint, $docBlockTypeHints);
121
        }
122
    }
123
124
    /**
125
     * @return null|string
126
     */
127
    private function analyzeReturnType(DebugContextInterface $debugContext, ParametersAcceptorWithPhpDocs $function): ?string
128
    {
129
        $phpTypeHint = $function->getNativeReturnType();
130
        $docBlockTypeHints = $function->getPhpDocReturnType();
131
132
        // If there is a type-hint, we have nothing to say unless it is an array.
133
        if ($phpTypeHint instanceof MixedType && $phpTypeHint->isExplicitMixed() === false) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\MixedType 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...
134
            return $this->analyzeWithoutTypehint($debugContext, $docBlockTypeHints);
135
        } else {
136
            return $this->analyzeWithTypehint($debugContext, $phpTypeHint, $docBlockTypeHints);
137
        }
138
    }
139
140
    /**
141
     * @param DebugContextInterface $debugContext
142
     * @param Type $phpTypeHint
143
     * @param Type $docBlockTypeHints
144
     * @return null|string
145
     */
146
    private function analyzeWithTypehint(DebugContextInterface $debugContext, Type $phpTypeHint, Type $docBlockTypeHints): ?string
147
    {
148
        $docblockWithoutNullable = $this->typesWithoutNullable($docBlockTypeHints);
149
150
        if (!$this->isTypeIterable($phpTypeHint)) {
151
            // FIXME: this should be handled with the "accepts" method of types (and actually, this is already triggered by PHPStan 0.10)
152
153
            if ($docBlockTypeHints instanceof MixedType && $docBlockTypeHints->isExplicitMixed() === false) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\MixedType 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...
154
                // No docblock.
155
                return null;
156
            }
157
158
            // Let's detect mismatches between docblock and PHP typehint
159
            if ($docblockWithoutNullable instanceof UnionType) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\UnionType 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...
160
                $docblocks = $docblockWithoutNullable->getTypes();
161
            } else {
162
                $docblocks = [$docblockWithoutNullable];
163
            }
164
            $phpTypeHintWithoutNullable = $this->typesWithoutNullable($phpTypeHint);
165
            foreach ($docblocks as $docblockTypehint) {
166
                if (get_class($docblockTypehint) !== get_class($phpTypeHintWithoutNullable)) {
167
                    if ($debugContext instanceof ParameterDebugContext) {
168
                        return sprintf('%s type is type-hinted to "%s" but the @param annotation says it is a "%s". Please fix the @param annotation.', (string) $debugContext, $phpTypeHint->describe(VerbosityLevel::typeOnly()), $docblockTypehint->describe(VerbosityLevel::typeOnly()));
169
                    } elseif (!$docblockTypehint instanceof MixedType || $docblockTypehint->isExplicitMixed()) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\MixedType 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...
170
                        return sprintf('%s return type is type-hinted to "%s" but the @return annotation says it is a "%s". Please fix the @return annotation.', (string) $debugContext, $phpTypeHint->describe(VerbosityLevel::typeOnly()), $docblockTypehint->describe(VerbosityLevel::typeOnly()));
171
                    }
172
                }
173
            }
174
175
            return null;
176
        }
177
178
        if ($phpTypeHint instanceof ArrayType) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\ArrayType 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...
179
            if ($docblockWithoutNullable instanceof MixedType && !$docblockWithoutNullable->isExplicitMixed()) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\MixedType 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...
180
                if ($debugContext instanceof ParameterDebugContext) {
181
                    return sprintf('%s type is "array". Please provide a @param annotation to further specify the type of the array. For instance: @param int[] $%s', (string) $debugContext, $debugContext->getName());
182
                } else {
183
                    return sprintf('%s return type is "array". Please provide a @param annotation to further specify the type of the array. For instance: @return int[]', (string) $debugContext);
184
                }
185
            } else {
186
                if ($docblockWithoutNullable instanceof UnionType) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\UnionType 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...
187
                    $docblocks = $docblockWithoutNullable->getTypes();
188
                } else {
189
                    $docblocks = [$docblockWithoutNullable];
190
                }
191
                foreach ($docblocks as $docblockTypehint) {
192
                    if (!$this->isTypeIterable($docblockTypehint)) {
193
                        if ($debugContext instanceof ParameterDebugContext) {
194
                            return sprintf('%s mismatching type-hints for parameter %s. PHP type hint is "array" and docblock type hint is %s.', (string) $debugContext, $debugContext->getName(), $docblockTypehint->describe(VerbosityLevel::typeOnly()));
195
                        } else {
196
                            return sprintf('%s mismatching type-hints for return type. PHP type hint is "array" and docblock declared return type is %s.', (string) $debugContext, $docblockTypehint->describe(VerbosityLevel::typeOnly()));
197
                        }
198
                    }
199
200
                    if ($docblockTypehint instanceof ArrayType && $docblockTypehint->getKeyType() instanceof MixedType && $docblockTypehint->getItemType() instanceof MixedType && $docblockTypehint->getKeyType()->isExplicitMixed() && $docblockTypehint->getItemType()->isExplicitMixed()) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\ArrayType 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...
Bug introduced by
The class PHPStan\Type\MixedType 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...
201
                        if ($debugContext instanceof ParameterDebugContext) {
202
                            return sprintf('%s type is "array". Please provide a more specific @param annotation in the docblock. For instance: @param int[] $%s. Use @param mixed[] $%s if this is really an array of mixed values.', (string) $debugContext, $debugContext->getName(), $debugContext->getName());
203
                        } else {
204
                            return sprintf('%s return type is "array". Please provide a more specific @return annotation. For instance: @return int[]. Use @return mixed[] if this is really an array of mixed values.', (string) $debugContext);
205
                        }
206
                    }
207
                }
208
            }
209
        }
210
211
        return null;
212
    }
213
214
    private function isTypeIterable(Type $phpTypeHint) : bool
215
    {
216
        return /*$phpTypeHint->isIterable()->maybe() ||*/ $phpTypeHint->isIterable()->yes();
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
217
        /*if ($phpTypeHint instanceof Array_ || $phpTypeHint instanceof Iterable_) {
218
            return true;
219
        }
220
        if ($phpTypeHint instanceof Object_) {
221
            // TODO: cache BetterReflection for better performance!
222
            try {
223
                $class = (new BetterReflection())->classReflector()->reflect((string) $phpTypeHint);
224
            } catch (IdentifierNotFound $e) {
225
                // Class not found? Let's not throw an error. It will be caught by other rules anyway.
226
                return false;
227
            }
228
            if ($class->implementsInterface('Traversable')) {
229
                return true;
230
            }
231
        }
232
233
        return false;*/
234
    }
235
236
    /**
237
     * @param DebugContextInterface $debugContext
238
     * @param Type $docBlockTypeHints
239
     * @return null|string
240
     */
241
    private function analyzeWithoutTypehint(DebugContextInterface $debugContext, Type $docBlockTypeHints): ?string
242
    {
243
        if ($docBlockTypeHints instanceof MixedType && $docBlockTypeHints->isExplicitMixed() === false) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\MixedType 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...
244
            if ($debugContext instanceof ParameterDebugContext) {
245
                return sprintf('%s has no type-hint and no @param annotation.', (string) $debugContext);
246
            } else {
247
                return sprintf('%s there is no return type and no @return annotation.', (string) $debugContext);
248
            }
249
        }
250
251
        $nativeTypehint = $this->isNativelyTypehintable($docBlockTypeHints);
252
253
        if ($nativeTypehint !== null) {
254
            if ($debugContext instanceof ParameterDebugContext) {
255
                return sprintf('%s can be type-hinted to "%s".', (string) $debugContext, $nativeTypehint);
256
            } else {
257
                return sprintf('%s a "%s" return type can be added.', (string) $debugContext, $nativeTypehint);
258
            }
259
        }
260
261
        return null;
262
    }
263
264
    /**
265
     * @param Type $docBlockTypeHints
266
     * @return string|null
267
     */
268
    private function isNativelyTypehintable(Type $docBlockTypeHints): ?string
269
    {
270
        if ($docBlockTypeHints instanceof UnionType) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\UnionType 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...
271
            $count = count($docBlockTypeHints->getTypes());
272
        } else {
273
            $count = 1;
274
        }
275
276
        if ($count > 2) {
277
            return null;
278
        }
279
        $isNullable = $this->isNullable($docBlockTypeHints);
280
        if ($count === 2 && !$isNullable) {
281
            return null;
282
        }
283
284
        $type = $this->typesWithoutNullable($docBlockTypeHints);
285
        // At this point, there is at most one element here
286
        /*if (empty($type)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
287
            return null;
288
        }*/
289
290
        //$type = $types[0];
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
291
292
        // "object" type-hint is not available in PHP 7.1
293
        if ($type instanceof ObjectWithoutClassType) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\ObjectWithoutClassType 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...
294
            // In PHP 7.2, this is true but not in PHP 7.1
295
            return null;
296
        }
297
298
        if ($type instanceof ObjectType) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\ObjectType 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...
299
            return ($isNullable?'?':'').'\\'.$type->describe(VerbosityLevel::typeOnly());
300
        }
301
302
        if ($type instanceof ArrayType) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\ArrayType 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...
303
            return ($isNullable?'?':'').'array';
304
        }
305
306
        if ($this->isNativeType($type)) {
307
            return ($isNullable?'?':'').$type->describe(VerbosityLevel::typeOnly());
308
        }
309
310
        // TODO: more definitions to add here
311
        // Manage interface/classes
312
        // Manage array of things => (cast to array)
313
314
315
        return null;
316
    }
317
318
    private function isNativeType(Type $type): bool
319
    {
320
        if ($type instanceof StringType
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\StringType 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...
321
            || $type instanceof IntegerType
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\IntegerType 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...
322
            || $type instanceof BooleanType
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\BooleanType 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...
323
            || $type instanceof FloatType
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\FloatType 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...
324
            || $type instanceof CallableType
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\CallableType 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...
325
            || $type instanceof IterableType
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\IterableType 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...
326
        ) {
327
            return true;
328
        }
329
        return false;
330
    }
331
332
    /**
333
     * @param Type $docBlockTypeHints
334
     * @return bool
335
     */
336
    private function isNullable(Type $docBlockTypeHints): bool
337
    {
338
        if ($docBlockTypeHints instanceof UnionType) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\UnionType 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...
339
            foreach ($docBlockTypeHints->getTypes() as $docBlockTypeHint) {
340
                if ($docBlockTypeHint instanceof NullType) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\NullType 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...
341
                    return true;
342
                }
343
            }
344
        }
345
        return false;
346
    }
347
348
    /**
349
     * Removes "null" from the list of types.
350
     *
351
     * @param Type $docBlockTypeHints
352
     * @return Type
353
     */
354
    private function typesWithoutNullable(Type $docBlockTypeHints): Type
355
    {
356
        if ($docBlockTypeHints instanceof UnionType) {
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\UnionType 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...
357
            $filteredTypes = array_values(array_filter($docBlockTypeHints->getTypes(), function (Type $item) {
358
                return !$item instanceof NullType;
0 ignored issues
show
Bug introduced by
The class PHPStan\Type\NullType 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...
359
            }));
360
            if (\count($filteredTypes) === 1) {
361
                return $filteredTypes[0];
362
            }
363
            return new UnionType($filteredTypes);
364
        }
365
        return $docBlockTypeHints;
366
    }
367
}
368