Skip to content

Commit

Permalink
Merge pull request #815 from kukulich/php81-function-is-static
Browse files Browse the repository at this point in the history
PHP 8.1: Implemented `ReflectionFunction::isStatic()`
  • Loading branch information
Ocramius authored Oct 14, 2021
2 parents c913191 + 14b2305 commit e2fa610
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,6 @@ public function getClosureUsedVariables(): array

public function isStatic(): bool
{
throw new Exception\NotImplemented('Not implemented');
return $this->betterReflectionFunction->isStatic();
}
}
7 changes: 7 additions & 0 deletions src/Reflection/ReflectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public function isDisabled(): bool
return false;
}

public function isStatic(): bool
{
$node = $this->getAst();

return ($node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction) && $node->static;
}

/**
* @throws NotImplemented
* @throws FunctionDoesNotExist
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Reflection/Adapter/ReflectionFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function methodExpectationProvider(): array
['invoke', null, null, []],
['invokeArgs', null, null, [[]]],
['getClosure', null, $closure, []],
['isStatic', NotImplemented::class, null, []],
['isStatic', null, true, []],
];
}

Expand Down
41 changes: 41 additions & 0 deletions test/unit/Reflection/ReflectionFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,47 @@ public function testCreateFromClosureWithArrowFunctionCanReflectTypesInNamespace
self::assertSame(ClassWithStaticMethod::class, $theParam->getName());
}

public function testIsStaticFromClosure(): void
{
// phpcs:disable SlevomatCodingStandard.Functions.RequireArrowFunction
$closure = static function () {
return 5;
};
// phpcs:enable
$reflection = ReflectionFunction::createFromClosure($closure);
self::assertTrue($reflection->isStatic());
}

public function testIsNotStaticFromClosure(): void
{
// phpcs:disable SlevomatCodingStandard.Functions.RequireArrowFunction
// phpcs:disable SlevomatCodingStandard.Functions.StaticClosure.ClosureNotStatic
$closure = function () {
return 5;
};
// phpcs:enable
$reflection = ReflectionFunction::createFromClosure($closure);
self::assertFalse($reflection->isStatic());
}

public function testIsStaticFromArrowFunction(): void
{
$closure = static fn () => 5;

$reflection = ReflectionFunction::createFromClosure($closure);
self::assertTrue($reflection->isStatic());
}

public function testIsNotStaticFromArrowFunction(): void
{
// phpcs:disable SlevomatCodingStandard.Functions.StaticClosure.ClosureNotStatic
$closure = fn () => 5;
// phpcs:enable

$reflection = ReflectionFunction::createFromClosure($closure);
self::assertFalse($reflection->isStatic());
}

public function testToString(): void
{
require_once __DIR__ . '/../Fixture/Functions.php';
Expand Down

0 comments on commit e2fa610

Please sign in to comment.