Skip to content

Fix #233 inline defined const #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}