Skip to content

Commit

Permalink
[+]: "PhpCodeParser" -> use all types (not only from phpdoc) from par…
Browse files Browse the repository at this point in the history
…ent classes & interfaces
  • Loading branch information
Lars Moelleken committed Sep 23, 2022
1 parent f238b28 commit 9275289
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/voku/SimplePhpParser/Parsers/PhpCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ private static function mergeInheritdocData(
&&
$parentMethod->{$key} !== null
&&
\stripos($key, 'typeFromPhpDoc') !== false
\stripos($key, 'type') !== false
) {
$value = $parentMethod->{$key};
}
Expand Down Expand Up @@ -434,7 +434,7 @@ private static function mergeInheritdocData(
&&
$interfaceMethod->{$key} !== null
&&
\stripos($key, 'typeFromPhpDoc') !== false
\stripos($key, 'type') !== false
) {
$value = $interfaceMethod->{$key};
}
Expand Down Expand Up @@ -466,7 +466,7 @@ private static function mergeInheritdocData(
&&
$interfaceMethodParameter->{$keyInner} !== null
&&
\stripos($keyInner, 'typeFromPhpDoc') !== false
\stripos($keyInner, 'type') !== false
) {
$valueInner = $interfaceMethodParameter->{$keyInner};
}
Expand Down Expand Up @@ -495,7 +495,7 @@ private static function mergeInheritdocData(
&&
$parentMethod->{$key} !== null
&&
\stripos($key, 'typeFromPhpDoc') !== false
\stripos($key, 'type') !== false
) {
$value = $parentMethod->{$key};
}
Expand Down Expand Up @@ -527,7 +527,7 @@ private static function mergeInheritdocData(
&&
$parentMethodParameter->{$keyInner} !== null
&&
\stripos($keyInner, 'typeFromPhpDoc') !== false
\stripos($keyInner, 'type') !== false
) {
$valueInner = $parentMethodParameter->{$keyInner};
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Dummy12.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace voku\tests;

/**
* @internal
*/
final class Dummy12 implements DummyInterface
{
/**
* {@inheritdoc}
*/
public function withComplexReturnArray($parsedParamTag)
{
return [
'parsedParamTagStr' => 'foo',
'variableName' => [null],
];
}
}
10 changes: 10 additions & 0 deletions tests/Dummy8.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ public function foo_broken($lall)
{
return $lall;
}

/**
* @param callable(string): string $callback
*
* @return string
*/
public function withCallback($callback)
{
return $callback('foo');
}
}
18 changes: 18 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ public function testConstructorPropertyPromotion(): void
}
}

public function testInheritDocFromInterface(): void
{
$phpCode = PhpCodeParser::getPhpFiles(__DIR__ . '/Dummy12.php');
$phpClasses = $phpCode->getClasses();

$getFieldArray = $phpClasses[Dummy12::class]->methods['withComplexReturnArray'];
static::assertSame('withComplexReturnArray', $getFieldArray->name);
static::assertSame('\phpDocumentor\Reflection\DocBlock\Tags\BaseTag', $getFieldArray->parameters['parsedParamTag']->type);
static::assertSame('\phpDocumentor\Reflection\DocBlock\Tags\BaseTag', $getFieldArray->parameters['parsedParamTag']->typeFromPhpDocSimple);
static::assertSame('array', $getFieldArray->returnTypeFromPhpDocSimple);
static::assertSame('array{parsedParamTagStr: string, variableName: (null[]|string)}', $getFieldArray->returnTypeFromPhpDocExtended);
}

public function testSimpleOneClassWithTrait(): void
{
$phpCode = PhpCodeParser::getPhpFiles(__DIR__ . '/Dummy8.php');
Expand Down Expand Up @@ -176,6 +189,11 @@ public function testSimpleOneClassWithTrait(): void
static::assertNull($phpClasses[Dummy8::class]->methods['foo_broken']->returnTypeFromPhpDocExtended);

static::assertNull($phpClasses[Dummy8::class]->methods['foo_broken']->parameters['lall']->typeFromPhpDocExtended);

static::assertSame(
'callable(string ): string',
$phpClasses[Dummy8::class]->methods['withCallback']->parameters['callback']->typeFromPhpDocExtended
);
}

public function testSimpleOneTrait(): void
Expand Down

0 comments on commit 9275289

Please sign in to comment.