Skip to content

Commit 5c650ec

Browse files
Added support for namespace level constants
1 parent fd8db77 commit 5c650ec

File tree

11 files changed

+92
-37
lines changed

11 files changed

+92
-37
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ completion for your IDE when encrypting your library with e.g.
1414
A basic installation via Composer could be done this way:
1515

1616
```bash
17-
$ composer require setasign/php-stub-generator ^0.3.0-alpha
17+
$ composer require setasign/php-stub-generator ^0.4
1818
```
1919

20-
Composer will install the library to your project's `vendor/setasign/php-stub-generator` directory.
20+
Composer will install the tool to your project's `vendor/setasign/php-stub-generator` directory.
2121

2222

2323
## Basic usage
2424

2525
```php
2626
<?php
27+
declare(strict_types=1);
2728

2829
use setasign\PhpStubGenerator\PhpStubGenerator;
2930
use setasign\PhpStubGenerator\Reader\AllFiles;
@@ -46,5 +47,10 @@ Alternatively you could just call the cli helper.
4647
vendor/bin/php-stub-generator generate vendor/setasign/setapdf-core/library setapdf-core-stub.php
4748
```
4849

49-
## TODO
50-
- Traits are not supported yet
50+
## Drawbacks / TODOs
51+
- Traits are not supported yet and probably won't be because of bugs like [this](https://bugs.php.net/bug.php?id=69180).
52+
The actual reflection api doesn't give enough information to rebuild the conflict resolution block.
53+
Additionally the "declaring class" of imported trait methods is the importing class and not like expected the trait.
54+
- Calculated constants or constants that use other constants like \_\_DIR\_\_ will be filled with the values of the
55+
runtime environment.
56+
- At the moment we only support public constants due to [goaop/parser-reflection](https://github.com/goaop/parser-reflection).

composer.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Formatter/ClassFormatter.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ public function format(bool $ignoreSubElements = false): string
124124
. $t . '{' . $n;
125125

126126
if (!$ignoreSubElements) {
127-
// todo implement traitUseBlockFormatter
128127
// $result .= (new TraitUseBlockFormatter($this->class))->format();
129128

130129
foreach ($this->class->getConstants() as $constantName => $constantValue) {

src/Formatter/TraitUseBlockFormatter.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class TraitUseBlockFormatter
2020
*/
2121
public function __construct(ReflectionClass $reflectionClass)
2222
{
23+
throw new \BadMethodCallException('TraitUseBlockFormatter isn\'t implemented!');
2324
$this->class = $reflectionClass;
2425
}
2526

@@ -31,19 +32,26 @@ public function format(): string
3132
$n = PhpStubGenerator::$eol;
3233
$t = PhpStubGenerator::$tab;
3334

34-
//$traits = $class->getTraits();
35+
$traits = $this->class->getTraitNames();
36+
if (\count($traits) === 0) {
37+
return '';
38+
}
39+
var_dump($this->class->getName());
40+
var_dump($this->class->getTraitAliases());
41+
var_dump($traits);
42+
// die();
3543
// todo needed?
36-
// $traits = array_filter($traits, function (ReflectionClass $trait) use ($traits) {
37-
// foreach ($traits as $compareTrait) {
38-
// /**
39-
// * @var ReflectionClass $compareTrait
40-
// */
41-
// if ($trait->($compareTrait->getName())) {
42-
// return false;
43-
// }
44-
// }
45-
// return true;
46-
// });
44+
// $traits = \array_filter($traits, function (ReflectionClass $trait) use ($traits) {
45+
// foreach ($traits as $compareTrait) {
46+
// /**
47+
// * @var ReflectionClass $compareTrait
48+
// */
49+
// if ($trait->($compareTrait->getName())) {
50+
// return false;
51+
// }
52+
// }
53+
// return true;
54+
// });
4755
//$class->getTraitAliases();
4856
// todo use traits
4957

src/Parser/GoaopParserReflection/GoaopParserReflectionParser.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class GoaopParserReflectionParser implements ParserInterface
2929
*/
3030
private $functions;
3131

32+
/**
33+
* @var null|array
34+
*/
35+
private $constants;
36+
3237
/**
3338
* @var null|array
3439
*/
@@ -86,6 +91,18 @@ public function getFunctions(): array
8691
return $this->functions;
8792
}
8893

94+
/**
95+
* @inheritDoc
96+
*/
97+
public function getConstants(): array
98+
{
99+
if ($this->constants === null) {
100+
throw new \BadMethodCallException('GoaopParserReflectionParser::parse wasn\'t called yet!');
101+
}
102+
103+
return $this->constants;
104+
}
105+
89106
/**
90107
* @inheritdoc
91108
*/
@@ -116,7 +133,7 @@ protected function resolveNamespaces(): void
116133
return new ReflectionFile($file);
117134
}, $files);
118135

119-
$fileNamespaces = \array_map(function (ReflectionFile $file) {
136+
$allFileNamespaces = \array_map(function (ReflectionFile $file) {
120137
return $file->getFileNamespaces();
121138
}, $files);
122139

@@ -125,8 +142,9 @@ protected function resolveNamespaces(): void
125142
*/
126143
$classes = [];
127144
$functions = [];
145+
$constants = [];
128146
$aliases = [];
129-
\array_walk($fileNamespaces, function (array $fileNamespaces) use (&$classes, &$functions, &$aliases) {
147+
foreach ($allFileNamespaces as $fileNamespaces) {
130148
foreach ($fileNamespaces as $fileNamespace) {
131149
/**
132150
* @var ReflectionFileNamespace $fileNamespace
@@ -144,10 +162,15 @@ protected function resolveNamespaces(): void
144162
$functions[$namespace][$functionName] = $function;
145163
$aliases[self::TYPE_FUNCTION][$functionName] = $namespaceAliases;
146164
}
165+
166+
foreach ($fileNamespace->getConstants(true) as $constantName => $constantValue) {
167+
$constants[$namespace][$constantName] = $constantValue;
168+
}
147169
}
148-
});
170+
}
149171
$this->classes = $classes;
150172
$this->functions = $functions;
173+
$this->constants = $constants;
151174
$this->aliases = $aliases;
152175
}
153176

src/Parser/GoaopParserReflection/ReflectionConst.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function getDeclaringClass()
7272
* @param $stmts
7373
* @return Class_
7474
*/
75-
private function findClassNode($stmts): Class_
75+
private function findClassNode($stmts): ?Class_
7676
{
7777
foreach ($stmts as $node) {
7878
if ($node instanceof Namespace_) {

src/Parser/ParserInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public function getClasses(): array;
2525
*/
2626
public function getFunctions(): array;
2727

28+
/**
29+
* @return array Returns an array like this: ['NamespaceName' => ['ConstantName' => 'ConstantValue']]
30+
* @throws \BadMethodCallException If parse wasn't called yet.
31+
*/
32+
public function getConstants(): array;
33+
2834
/**
2935
* Returns an array with all use aliases for $classOrFunctionName.
3036
*

src/PhpStubGenerator.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function generate(): string
6161
{
6262
$n = self::$eol;
6363
$result = '<?php' . $n
64+
. '/** @noinspection ALL */' . $n . $n
6465
. '// @codingStandardsIgnoreFile' . $n . $n;
6566

6667
$parser = $this->getParser();
@@ -74,7 +75,7 @@ public function generate(): string
7475
$isGlobalNamespace = $namespace === '';
7576
$result .= 'namespace' . (!$isGlobalNamespace ? ' ' . $namespace : '') . $n
7677
. '{' . $n;
77-
$result .= $this->formatAliases(
78+
$result .= $this->formatNamespaceAliases(
7879
$parser->getAliases($class->getName(), ParserInterface::TYPE_CLASS)
7980
);
8081
$result .= $n
@@ -91,7 +92,7 @@ public function generate(): string
9192
$isGlobalNamespace = ($namespace === '');
9293
$result .= 'namespace' . (!$isGlobalNamespace ? ' ' . $namespace : '') . $n
9394
. '{' . $n;
94-
$result .= $this->formatAliases(
95+
$result .= $this->formatNamespaceAliases(
9596
$parser->getAliases($function->getName(), ParserInterface::TYPE_FUNCTION)
9697
);
9798
$result .= $n
@@ -107,13 +108,22 @@ public function generate(): string
107108
* @param array $aliases
108109
* @return string
109110
*/
110-
protected function formatAliases(array $aliases): string
111+
protected function formatNamespaceAliases(array $aliases): string
111112
{
112113
$n = self::$eol;
113114
$t = self::$tab;
114115

115116
$result = '';
116117
foreach ($aliases as $fullName => $alias) {
118+
// todo: can be removed when this pull request is confirmed and tagged
119+
// https://github.com/goaop/parser-reflection/pull/96
120+
if ($alias === null) {
121+
\preg_match('~\\\\?(?P<last>[^\\\\]+)$~', $fullName, $matches);
122+
$alias = $matches['last'] ?? $fullName;
123+
} else {
124+
$alias = (string) $alias;
125+
}
126+
117127
$result .= $t . 'use ' . $fullName;
118128
if ($alias !== \substr($fullName, -\strlen($alias))) {
119129
$result .= ' as ' . $alias;

tests/unit/Formatter/ClassFormatterTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace setasign\PhpStubGenerator\Tests\unit\Formatter;
55

6+
use PHPUnit\Framework\MockObject\MockObject;
67
use PHPUnit\Framework\TestCase;
78
use setasign\PhpStubGenerator\Formatter\ClassFormatter;
89
use setasign\PhpStubGenerator\Parser\ParserInterface;
@@ -17,7 +18,7 @@ protected function createParserInterfaceMock(): ParserInterface
1718
->getMockForAbstractClass();
1819
}
1920

20-
protected function createReflectionClassMock(): \PHPUnit_Framework_MockObject_MockObject
21+
protected function createReflectionClassMock(): MockObject
2122
{
2223
return $this->getMockBuilder(\ReflectionClass::class)
2324
->disableOriginalConstructor()

tests/unit/Formatter/ConstantFormatterTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace setasign\PhpStubGenerator\Tests\unit\Formatter;
55

6+
use PHPUnit\Framework\MockObject\MockObject;
67
use PHPUnit\Framework\TestCase;
78
use setasign\PhpStubGenerator\Formatter\ConstantFormatter;
89
use setasign\PhpStubGenerator\Parser\ParserInterface;
@@ -11,21 +12,21 @@
1112

1213
class ConstantFormatterTest extends TestCase
1314
{
14-
protected function createParserInterfaceMock(): \PHPUnit_Framework_MockObject_MockObject
15+
protected function createParserInterfaceMock(): MockObject
1516
{
1617
return $this->getMockBuilder(ParserInterface::class)
1718
->setMethods(['getConstantReflection'])
1819
->getMockForAbstractClass();
1920
}
2021

21-
protected function createReflectionConstMock(): \PHPUnit_Framework_MockObject_MockObject
22+
protected function createReflectionConstMock(): MockObject
2223
{
2324
return $this->getMockBuilder(ReflectionConst::class)
2425
->setMethods(['getDocComment'])
2526
->getMockForAbstractClass();
2627
}
2728

28-
protected function createReflectionClassMock(): \PHPUnit_Framework_MockObject_MockObject
29+
protected function createReflectionClassMock(): MockObject
2930
{
3031
return $this->getMockBuilder(\ReflectionClass::class)
3132
->disableOriginalConstructor()

0 commit comments

Comments
 (0)