Inheritance: implements BetterReflection\Reflection\Reflection
 /**
  * @param ReflectionClass $class
  *
  * @return self
  */
 public static function fromReflectionClass(ReflectionClass $class)
 {
     $type = 'class';
     if ($class->isTrait()) {
         $type = 'trait';
     }
     return new self(sprintf('Provided node "%s" is not interface, but "%s"', $class->getName(), $type));
 }
 /**
  * @param Reflector $reflector
  * @param MethodNode $node
  * @param ReflectionClass $declaringClass
  * @return ReflectionMethod
  */
 public static function createFromNode(Reflector $reflector, MethodNode $node, ReflectionClass $declaringClass)
 {
     $method = new self();
     $method->declaringClass = $declaringClass;
     // Compat with core reflection means we should NOT pass namespace info
     // for ReflectionMethod
     $method->populateFunctionAbstract($reflector, $node, $declaringClass->getLocatedSource(), null);
     return $method;
 }
 /**
  * @param MethodNode $node
  * @param ReflectionClass $declaringClass
  * @return ReflectionMethod
  */
 public static function createFromNode(MethodNode $node, ReflectionClass $declaringClass)
 {
     $method = new self($node);
     $method->declaringClass = $declaringClass;
     // Compat with core reflection means we should NOT pass namespace info
     // for ReflectionMethod
     $method->populateFunctionAbstract($node, $declaringClass->getLocatedSource(), null);
     $method->flags |= $node->isAbstract() ? self::IS_ABSTRACT : 0;
     $method->flags |= $node->isFinal() ? self::IS_FINAL : 0;
     $method->flags |= $node->isPrivate() ? self::IS_PRIVATE : 0;
     $method->flags |= $node->isProtected() ? self::IS_PROTECTED : 0;
     $method->flags |= $node->isPublic() ? self::IS_PUBLIC : 0;
     $method->flags |= $node->isStatic() ? self::IS_STATIC : 0;
     return $method;
 }
 public function methods() : Methods
 {
     return new Methods($this->name(), array_map(function (ReflectionMethod $method) {
         $returnType = $this->typeFactory->create($this->reflector, $method->getReturnType() ? $method->getReturnType()->getTypeObject() : new Mixed(), $method->getDocBlockReturnTypes(), $method->getReturnType() ? $method->getReturnType()->allowsNull() : false);
         $parameters = array_map(function (ReflectionParameter $parameter) {
             return new Parameter($parameter, $this->typeFactory->create($this->reflector, $parameter->getTypeHint(), $parameter->getDocBlockTypeStrings(), $parameter->allowsNull()), $parameter->getPosition(), $parameter->isOptional());
         }, $method->getParameters());
         return new Method($method, $this->annotations->scanForAnnotations($method->getDocComment(), $this->fileName(), $this->imports), new Parameters($this->name() . '::' . $method->getName(), $parameters), $returnType);
     }, $this->reflectionClass->getMethods()));
 }
 /**
  * Take an AST node in some located source (potentially in a namespace) and
  * convert it to a Reflection
  *
  * @param Reflector $reflector
  * @param Node $node
  * @param LocatedSource $locatedSource
  * @param Node\Stmt\Namespace_|null $namespace
  * @return Reflection|null
  */
 public function __invoke(Reflector $reflector, Node $node, LocatedSource $locatedSource, Node\Stmt\Namespace_ $namespace = null)
 {
     if ($node instanceof Node\Stmt\ClassLike) {
         return ReflectionClass::createFromNode($reflector, $node, $locatedSource, $namespace);
     }
     if ($node instanceof Node\FunctionLike) {
         return ReflectionFunction::createFromNode($reflector, $node, $locatedSource, $namespace);
     }
     return null;
 }
Example #6
0
 /**
  * @param Node $node
  * @param LocatedSource $locatedSource
  * @param Node\Stmt\Namespace_|null $namespace
  * @return Reflection|null
  */
 private function reflectNode(Node $node, LocatedSource $locatedSource, Node\Stmt\Namespace_ $namespace = null)
 {
     if ($node instanceof Node\Stmt\ClassLike) {
         return ReflectionClass::createFromNode(new ClassReflector($this->sourceLocator), $node, $locatedSource, $namespace);
     }
     if ($node instanceof Node\Stmt\Function_) {
         return ReflectionFunction::createFromNode(new FunctionReflector($this->sourceLocator), $node, $locatedSource, $namespace);
     }
     return null;
 }
Example #7
0
 /**
  * @param Node $node
  * @param LocatedSource $locatedSource
  * @param Node\Stmt\Namespace_|null $namespace
  * @return Reflection|null
  */
 private function reflectNode(Node $node, LocatedSource $locatedSource, Node\Stmt\Namespace_ $namespace = null)
 {
     if ($node instanceof Node\Stmt\Class_) {
         return ReflectionClass::createFromNode($node, $locatedSource, $namespace);
     }
     if ($node instanceof Node\Stmt\Function_) {
         return ReflectionFunction::createFromNode($node, $locatedSource, $namespace);
     }
     return null;
 }
Example #8
0
 /**
  * createFromPlaceholder extracts the methods of a placeholder that can be called from a template.
  *
  * Methods must be public and take a Call object as the first argument.
  *
  * @return \nochso\WriteMe\Reflection\Method[]
  */
 public function createFromPlaceholder(Placeholder $placeholder)
 {
     $methods = [];
     $class = Reflection\ReflectionClass::createFromInstance($placeholder);
     foreach ($class->getMethods() as $method) {
         if ($this->isCallable($method)) {
             $methods[] = new Method($placeholder, $method);
         }
     }
     return $methods;
 }
 /**
  * @dataProvider internalSymbolsProvider
  *
  * @param string $className
  */
 public function testCanReflectInternalClasses($className)
 {
     /* @var $class */
     $phpInternalSourceLocator = new PhpInternalSourceLocator();
     $reflector = new ClassReflector($phpInternalSourceLocator);
     try {
         $class = $reflector->reflect($className);
     } catch (\ReflectionException $e) {
         if ($phpInternalSourceLocator->hasStub($className)) {
             throw $e;
         }
         $this->markTestIncomplete(sprintf('Can\'t reflect class "%s" due to an internal reflection exception: "%s". Consider adding a stub class', $className, $e->getMessage()));
     }
     $this->assertInstanceOf(ReflectionClass::class, $class);
     $this->assertSame($className, $class->getName());
     $this->assertTrue($class->isInternal());
     $this->assertFalse($class->isUserDefined());
     $internalReflection = new \ReflectionClass($className);
     $this->assertSame($internalReflection->isInterface(), $class->isInterface());
     $this->assertSame($internalReflection->isTrait(), $class->isTrait());
 }
Example #10
0
 public function writemePlaceholderDocs(Call $call)
 {
     $classes = [];
     foreach ($this->placeholders->toArray() as $placeholder) {
         $classes[get_class($placeholder)] = ReflectionClass::createFromInstance($placeholder);
     }
     $template = new TemplateData();
     $template->setHeaderStartLevel($this->options->getValue('placeholder-docs.header-depth'));
     $template->prepare($classes, $this->placeholders);
     $docs = $template->render('full.php');
     $docs = (new Converter())->escape($docs);
     $call->replace($docs);
 }
 /**
  * Create a reflection of a parameter using an instance
  *
  * @param object $instance
  * @param string $methodName
  * @param string $parameterName
  * @return ReflectionParameter
  */
 public static function createFromClassInstanceAndMethod($instance, $methodName, $parameterName)
 {
     return ReflectionClass::createFromInstance($instance)->getMethod($methodName)->getParameter($parameterName);
 }
Example #12
0
 /**
  * Factory depuis une class existante
  *
  * @param $class
  */
 public static function load($class)
 {
     $instance = new static();
     $instance->setClass(ReflectionClass::createFromName($class));
     return $instance;
 }
 /**
  * Create a reflection of a method by it's name using an instance
  *
  * @param object $instance
  * @param string $methodName
  * @return ReflectionMethod
  */
 public static function createFromInstance($instance, $methodName)
 {
     return ReflectionClass::createFromInstance($instance)->getMethod($methodName);
 }
 public function testGetParentClassThrowsExceptionWithNoParent()
 {
     $reflection = ReflectionClass::createFromName('BetterReflectionTest\\Fixture\\ExampleClass');
     $this->assertNull($reflection->getParentClass());
 }
 /**
  * {@inheritdoc}
  */
 public function addProperty($methodName, $visibility = 'public', $static = false)
 {
     return $this->reflectionClass->addProperty($methodName, $visibility, $static);
 }
Example #16
0
<?php

// Load a standard (internal) class
require_once __DIR__ . '/../../vendor/autoload.php';
use BetterReflection\Reflection\ReflectionClass;
$reflection = ReflectionClass::createFromName('stdClass');
var_dump($reflection->getName());
// stdClass
var_dump($reflection->isInternal());
// true
 /**
  * This test loops through the DataProvider (which provides a list of public
  * methods from ReflectionClass), ensures the method exists in ReflectionObject
  * and that when the method is called on ReflectionObject, the method of the
  * same name on ReflectionClass is also called.
  *
  * @param string $methodName
  * @dataProvider reflectionClassMethodProvider
  */
 public function testReflectionObjectOverridesAllMethodsInReflectionClass($methodName)
 {
     // First, ensure the expected method even exists
     $publicObjectMethods = get_class_methods(ReflectionObject::class);
     $this->assertContains($methodName, $publicObjectMethods);
     // Create a mock that will be used to assert that the named method will
     // be called when we call the same method on ReflectionObject
     $mockReflectionClass = $this->getMockBuilder(ReflectionClass::class)->disableOriginalConstructor()->setMethods([$methodName])->getMock();
     $mockReflectionClass->expects($this->atLeastOnce())->method($methodName);
     // Force inject node and locatedSource properties on our ReflectionClass
     // mock so that methods will not fail when they are accessed
     $mockReflectionClassReflection = new \ReflectionClass(ReflectionClass::class);
     $php = '<?php class stdClass {}';
     $mockReflectionClassNodeReflection = $mockReflectionClassReflection->getProperty('locatedSource');
     $mockReflectionClassNodeReflection->setAccessible(true);
     $mockReflectionClassNodeReflection->setValue($mockReflectionClass, new EvaledLocatedSource($php));
     $mockReflectionClassNodeReflection = $mockReflectionClassReflection->getProperty('node');
     $mockReflectionClassNodeReflection->setAccessible(true);
     $mockReflectionClassNodeReflection->setValue($mockReflectionClass, $this->getPhpParser()->parse($php)[0]);
     // Create the ReflectionObject from a dummy class
     $reflectionObject = ReflectionObject::createFromInstance(new \stdClass());
     // Override the reflectionClass property on the ReflectionObject to use
     // the mocked reflectionclass above
     $reflectionObjectReflection = new \ReflectionObject($reflectionObject);
     $reflectionObjectReflectionClassPropertyReflection = $reflectionObjectReflection->getProperty('reflectionClass');
     $reflectionObjectReflectionClassPropertyReflection->setAccessible(true);
     $reflectionObjectReflectionClassPropertyReflection->setValue($reflectionObject, $mockReflectionClass);
     // Finally, call the method name with some dummy parameters. This should
     // ensure that the method of the same name gets called on the
     // $mockReflectionClass mock (as we expect $methodName to be called)
     $reflectionObject->{$methodName}('foo', 'bar', 'baz');
 }
 /**
  * {@inheritDoc}
  */
 public function getShortName()
 {
     return $this->betterReflectionClass->getShortName();
 }
 private function assertSameClassAttributes(\ReflectionClass $original, ReflectionClass $stubbed)
 {
     $this->assertSame($original->getName(), $stubbed->getName());
     $internalParent = $original->getParentClass();
     $betterParent = $stubbed->getParentClass();
     $internalParentName = $internalParent ? $internalParent->getName() : null;
     $betterParentName = $betterParent ? $betterParent->getName() : null;
     $this->assertSame($internalParentName, $betterParentName);
     $originalMethods = $original->getMethods();
     $originalMethodNames = array_map(function (\ReflectionMethod $method) {
         return $method->getName();
     }, $originalMethods);
     $stubbedMethodNames = array_map(function (ReflectionMethod $method) {
         return $method->getName();
     }, $stubbed->getMethods());
     sort($originalMethodNames);
     sort($stubbedMethodNames);
     $this->assertSame($originalMethodNames, $stubbedMethodNames);
     $this->assertEquals($original->getConstants(), $stubbed->getConstants());
     foreach ($originalMethods as $method) {
         $this->assertSameMethodAttributes($method, $stubbed->getMethod($method->getName()));
     }
 }
Example #20
0
 public function formatClass(ReflectionClass $class)
 {
     $ml = Multiline::create($class->getLocatedSource()->getSource());
     return trim($ml[$class->getStartLine() - 1]);
 }
Example #21
0
 /**
  * @param \BetterReflection\Reflection\ReflectionClass $class
  *
  * @return \phpDocumentor\Reflection\DocBlock
  */
 public function getClassDocBlock(ReflectionClass $class)
 {
     return new DocBlock($class->getDocComment());
 }
 public function testStaticCreation()
 {
     $reflection = ReflectionClass::createFromName('BetterReflectionTest\\Fixture\\ExampleClass');
     $this->assertSame('ExampleClass', $reflection->getShortName());
 }
 /**
  * {@inheritdoc}
  */
 public function isIterateable()
 {
     return $this->reflectionClass->isIterateable();
 }
 public function testExportWithNoClassName()
 {
     $this->setExpectedException(\InvalidArgumentException::class);
     ReflectionClass::export();
 }
 /**
  * {@inheritdoc}
  */
 public function getAst()
 {
     return $this->reflectionClass->getAst();
 }
 /**
  * Create a reflection of an instance's property by it's name
  *
  * @param object $instance
  * @param string $propertyName
  * @return ReflectionMethod
  */
 public static function createFromInstance($instance, $propertyName)
 {
     return ReflectionClass::createFromInstance($instance)->getProperty($propertyName);
 }
Example #27
0
 /**
  * @param ReflectionClass $class
  *
  * @return bool
  */
 protected function isInterface(ReflectionClass $class)
 {
     return $class->isInterface() || substr($class->getName(), -strlen('Interface')) === 'Interface';
 }
Example #28
0
<?php

// Load an autoloadable class
require_once __DIR__ . '/../../vendor/autoload.php';
use BetterReflection\Reflection\ReflectionClass;
$reflection = ReflectionClass::createFromName(ReflectionClass::class);
echo $reflection->getName() . "\n";
// ReflectionClass
echo ($reflection->isInternal() === true ? 'internal' : 'not internal') . "\n";
// not internal
 function it_might_implement_an_interface(ReflectionClass $reflectionClass)
 {
     $reflectionClass->implementsInterface('MyInterface')->willReturn(false);
     $this->implementsInterface('MyInterface')->shouldBe(false);
     $reflectionClass->implementsInterface('MyInterface')->willReturn(true);
     $this->implementsInterface('MyInterface')->shouldBe(true);
 }