From df44960e479e96c0576be085f43f58e50330f7c5 Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Fri, 10 Jul 2015 12:48:42 +0100 Subject: [PATCH 1/4] Added ReflectionMethod->getModifiers() --- docs/compatibility.md | 2 +- src/Reflection/ReflectionMethod.php | 17 ++++++++++ test/unit/Reflection/ReflectionMethodTest.php | 31 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/docs/compatibility.md b/docs/compatibility.md index 27b2e76ad..07e3c6d3c 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -86,7 +86,7 @@ The progress of compatibility can also be tracked in issue [#7](https://github.c |--------|-----------| | getClosure | :x: No - would require loading of the method itself (#14) | | getDeclaringClass | :heavy_check_mark: Yes | -| getModifiers | todo | +| getModifiers | :heavy_check_mark: Yes | | getPrototype | todo | | invoke | :x: No - would require loading of the method itself (#14) | | invokeArgs | :x: No - would require loading of the method itself (#14) | diff --git a/src/Reflection/ReflectionMethod.php b/src/Reflection/ReflectionMethod.php index 5dc6b1346..812a8d330 100644 --- a/src/Reflection/ReflectionMethod.php +++ b/src/Reflection/ReflectionMethod.php @@ -49,6 +49,23 @@ public static function createFromNode( return $method; } + /** + * Get the core-reflection-compatible modifier values + * + * @return int + */ + public function getModifiers() + { + $val = 0; + $val += $this->isStatic() ? \ReflectionMethod::IS_STATIC : 0; + $val += $this->isPublic() ? \ReflectionMethod::IS_PUBLIC : 0; + $val += $this->isProtected() ? \ReflectionMethod::IS_PROTECTED : 0; + $val += $this->isPrivate() ? \ReflectionMethod::IS_PRIVATE : 0; + $val += $this->isAbstract() ? \ReflectionMethod::IS_ABSTRACT : 0; + $val += $this->isFinal() ? \ReflectionMethod::IS_FINAL : 0; + return $val; + } + /** * Check to see if a flag is set on this method * diff --git a/test/unit/Reflection/ReflectionMethodTest.php b/test/unit/Reflection/ReflectionMethodTest.php index 3c0b342cb..54294985a 100644 --- a/test/unit/Reflection/ReflectionMethodTest.php +++ b/test/unit/Reflection/ReflectionMethodTest.php @@ -128,4 +128,35 @@ public function testMethodNameWithNamespace() $this->assertSame('', $methodInfo->getNamespaceName()); $this->assertSame('someMethod', $methodInfo->getShortName()); } + + public function modifierProvider() + { + return [ + ['publicMethod', 256, ['public']], + ['privateMethod', 1024, ['private']], + ['protectedMethod', 512, ['protected']], + ['finalPublicMethod', 260, ['final', 'public']], + ['abstractPublicMethod', 258, ['abstract', 'public']], + ['staticPublicMethod', 257, ['public', 'static']], + ['noVisibility', 256, ['public']], + ]; + } + + /** + * @param string $methodName + * @param int $expectedModifier + * @param string[] $expectedModifierNames + * @dataProvider modifierProvider + */ + public function testGetModifiers($methodName, $expectedModifier, array $expectedModifierNames) + { + $classInfo = $this->reflector->reflect('\BetterReflectionTest\Fixture\Methods'); + $method = $classInfo->getMethod($methodName); + + $this->assertSame($expectedModifier, $method->getModifiers()); + $this->assertSame( + $expectedModifierNames, + \Reflection::getModifierNames($method->getModifiers()) + ); + } } From d003214182099e961ef6fcbadd6cce942656b575 Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Fri, 10 Jul 2015 13:28:49 +0100 Subject: [PATCH 2/4] Added incomplete implementation of ReflectionMethod->getPrototype() --- .../Exception/MethodPrototypeNotFound.php | 7 +++++ src/Reflection/ReflectionMethod.php | 18 +++++++++++ test/unit/Fixture/PrototypeTree.php | 31 +++++++++++++++++++ test/unit/Reflection/ReflectionMethodTest.php | 12 +++++++ 4 files changed, 68 insertions(+) create mode 100644 src/Reflection/Exception/MethodPrototypeNotFound.php create mode 100644 test/unit/Fixture/PrototypeTree.php diff --git a/src/Reflection/Exception/MethodPrototypeNotFound.php b/src/Reflection/Exception/MethodPrototypeNotFound.php new file mode 100644 index 000000000..cfa005833 --- /dev/null +++ b/src/Reflection/Exception/MethodPrototypeNotFound.php @@ -0,0 +1,7 @@ +getDeclaringClass()->getName(), + $this->getName() + )); + } + /** * Get the core-reflection-compatible modifier values * diff --git a/test/unit/Fixture/PrototypeTree.php b/test/unit/Fixture/PrototypeTree.php new file mode 100644 index 000000000..66dd507c1 --- /dev/null +++ b/test/unit/Fixture/PrototypeTree.php @@ -0,0 +1,31 @@ +getModifiers()) ); } + + public function testGetPrototype() + { + $fixture = __DIR__ . '/../Fixture/PrototypeTree.php'; + $reflector = new ClassReflector(new SingleFileSourceLocator($fixture)); + + $this->setExpectedException(MethodPrototypeNotFound::class); + $reflector->reflect('ClassB')->getMethod('foo')->getPrototype(); + } } From d16e79a9b9c0c128ccfba3558f84b3fd62eab96f Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Fri, 10 Jul 2015 13:32:06 +0100 Subject: [PATCH 3/4] Updated compatibility.md --- docs/compatibility.md | 50 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/docs/compatibility.md b/docs/compatibility.md index 07e3c6d3c..1fc624959 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -12,7 +12,7 @@ The progress of compatibility can also be tracked in issue [#7](https://github.c | getDefaultProperties | todo | | getDocComment | todo | | getEndLine | todo | -| getExtension | :x: No - extensions are not supported (#15) | +| getExtension | :x: No - extensions are not supported ([#15](https://github.com/Roave/BetterReflection/issues/15)) | | getFileName | :heavy_check_mark: Yes | | getInterfaceNames | todo | | getInterfaces | todo | @@ -26,8 +26,8 @@ The progress of compatibility can also be tracked in issue [#7](https://github.c | getProperty | :heavy_check_mark: Yes | | getShortName | :heavy_check_mark: Yes | | getStartLine | todo | -| getStaticProperties | :x: No - would require loading (#14) | -| getStaticPropertyValue | :x: No - would require loading (#14) | +| getStaticProperties | :x: No - would require loading ([#14](https://github.com/Roave/BetterReflection/issues/14)) | +| getStaticPropertyValue | :x: No - would require loading ([#14](https://github.com/Roave/BetterReflection/issues/14)) | | getTraitAliases | todo | | getTraitNames | todo | | getTraits | todo | @@ -50,18 +50,18 @@ The progress of compatibility can also be tracked in issue [#7](https://github.c | newInstance | todo | | newInstanceArgs | todo | | newInstanceWithoutConstructor | todo | -| setStaticPropertyValue | :x: No - would require loading (#14) | +| setStaticPropertyValue | :x: No - would require loading ([#14](https://github.com/Roave/BetterReflection/issues/14)) | ## ReflectionFunctionAbstract | Method | Supported | |--------|-----------| -| getClosureScopeClass | :x: No - would require loading of the method itself (#14) | -| getClosureThis | :x: No - would require loading of the method itself (#14) | +| getClosureScopeClass | :x: No - would require loading of the method itself ([#14](https://github.com/Roave/BetterReflection/issues/14)) | +| getClosureThis | :x: No - would require loading of the method itself ([#14](https://github.com/Roave/BetterReflection/issues/14)) | | getDocComment | :heavy_check_mark: Yes | | getEndLine | :heavy_check_mark: Yes | -| getExtension | :x: No - extensions are not supported (#15) | -| getExtensionName | :x: No - extensions are not supported (#15) | +| getExtension | :x: No - extensions are not supported ([#15](https://github.com/Roave/BetterReflection/issues/15)) | +| getExtensionName | :x: No - extensions are not supported ([#15](https://github.com/Roave/BetterReflection/issues/15)) | | getFileName | :heavy_check_mark: Yes | | getName | :heavy_check_mark: Yes | | getNamespaceName | :heavy_check_mark: Yes | @@ -70,26 +70,26 @@ The progress of compatibility can also be tracked in issue [#7](https://github.c | getParameters | :heavy_check_mark: Yes | | getShortName | :heavy_check_mark: Yes | | getStartLine | :heavy_check_mark: Yes | -| getStaticVariables | :x: No - would require loading (#14) | +| getStaticVariables | :x: No - would require loading ([#14](https://github.com/Roave/BetterReflection/issues/14)) | | inNamespace | :heavy_check_mark: Yes | -| isClosure | :heavy_check_mark: Yes - but see issue (#37) | -| isDeprecated | :heavy_check_mark: Yes - but see issue (#38) | +| isClosure | :heavy_check_mark: Yes - but see issue ([#37](https://github.com/Roave/BetterReflection/issues/37)) | +| isDeprecated | :heavy_check_mark: Yes - but see issue ([#38](https://github.com/Roave/BetterReflection/issues/38)) | | isGenerator | :heavy_check_mark: Yes | -| isInternal | :heavy_check_mark: Yes - but see issue (#38) | +| isInternal | :heavy_check_mark: Yes - but see issue ([#38](https://github.com/Roave/BetterReflection/issues/38)) | | isUserDefined | :heavy_check_mark: Yes | | isVariadic | :heavy_check_mark: Yes | -| returnsReference | :heavy_check_mark: Ye | +| returnsReference | :heavy_check_mark: Yes | ## ReflectionMethod | Method | Supported | |--------|-----------| -| getClosure | :x: No - would require loading of the method itself (#14) | +| getClosure | :x: No - would require loading of the method itself ([#14](https://github.com/Roave/BetterReflection/issues/14)) | | getDeclaringClass | :heavy_check_mark: Yes | | getModifiers | :heavy_check_mark: Yes | -| getPrototype | todo | -| invoke | :x: No - would require loading of the method itself (#14) | -| invokeArgs | :x: No - would require loading of the method itself (#14) | +| getPrototype | todo - [#57](https://github.com/Roave/BetterReflection/issues/57) | +| invoke | :x: No - would require loading of the method itself ([#14](https://github.com/Roave/BetterReflection/issues/14)) | +| invokeArgs | :x: No - would require loading of the method itself ([#14](https://github.com/Roave/BetterReflection/issues/14)) | | isAbstract | :heavy_check_mark: Yes | | isConstructor | :heavy_check_mark: Yes | | isDestructor | :heavy_check_mark: Yes | @@ -98,7 +98,7 @@ The progress of compatibility can also be tracked in issue [#7](https://github.c | isProtected | :heavy_check_mark: Yes | | isPublic | :heavy_check_mark: Yes | | isStatic | :heavy_check_mark: Yes | -| setAccessible | :x: No - would require loading of the method itself (#14) | +| setAccessible | :x: No - would require loading of the method itself ([#14](https://github.com/Roave/BetterReflection/issues/14)) | | _inherited methods_ | see `ReflectionFunctionAbstract` | ## ReflectionParameter @@ -126,10 +126,10 @@ The progress of compatibility can also be tracked in issue [#7](https://github.c | Method | Supported | |--------|-----------| -| getClosure | :x: No - would require actual compilation of the AST (#14) | -| invoke | :x: No - would require loading of the function itself (#14) | -| invokeArgs | :x: No - would require loading of the function itself (#14) | -| isDisabled | :heavy_check_mark: Yes - but see issue (#38) | +| getClosure | :x: No - would require actual compilation of the AST ([#14](https://github.com/Roave/BetterReflection/issues/14)) | +| invoke | :x: No - would require loading of the function itself ([#14](https://github.com/Roave/BetterReflection/issues/14)) | +| invokeArgs | :x: No - would require loading of the function itself ([#14](https://github.com/Roave/BetterReflection/issues/14)) | +| isDisabled | :heavy_check_mark: Yes - but see issue ([#38](https://github.com/Roave/BetterReflection/issues/38)) | | _inherited methods_ | see `ReflectionFunctionAbstract` | ## ReflectionProperty @@ -140,14 +140,14 @@ The progress of compatibility can also be tracked in issue [#7](https://github.c | getDocComment | :heavy_check_mark: Yes | | getModifiers | :heavy_check_mark: Yes | | getName | :heavy_check_mark: Yes | -| getValue | :x: No - would require an instance of an object (#14) | +| getValue | :x: No - would require an instance of an object ([#14](https://github.com/Roave/BetterReflection/issues/14)) | | isDefault | :heavy_check_mark: Yes | | isPrivate | :heavy_check_mark: Yes | | isProtected | :heavy_check_mark: Yes | | isPublic | :heavy_check_mark: Yes | | isStatic | :heavy_check_mark: Yes | -| setAccessible | :x: No - would require an instance of an object (#14) | -| setValue | :x: No - would require an instance of an object (#14) | +| setAccessible | :x: No - would require an instance of an object ([#14](https://github.com/Roave/BetterReflection/issues/14)) | +| setValue | :x: No - would require an instance of an object ([#14](https://github.com/Roave/BetterReflection/issues/14)) | ## ReflectionExtension From 236fe8a23fe80d22c8c3705232af2b757db76129 Mon Sep 17 00:00:00 2001 From: James Titcumb Date: Fri, 10 Jul 2015 14:43:54 +0100 Subject: [PATCH 4/4] Use constants to refer to flags in tests so BC does not break if values change --- test/unit/Reflection/ReflectionMethodTest.php | 14 +++++++------- test/unit/Reflection/ReflectionPropertyTest.php | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/unit/Reflection/ReflectionMethodTest.php b/test/unit/Reflection/ReflectionMethodTest.php index 43892339b..2bb463a76 100644 --- a/test/unit/Reflection/ReflectionMethodTest.php +++ b/test/unit/Reflection/ReflectionMethodTest.php @@ -135,13 +135,13 @@ public function testMethodNameWithNamespace() public function modifierProvider() { return [ - ['publicMethod', 256, ['public']], - ['privateMethod', 1024, ['private']], - ['protectedMethod', 512, ['protected']], - ['finalPublicMethod', 260, ['final', 'public']], - ['abstractPublicMethod', 258, ['abstract', 'public']], - ['staticPublicMethod', 257, ['public', 'static']], - ['noVisibility', 256, ['public']], + ['publicMethod', \ReflectionMethod::IS_PUBLIC, ['public']], + ['privateMethod', \ReflectionMethod::IS_PRIVATE, ['private']], + ['protectedMethod', \ReflectionMethod::IS_PROTECTED, ['protected']], + ['finalPublicMethod', \ReflectionMethod::IS_FINAL | \ReflectionMethod::IS_PUBLIC, ['final', 'public']], + ['abstractPublicMethod', \ReflectionMethod::IS_ABSTRACT | \ReflectionMethod::IS_PUBLIC, ['abstract', 'public']], + ['staticPublicMethod', \ReflectionMethod::IS_STATIC | \ReflectionMethod::IS_PUBLIC, ['public', 'static']], + ['noVisibility', \ReflectionMethod::IS_PUBLIC, ['public']], ]; } diff --git a/test/unit/Reflection/ReflectionPropertyTest.php b/test/unit/Reflection/ReflectionPropertyTest.php index 1d5b5831a..7582aed1e 100644 --- a/test/unit/Reflection/ReflectionPropertyTest.php +++ b/test/unit/Reflection/ReflectionPropertyTest.php @@ -123,10 +123,10 @@ public function testExportThrowsException() public function modifierProvider() { return [ - ['publicProperty', 256, ['public']], - ['protectedProperty', 512, ['protected']], - ['privateProperty', 1024, ['private']], - ['publicStaticProperty', 257, ['public', 'static']], + ['publicProperty', \ReflectionProperty::IS_PUBLIC, ['public']], + ['protectedProperty', \ReflectionProperty::IS_PROTECTED, ['protected']], + ['privateProperty', \ReflectionProperty::IS_PRIVATE, ['private']], + ['publicStaticProperty', \ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_STATIC, ['public', 'static']], ]; }