Skip to content

Commit d0fcff5

Browse files
authored
Merge pull request #234 from phpDocumentor/fix/233-in_function_defined_const
Fix #233 inline defined const
2 parents 7666c4b + 88e6692 commit d0fcff5

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use phpDocumentor\Reflection\Php\Project;
1111
use phpDocumentor\Reflection\Types\Context as TypeContext;
1212

13+
use function array_reverse;
1314
use function end;
1415

1516
final class ContextStack
@@ -73,4 +74,26 @@ public function peek()
7374

7475
return $element;
7576
}
77+
78+
/**
79+
* Returns the first element of type.
80+
*
81+
* Will reverse search the stack for an element matching $type. Will return null when the element type is not
82+
* in the current stack.
83+
*
84+
* @param class-string $type
85+
*
86+
* @return Element|FileElement|null
87+
*/
88+
public function search(string $type)
89+
{
90+
$reverseElements = array_reverse($this->elements);
91+
foreach ($reverseElements as $element) {
92+
if ($element instanceof $type) {
93+
return $element;
94+
}
95+
}
96+
97+
return null;
98+
}
7699
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected function doCreate(
9999
return;
100100
}
101101

102-
$file = $context->peek();
102+
$file = $context->search(FileElement::class);
103103
assert($file instanceof FileElement);
104104

105105
$constant = new ConstantElement(

tests/integration/data/Luigi/constants.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
const OVEN_TEMPERATURE = 9001;
66
define('\\Luigi\\MAX_OVEN_TEMPERATURE', 9002);
77
define('OUTSIDE_OVEN_TEMPERATURE', 9002);
8+
9+
function in_function_define(){
10+
define('IN_FUNCTION_OVEN_TEMPERATURE', 9003);
11+
}

tests/unit/phpDocumentor/Reflection/Php/Factory/ContextStackTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use OutOfBoundsException;
88
use phpDocumentor\Reflection\Fqsen;
99
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
10+
use phpDocumentor\Reflection\Php\Method;
1011
use phpDocumentor\Reflection\Php\Project;
1112
use phpDocumentor\Reflection\Types\Context;
1213
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
@@ -89,4 +90,33 @@ public function testCreateWithTypeContext(): void
8990
self::assertSame($project, $context->getProject());
9091
self::assertSame($typeContext, $context->getTypeContext());
9192
}
93+
94+
/**
95+
* @covers ::__construct
96+
* @covers ::search
97+
*/
98+
public function testSearchEmptyStackResultsInNull(): void
99+
{
100+
$project = new Project('myProject');
101+
$context = new ContextStack($project);
102+
103+
self::assertNull($context->search(ClassElement::class));
104+
}
105+
106+
/**
107+
* @covers ::__construct
108+
* @covers ::search
109+
*/
110+
public function testSearchStackForExistingElementTypeWillReturnTheFirstHit(): void
111+
{
112+
$class = new ClassElement(new Fqsen('\MyClass'));
113+
$project = new Project('myProject');
114+
$context = new ContextStack($project);
115+
$context = $context
116+
->push(new ClassElement(new Fqsen('\OtherClass')))
117+
->push($class)
118+
->push(new Method(new Fqsen('\MyClass::method()')));
119+
120+
self::assertSame($class, $context->search(ClassElement::class));
121+
}
92122
}

0 commit comments

Comments
 (0)