Skip to content

Commit eec8173

Browse files
committed
Add support for conditional function definitions
Elements can be defined in a conditional way, this allows elements to be added to the docs. Fixes #141
1 parent 2019338 commit eec8173

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/phpDocumentor/Reflection/Php/Factory/File.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ private function createElements(
130130
) : void {
131131
foreach ($nodes as $node) {
132132
switch (get_class($node)) {
133+
case Node\Stmt\If_::class:
134+
$this->createElements($node->stmts, $file, $strategies, $context);
135+
136+
foreach ($node->elseifs as $subNode) {
137+
$this->createElements($subNode->stmts, $file, $strategies, $context);
138+
}
139+
140+
if ($node->else instanceof Node\Stmt\Else_) {
141+
$this->createElements($node->else->stmts, $file, $strategies, $context);
142+
}
143+
break;
133144
case Node\Stmt\Expression::class:
134145
try {
135146
$strategy = $strategies->findMatching($node);

tests/integration/FileDocblockTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
/**
1212
* Integration tests to check the correct working of processing a namespace into a project.
13-
*
14-
* @coversNothing
1513
*/
1614
final class FileDocblockTest extends TestCase
1715
{
@@ -39,13 +37,30 @@ public function testFileDocblock(string $fileName) : void
3937
);
4038
}
4139

42-
43-
public function fileProvider(string $file)
40+
public function fileProvider() : array
4441
{
4542
return [
4643
[ __DIR__ . '/data/GlobalFiles/empty.php' ],
4744
[ __DIR__ . '/data/GlobalFiles/empty_with_declare.php' ],
4845
[ __DIR__ . '/data/GlobalFiles/empty_shebang.php' ],
4946
];
5047
}
48+
49+
/**
50+
* @covers \phpDocumentor\Reflection\Php\Factory\File::create
51+
* @covers \phpDocumentor\Reflection\Php\Factory\File::<private>
52+
*/
53+
public function testConditionalFunctionDefine() : void
54+
{
55+
$fileName = __DIR__ . '/data/GlobalFiles/conditional_function.php';
56+
$project = $this->fixture->create(
57+
'MyProject',
58+
[new LocalFile($fileName)]
59+
);
60+
61+
$this->assertCount(
62+
4,
63+
$project->getFiles()[$fileName]->getFunctions()
64+
);
65+
}
5166
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
if (!function_exists('h')) {
5+
function h() {
6+
7+
}
8+
} elseif(!function_exists('i')) {
9+
if (true) {
10+
function i()
11+
{
12+
}
13+
}
14+
} else {
15+
function j() {
16+
17+
}
18+
}
19+
20+
function a() {
21+
22+
}

0 commit comments

Comments
 (0)