Skip to content

Commit c274375

Browse files
committed
[+]: "PhpCodeParser" add "getFromClassName()" + tests
1 parent b27f272 commit c274375

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ $phpClasses = $phpCode->getClasses();
5151
var_dump($phpClasses['voku\tests\SimpleClass']); // "PHPClass"-object
5252
```
5353

54+
Parse one class:
55+
```php
56+
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getFromClassName(Dummy::class);
57+
$phpClasses = $phpCode->getClasses();
58+
59+
var_dump($phpClasses[Dummy::class]); // "PHPClass"-object
60+
61+
var_dump($phpClasses[Dummy::class]->methods); // "PHPMethod[]"-objects
62+
63+
var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']); // "PHPMethod"-object
64+
65+
var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters); // "PHPParameter[]"-objects
66+
67+
var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters['useRandInt']); // "PHPParameter"-object
68+
69+
var_dump($phpClasses[Dummy::class]->methods['withoutPhpDocParam']->parameters['useRandInt']->type); // "bool"
70+
````
71+
5472
Parse one file:
5573
```php
5674
$phpCode = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/Dummy.php');

src/voku/SimplePhpParser/Parsers/PhpCodeParser.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ public static function getFromString(
4141
);
4242
}
4343

44+
/**
45+
* @param string $className
46+
* @param string[] $autoloaderProjectPaths
47+
*
48+
* @pslam-param class-string $className
49+
*
50+
* @throws \Roave\BetterReflection\Reflector\Exception\IdentifierNotFound
51+
*
52+
* @return \voku\SimplePhpParser\Parsers\Helper\ParserContainer
53+
*/
54+
public static function getFromClassName(
55+
string $className,
56+
array $autoloaderProjectPaths = []
57+
): ParserContainer {
58+
$reflectionClass = ReflectionClass::createFromName($className);
59+
60+
return self::getPhpFiles(
61+
(string)$reflectionClass->getFileName(),
62+
$autoloaderProjectPaths
63+
);
64+
}
65+
4466
/**
4567
* @param string $pathOrCode
4668
* @param string[] $autoloaderProjectPaths

tests/ParserTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ class AnotherClass {}';
167167
static::assertCount(4, $phpClasses);
168168
}
169169

170+
public function testFromClassName(): void
171+
{
172+
$phpCode = PhpCodeParser::getFromClassName(Dummy::class);
173+
$phpClasses = $phpCode->getClasses();
174+
175+
static::assertSame('array<int, int>', $phpClasses['voku\tests\Dummy']->methods['withReturnType']->returnTypeFromPhpDocPslam);
176+
}
177+
170178
public function testSimpleStringInputConstants(): void
171179
{
172180
$code = '<?php

0 commit comments

Comments
 (0)