Skip to content

Commit

Permalink
[+]: get more info from PhpReflection
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed May 29, 2020
1 parent a991924 commit d1722e3
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 21 deletions.
12 changes: 0 additions & 12 deletions src/voku/SimplePhpParser/Model/PHPClass.php
Expand Up @@ -113,10 +113,6 @@ public function readObjectFromReflection($clazz): self
}

foreach ($clazz->getProperties() as $property) {
if ($property->getDeclaringClass()->getName() !== $this->name) {
continue;
}

$propertyPhp = (new PHPProperty($this->usePhpReflection()))->readObjectFromReflection($property);
$this->properties[$propertyPhp->name] = $propertyPhp;
}
Expand All @@ -126,18 +122,10 @@ public function readObjectFromReflection($clazz): self
}

foreach ($clazz->getMethods() as $method) {
if ($method->getDeclaringClass()->getName() !== $this->name) {
continue;
}

$this->methods[$method->getName()] = (new PHPMethod($this->usePhpReflection()))->readObjectFromReflection($method);
}

foreach ($clazz->getReflectionConstants() as $constant) {
if ($constant->getDeclaringClass()->getName() !== $this->name) {
continue;
}

$this->constants[$constant->getName()] = (new PHPConst($this->usePhpReflection()))->readObjectFromReflection($constant);
}

Expand Down
8 changes: 0 additions & 8 deletions src/voku/SimplePhpParser/Model/PHPInterface.php
Expand Up @@ -66,19 +66,11 @@ public function readObjectFromReflection($interface): self
$this->name = $interface->getName();

foreach ($interface->getMethods() as $method) {
if ($method->getDeclaringClass()->getName() !== $this->name) {
continue;
}

$this->methods[$method->getName()] = (new PHPMethod($this->usePhpReflection()))->readObjectFromReflection($method);
}

$this->parentInterfaces = $interface->getInterfaceNames();
foreach ($interface->getReflectionConstants() as $constant) {
if ($constant->getDeclaringClass()->getName() !== $this->name) {
continue;
}

$this->constants[$constant->name] = (new PHPConst($this->usePhpReflection()))->readObjectFromReflection($constant);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Dummy.php
Expand Up @@ -18,7 +18,7 @@ function foo(int $foo = 0)
* @property int $foo
* @property string $bar
*/
final class Dummy extends \stdClass
class Dummy extends \stdClass
{
const FOO1 = [1, 2];

Expand Down
18 changes: 18 additions & 0 deletions tests/Dummy4.php
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace voku\tests;

/**
* @internal
*/
final class Dummy4 extends Dummy
{
/**
* @var null|int[]
*
* @phpstan-var null|array<int,int>
*/
public $lall4 = [];
}
227 changes: 227 additions & 0 deletions tests/ParserTest.php
Expand Up @@ -101,6 +101,233 @@ public function foo() { return []; }
static::assertSame('Foo', $phpClasses['Foo']->name);
}

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

$result = $phpClasses[Dummy4::class]->getMethodsInfo();

// DEBUG
//\var_export($result);

static::assertSame(
[
'withReturnType' => [
'fullDescription' => '',
'paramsTypes' => [
],
'returnTypes' => [
'type' => 'array',
'typeMaybeWithComment' => 'array<int,int>',
'typeFromPhpDoc' => 'array<int,int>',
'typeFromPhpDocSimple' => 'int[]',
'typeFromPhpDocPslam' => 'array<int, int>',
],
'line' => null,
'error' => '',
'is_deprecated' => false,
'is_meta' => false,
'is_internal' => false,
'is_removed' => false,
],
'withoutReturnType' => [
'fullDescription' => '',
'paramsTypes' => [
],
'returnTypes' => [
'type' => null,
'typeMaybeWithComment' => 'false|int',
'typeFromPhpDoc' => 'false|int',
'typeFromPhpDocSimple' => 'false|int',
'typeFromPhpDocPslam' => 'false|int',
],
'line' => null,
'error' => '',
'is_deprecated' => false,
'is_meta' => false,
'is_internal' => false,
'is_removed' => false,
],
'withoutPhpDocParam' => [
'fullDescription' => '',
'paramsTypes' => [
'useRandInt' => [
'type' => 'bool',
'typeMaybeWithComment' => null,
'typeFromPhpDoc' => null,
'typeFromPhpDocSimple' => null,
'typeFromPhpDocPslam' => null,
'typeFromDefaultValue' => 'bool',
],
],
'returnTypes' => [
'type' => null,
'typeMaybeWithComment' => 'int[]|string[]|null <p>foo</p>',
'typeFromPhpDoc' => 'int[]|string[]|null',
'typeFromPhpDocSimple' => 'int[]|string[]|null',
'typeFromPhpDocPslam' => 'list<int|string>|null',
],
'line' => null,
'error' => '',
'is_deprecated' => false,
'is_meta' => false,
'is_internal' => false,
'is_removed' => false,
],
'withPhpDocParam' => [
'fullDescription' => '',
'paramsTypes' => [
'useRandInt' => [
'type' => null,
'typeMaybeWithComment' => 'int[]|null $useRandInt',
'typeFromPhpDoc' => 'int[]|null',
'typeFromPhpDocSimple' => 'int[]|null',
'typeFromPhpDocPslam' => 'list<int>|null',
'typeFromDefaultValue' => 'array',
],
],
'returnTypes' => [
'type' => null,
'typeMaybeWithComment' => null,
'typeFromPhpDoc' => null,
'typeFromPhpDocSimple' => null,
'typeFromPhpDocPslam' => null,
],
'line' => null,
'error' => '',
'is_deprecated' => false,
'is_meta' => false,
'is_internal' => false,
'is_removed' => false,
],
'withPsalmPhpDocOnlyParam' => [
'fullDescription' => '',
'paramsTypes' => [
'useRandInt' => [
'type' => null,
'typeMaybeWithComment' => null,
'typeFromPhpDoc' => null,
'typeFromPhpDocSimple' => null,
'typeFromPhpDocPslam' => 'list<int>|null',
'typeFromDefaultValue' => 'array',
],
],
'returnTypes' => [
'type' => null,
'typeMaybeWithComment' => null,
'typeFromPhpDoc' => null,
'typeFromPhpDocSimple' => null,
'typeFromPhpDocPslam' => null,
],
'line' => null,
'error' => '',
'is_deprecated' => false,
'is_meta' => false,
'is_internal' => false,
'is_removed' => false,
],
'withComplexReturnArray' => [
'fullDescription' => '',
'paramsTypes' => [
'parsedParamTag' => [
'type' => '\\phpDocumentor\\Reflection\\DocBlock\\Tags\\BaseTag',
'typeMaybeWithComment' => '\\phpDocumentor\\Reflection\\DocBlock\\Tags\\BaseTag $parsedParamTag',
'typeFromPhpDoc' => '\\phpDocumentor\\Reflection\\DocBlock\\Tags\\BaseTag',
'typeFromPhpDocSimple' => '\\phpDocumentor\\Reflection\\DocBlock\\Tags\\BaseTag',
'typeFromPhpDocPslam' => 'phpDocumentor\\Reflection\\DocBlock\\Tags\\BaseTag',
'typeFromDefaultValue' => null,
],
],
'returnTypes' => [
'type' => null,
'typeMaybeWithComment' => 'array',
'typeFromPhpDoc' => 'array',
'typeFromPhpDocSimple' => 'array',
'typeFromPhpDocPslam' => 'array{parsedParamTagStr: string, variableName: array<array-key, null>|string}',
],
'line' => null,
'error' => '',
'is_deprecated' => false,
'is_meta' => false,
'is_internal' => false,
'is_removed' => false,
],
'withEmptyParamTypePhpDoc' => [
'fullDescription' => '',
'paramsTypes' => [
'parsedParamTag' => [
'type' => null,
'typeMaybeWithComment' => null,
'typeFromPhpDoc' => null,
'typeFromPhpDocSimple' => null,
'typeFromPhpDocPslam' => null,
'typeFromDefaultValue' => null,
],
],
'returnTypes' => [
'type' => null,
'typeMaybeWithComment' => 'array',
'typeFromPhpDoc' => 'array',
'typeFromPhpDocSimple' => 'array',
'typeFromPhpDocPslam' => 'array{parsedParamTagStr: string, variableName: array<array-key, null>|string}',
],
'line' => null,
'error' => '',
'is_deprecated' => false,
'is_meta' => false,
'is_internal' => false,
'is_removed' => false,
],
'withConstFromClass' => [
'fullDescription' => '',
'paramsTypes' => [
'p1' => [
'type' => null,
'typeMaybeWithComment' => 'mixed $p1',
'typeFromPhpDoc' => 'mixed',
'typeFromPhpDocSimple' => 'mixed',
'typeFromPhpDocPslam' => 'mixed',
'typeFromDefaultValue' => 'array',
],
'p2' => [
'type' => null,
'typeMaybeWithComment' => 'mixed $p2',
'typeFromPhpDoc' => 'mixed',
'typeFromPhpDocSimple' => 'mixed',
'typeFromPhpDocPslam' => 'mixed',
'typeFromDefaultValue' => 'string',
],
'p3' => [
'type' => null,
'typeMaybeWithComment' => 'mixed $p3',
'typeFromPhpDoc' => 'mixed',
'typeFromPhpDocSimple' => 'mixed',
'typeFromPhpDocPslam' => 'mixed',
'typeFromDefaultValue' => 'int',
],
],
'returnTypes' => [
'type' => null,
'typeMaybeWithComment' => 'array',
'typeFromPhpDoc' => 'array',
'typeFromPhpDocSimple' => 'array',
'typeFromPhpDocPslam' => 'array<array-key, mixed>',
],
'line' => null,
'error' => '',
'is_deprecated' => false,
'is_meta' => false,
'is_internal' => false,
'is_removed' => false,
],
],
$result
);
}

public function testGetMethodsInfoViaPhpReflectionOnly(): void
{
$phpCode = PhpCodeParser::getPhpFiles(
Expand Down

0 comments on commit d1722e3

Please sign in to comment.