Skip to content

Commit

Permalink
Merge pull request #234 from phpDocumentor/fix/233-in_function_define…
Browse files Browse the repository at this point in the history
…d_const

Fix #233 inline defined const
  • Loading branch information
jaapio authored Jan 4, 2022
2 parents 7666c4b + 88e6692 commit d0fcff5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/phpDocumentor/Reflection/Php/Factory/ContextStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use phpDocumentor\Reflection\Php\Project;
use phpDocumentor\Reflection\Types\Context as TypeContext;

use function array_reverse;
use function end;

final class ContextStack
Expand Down Expand Up @@ -73,4 +74,26 @@ public function peek()

return $element;
}

/**
* Returns the first element of type.
*
* Will reverse search the stack for an element matching $type. Will return null when the element type is not
* in the current stack.
*
* @param class-string $type
*
* @return Element|FileElement|null
*/
public function search(string $type)
{
$reverseElements = array_reverse($this->elements);
foreach ($reverseElements as $element) {
if ($element instanceof $type) {
return $element;
}
}

return null;
}
}
2 changes: 1 addition & 1 deletion src/phpDocumentor/Reflection/Php/Factory/Define.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function doCreate(
return;
}

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

$constant = new ConstantElement(
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/data/Luigi/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
const OVEN_TEMPERATURE = 9001;
define('\\Luigi\\MAX_OVEN_TEMPERATURE', 9002);
define('OUTSIDE_OVEN_TEMPERATURE', 9002);

function in_function_define(){
define('IN_FUNCTION_OVEN_TEMPERATURE', 9003);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use OutOfBoundsException;
use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
use phpDocumentor\Reflection\Php\Method;
use phpDocumentor\Reflection\Php\Project;
use phpDocumentor\Reflection\Types\Context;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
Expand Down Expand Up @@ -89,4 +90,33 @@ public function testCreateWithTypeContext(): void
self::assertSame($project, $context->getProject());
self::assertSame($typeContext, $context->getTypeContext());
}

/**
* @covers ::__construct
* @covers ::search
*/
public function testSearchEmptyStackResultsInNull(): void
{
$project = new Project('myProject');
$context = new ContextStack($project);

self::assertNull($context->search(ClassElement::class));
}

/**
* @covers ::__construct
* @covers ::search
*/
public function testSearchStackForExistingElementTypeWillReturnTheFirstHit(): void
{
$class = new ClassElement(new Fqsen('\MyClass'));
$project = new Project('myProject');
$context = new ContextStack($project);
$context = $context
->push(new ClassElement(new Fqsen('\OtherClass')))
->push($class)
->push(new Method(new Fqsen('\MyClass::method()')));

self::assertSame($class, $context->search(ClassElement::class));
}
}

0 comments on commit d0fcff5

Please sign in to comment.