Skip to content

Commit fd6f345

Browse files
committed
Merge remote-tracking branch 'origin/1.12.x' into 2.1.x
2 parents da7141a + 10388a9 commit fd6f345

File tree

3 files changed

+122
-132
lines changed

3 files changed

+122
-132
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,6 @@ parameters:
174174
count: 1
175175
path: src/Diagnose/PHPStanDiagnoseExtension.php
176176

177-
-
178-
message: '#^Variable method call on PHPStan\\Reflection\\ClassReflection\.$#'
179-
identifier: method.dynamicName
180-
count: 2
181-
path: src/PhpDoc/PhpDocBlock.php
182-
183-
-
184-
message: '#^Variable static method call on PHPStan\\PhpDoc\\PhpDocBlock\.$#'
185-
identifier: staticMethod.dynamicName
186-
count: 1
187-
path: src/PhpDoc/PhpDocBlock.php
188-
189177
-
190178
message: '#^Call to function method_exists\(\) with PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocNode and ''getParamOutTypeTagV…'' will always evaluate to true\.$#'
191179
identifier: function.alreadyNarrowedType

src/PhpDoc/PhpDocBlock.php

Lines changed: 122 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
use PHPStan\Reflection\ClassReflection;
88
use PHPStan\Reflection\MethodReflection;
99
use PHPStan\Reflection\Php\PhpMethodReflection;
10-
use PHPStan\Reflection\Php\PhpPropertyReflection;
11-
use PHPStan\Reflection\PropertyReflection;
1210
use PHPStan\Reflection\ResolvedMethodReflection;
13-
use PHPStan\Reflection\ResolvedPropertyReflection;
1411
use PHPStan\Type\ConditionalTypeForParameter;
1512
use PHPStan\Type\Type;
1613
use PHPStan\Type\TypeTraverser;
@@ -114,74 +111,71 @@ public function transformAssertTagParameterWithParameterNameMapping(AssertTagPar
114111
return $parameter;
115112
}
116113

117-
/**
118-
* @param array<int, string> $originalPositionalParameterNames
119-
* @param array<int, string> $newPositionalParameterNames
120-
*/
121114
public static function resolvePhpDocBlockForProperty(
122115
?string $docComment,
123116
ClassReflection $classReflection,
124117
?string $trait,
125118
string $propertyName,
126119
?string $file,
127120
?bool $explicit,
128-
array $originalPositionalParameterNames, // unused
129-
array $newPositionalParameterNames, // unused
130121
): self
131122
{
132-
$docBlocksFromParents = self::resolveParentPhpDocBlocks(
133-
self::getParentReflections($classReflection),
134-
$propertyName,
135-
'hasNativeProperty',
136-
'getNativeProperty',
137-
__FUNCTION__,
138-
$explicit ?? $docComment !== null,
139-
$newPositionalParameterNames,
140-
);
123+
$docBlocksFromParents = [];
124+
foreach (self::getParentReflections($classReflection) as $parentReflection) {
125+
$oneResult = self::resolvePropertyPhpDocBlockFromClass(
126+
$parentReflection,
127+
$propertyName,
128+
$explicit ?? $docComment !== null,
129+
);
130+
131+
if ($oneResult === null) { // Null if it is private or from a wrong trait.
132+
continue;
133+
}
134+
135+
$docBlocksFromParents[] = $oneResult;
136+
}
141137

142138
return new self(
143139
$docComment ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING,
144140
$file,
145141
$classReflection,
146142
$trait,
147143
$explicit ?? true,
148-
self::remapParameterNames($originalPositionalParameterNames, $newPositionalParameterNames),
144+
[],
149145
$docBlocksFromParents,
150146
);
151147
}
152148

153-
/**
154-
* @param array<int, string> $originalPositionalParameterNames
155-
* @param array<int, string> $newPositionalParameterNames
156-
*/
157149
public static function resolvePhpDocBlockForConstant(
158150
?string $docComment,
159151
ClassReflection $classReflection,
160-
?string $trait, // unused
161152
string $constantName,
162153
?string $file,
163154
?bool $explicit,
164-
array $originalPositionalParameterNames, // unused
165-
array $newPositionalParameterNames, // unused
166155
): self
167156
{
168-
$docBlocksFromParents = self::resolveParentPhpDocBlocks(
169-
self::getParentReflections($classReflection),
170-
$constantName,
171-
'hasConstant',
172-
'getConstant',
173-
__FUNCTION__,
174-
$explicit ?? $docComment !== null,
175-
$newPositionalParameterNames,
176-
);
157+
$docBlocksFromParents = [];
158+
foreach (self::getParentReflections($classReflection) as $parentReflection) {
159+
$oneResult = self::resolveConstantPhpDocBlockFromClass(
160+
$parentReflection,
161+
$constantName,
162+
$explicit ?? $docComment !== null,
163+
);
164+
165+
if ($oneResult === null) { // Null if it is private or from a wrong trait.
166+
continue;
167+
}
168+
169+
$docBlocksFromParents[] = $oneResult;
170+
}
177171

178172
return new self(
179173
$docComment ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING,
180174
$file,
181175
$classReflection,
182-
$trait,
176+
null,
183177
$explicit ?? true,
184-
self::remapParameterNames($originalPositionalParameterNames, $newPositionalParameterNames),
178+
[],
185179
$docBlocksFromParents,
186180
);
187181
}
@@ -219,15 +213,21 @@ public static function resolvePhpDocBlockForMethod(
219213
$parentReflections[] = $traitReflection;
220214
}
221215

222-
$docBlocksFromParents = self::resolveParentPhpDocBlocks(
223-
$parentReflections,
224-
$methodName,
225-
'hasNativeMethod',
226-
'getNativeMethod',
227-
__FUNCTION__,
228-
$explicit ?? $docComment !== null,
229-
$newPositionalParameterNames,
230-
);
216+
$docBlocksFromParents = [];
217+
foreach ($parentReflections as $parentReflection) {
218+
$oneResult = self::resolveMethodPhpDocBlockFromClass(
219+
$parentReflection,
220+
$methodName,
221+
$explicit ?? $docComment !== null,
222+
$newPositionalParameterNames,
223+
);
224+
225+
if ($oneResult === null) { // Null if it is private or from a wrong trait.
226+
continue;
227+
}
228+
229+
$docBlocksFromParents[] = $oneResult;
230+
}
231231

232232
return new self(
233233
$docComment ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING,
@@ -262,117 +262,124 @@ private static function remapParameterNames(
262262
}
263263

264264
/**
265-
* @param array<int, ClassReflection> $parentReflections
266-
* @param array<int, string> $positionalParameterNames
267-
* @return array<int, self>
265+
* @return array<int, ClassReflection>
268266
*/
269-
private static function resolveParentPhpDocBlocks(
270-
array $parentReflections,
267+
private static function getParentReflections(ClassReflection $classReflection): array
268+
{
269+
$result = [];
270+
271+
$parent = $classReflection->getParentClass();
272+
if ($parent !== null) {
273+
$result[] = $parent;
274+
}
275+
276+
foreach ($classReflection->getInterfaces() as $interface) {
277+
$result[] = $interface;
278+
}
279+
280+
return $result;
281+
}
282+
283+
private static function resolveConstantPhpDocBlockFromClass(
284+
ClassReflection $classReflection,
271285
string $name,
272-
string $hasMethodName,
273-
string $getMethodName,
274-
string $resolveMethodName,
275286
bool $explicit,
276-
array $positionalParameterNames,
277-
): array
287+
): ?self
278288
{
279-
$result = [];
289+
if ($classReflection->hasConstant($name)) {
290+
$parentReflection = $classReflection->getConstant($name);
291+
if ($parentReflection->isPrivate()) {
292+
return null;
293+
}
280294

281-
foreach ($parentReflections as $parentReflection) {
282-
$oneResult = self::resolvePhpDocBlockFromClass(
283-
$parentReflection,
295+
$classReflection = $parentReflection->getDeclaringClass();
296+
297+
return self::resolvePhpDocBlockForConstant(
298+
$parentReflection->getDocComment() ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING,
299+
$classReflection,
284300
$name,
285-
$hasMethodName,
286-
$getMethodName,
287-
$resolveMethodName,
301+
$classReflection->getFileName(),
288302
$explicit,
289-
$positionalParameterNames,
290303
);
291-
292-
if ($oneResult === null) { // Null if it is private or from a wrong trait.
293-
continue;
294-
}
295-
296-
$result[] = $oneResult;
297304
}
298305

299-
return $result;
306+
return null;
300307
}
301308

302-
/**
303-
* @return array<int, ClassReflection>
304-
*/
305-
private static function getParentReflections(ClassReflection $classReflection): array
309+
private static function resolvePropertyPhpDocBlockFromClass(
310+
ClassReflection $classReflection,
311+
string $name,
312+
bool $explicit,
313+
): ?self
306314
{
307-
$result = [];
315+
if ($classReflection->hasNativeProperty($name)) {
316+
$parentReflection = $classReflection->getNativeProperty($name);
317+
if ($parentReflection->isPrivate()) {
318+
return null;
319+
}
308320

309-
$parent = $classReflection->getParentClass();
310-
if ($parent !== null) {
311-
$result[] = $parent;
312-
}
321+
$classReflection = $parentReflection->getDeclaringClass();
322+
$traitReflection = $parentReflection->getDeclaringTrait();
313323

314-
foreach ($classReflection->getInterfaces() as $interface) {
315-
$result[] = $interface;
324+
$trait = $traitReflection !== null
325+
? $traitReflection->getName()
326+
: null;
327+
328+
return self::resolvePhpDocBlockForProperty(
329+
$parentReflection->getDocComment() ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING,
330+
$classReflection,
331+
$trait,
332+
$name,
333+
$classReflection->getFileName(),
334+
$explicit,
335+
);
316336
}
317337

318-
return $result;
338+
return null;
319339
}
320340

321341
/**
322342
* @param array<int, string> $positionalParameterNames
323343
*/
324-
private static function resolvePhpDocBlockFromClass(
344+
private static function resolveMethodPhpDocBlockFromClass(
325345
ClassReflection $classReflection,
326346
string $name,
327-
string $hasMethodName,
328-
string $getMethodName,
329-
string $resolveMethodName,
330347
bool $explicit,
331348
array $positionalParameterNames,
332349
): ?self
333350
{
334-
if ($classReflection->$hasMethodName($name)) {
335-
/** @var PropertyReflection|MethodReflection|ClassConstantReflection $parentReflection */
336-
$parentReflection = $classReflection->$getMethodName($name);
351+
if ($classReflection->hasNativeMethod($name)) {
352+
$parentReflection = $classReflection->getNativeMethod($name);
337353
if ($parentReflection->isPrivate()) {
338354
return null;
339355
}
340356

341357
$classReflection = $parentReflection->getDeclaringClass();
342-
343-
if ($parentReflection instanceof PhpPropertyReflection || $parentReflection instanceof ResolvedPropertyReflection) {
358+
$traitReflection = null;
359+
if ($parentReflection instanceof PhpMethodReflection || $parentReflection instanceof ResolvedMethodReflection) {
344360
$traitReflection = $parentReflection->getDeclaringTrait();
345-
$positionalMethodParameterNames = [];
346-
} elseif ($parentReflection instanceof MethodReflection) {
347-
$traitReflection = null;
348-
if ($parentReflection instanceof PhpMethodReflection || $parentReflection instanceof ResolvedMethodReflection) {
349-
$traitReflection = $parentReflection->getDeclaringTrait();
350-
}
351-
$methodVariants = $parentReflection->getVariants();
352-
$positionalMethodParameterNames = [];
353-
$lowercaseMethodName = strtolower($parentReflection->getName());
354-
if (
355-
count($methodVariants) === 1
356-
&& $lowercaseMethodName !== '__construct'
357-
&& $lowercaseMethodName !== strtolower($parentReflection->getDeclaringClass()->getName())
358-
) {
359-
$methodParameters = $methodVariants[0]->getParameters();
360-
foreach ($methodParameters as $methodParameter) {
361-
$positionalMethodParameterNames[] = $methodParameter->getName();
362-
}
363-
} else {
364-
$positionalMethodParameterNames = $positionalParameterNames;
361+
}
362+
$methodVariants = $parentReflection->getVariants();
363+
$positionalMethodParameterNames = [];
364+
$lowercaseMethodName = strtolower($parentReflection->getName());
365+
if (
366+
count($methodVariants) === 1
367+
&& $lowercaseMethodName !== '__construct'
368+
&& $lowercaseMethodName !== strtolower($parentReflection->getDeclaringClass()->getName())
369+
) {
370+
$methodParameters = $methodVariants[0]->getParameters();
371+
foreach ($methodParameters as $methodParameter) {
372+
$positionalMethodParameterNames[] = $methodParameter->getName();
365373
}
366374
} else {
367-
$traitReflection = null;
368-
$positionalMethodParameterNames = [];
375+
$positionalMethodParameterNames = $positionalParameterNames;
369376
}
370377

371378
$trait = $traitReflection !== null
372379
? $traitReflection->getName()
373380
: null;
374381

375-
return self::$resolveMethodName(
382+
return self::resolvePhpDocBlockForMethod(
376383
$parentReflection->getDocComment() ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING,
377384
$classReflection,
378385
$trait,

src/PhpDoc/PhpDocInheritanceResolver.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ public function resolvePhpDocForProperty(
3333
$propertyName,
3434
$classReflectionFileName,
3535
null,
36-
[],
37-
[],
3836
);
3937

4038
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, $declaringTraitName, null, $propertyName, null);
@@ -50,12 +48,9 @@ public function resolvePhpDocForConstant(
5048
$phpDocBlock = PhpDocBlock::resolvePhpDocBlockForConstant(
5149
$docComment,
5250
$classReflection,
53-
null,
5451
$constantName,
5552
$classReflectionFileName,
5653
null,
57-
[],
58-
[],
5954
);
6055

6156
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, null, null, null, $constantName);

0 commit comments

Comments
 (0)