Skip to content

Commit e2fa610

Browse files
authored
Merge pull request #815 from kukulich/php81-function-is-static
PHP 8.1: Implemented `ReflectionFunction::isStatic()`
2 parents c913191 + 14b2305 commit e2fa610

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

src/Reflection/Adapter/ReflectionFunction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,6 @@ public function getClosureUsedVariables(): array
231231

232232
public function isStatic(): bool
233233
{
234-
throw new Exception\NotImplemented('Not implemented');
234+
return $this->betterReflectionFunction->isStatic();
235235
}
236236
}

src/Reflection/ReflectionFunction.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public function isDisabled(): bool
7777
return false;
7878
}
7979

80+
public function isStatic(): bool
81+
{
82+
$node = $this->getAst();
83+
84+
return ($node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction) && $node->static;
85+
}
86+
8087
/**
8188
* @throws NotImplemented
8289
* @throws FunctionDoesNotExist

test/unit/Reflection/Adapter/ReflectionFunctionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function methodExpectationProvider(): array
9191
['invoke', null, null, []],
9292
['invokeArgs', null, null, [[]]],
9393
['getClosure', null, $closure, []],
94-
['isStatic', NotImplemented::class, null, []],
94+
['isStatic', null, true, []],
9595
];
9696
}
9797

test/unit/Reflection/ReflectionFunctionTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,47 @@ public function testCreateFromClosureWithArrowFunctionCanReflectTypesInNamespace
185185
self::assertSame(ClassWithStaticMethod::class, $theParam->getName());
186186
}
187187

188+
public function testIsStaticFromClosure(): void
189+
{
190+
// phpcs:disable SlevomatCodingStandard.Functions.RequireArrowFunction
191+
$closure = static function () {
192+
return 5;
193+
};
194+
// phpcs:enable
195+
$reflection = ReflectionFunction::createFromClosure($closure);
196+
self::assertTrue($reflection->isStatic());
197+
}
198+
199+
public function testIsNotStaticFromClosure(): void
200+
{
201+
// phpcs:disable SlevomatCodingStandard.Functions.RequireArrowFunction
202+
// phpcs:disable SlevomatCodingStandard.Functions.StaticClosure.ClosureNotStatic
203+
$closure = function () {
204+
return 5;
205+
};
206+
// phpcs:enable
207+
$reflection = ReflectionFunction::createFromClosure($closure);
208+
self::assertFalse($reflection->isStatic());
209+
}
210+
211+
public function testIsStaticFromArrowFunction(): void
212+
{
213+
$closure = static fn () => 5;
214+
215+
$reflection = ReflectionFunction::createFromClosure($closure);
216+
self::assertTrue($reflection->isStatic());
217+
}
218+
219+
public function testIsNotStaticFromArrowFunction(): void
220+
{
221+
// phpcs:disable SlevomatCodingStandard.Functions.StaticClosure.ClosureNotStatic
222+
$closure = fn () => 5;
223+
// phpcs:enable
224+
225+
$reflection = ReflectionFunction::createFromClosure($closure);
226+
self::assertFalse($reflection->isStatic());
227+
}
228+
188229
public function testToString(): void
189230
{
190231
require_once __DIR__ . '/../Fixture/Functions.php';

0 commit comments

Comments
 (0)