Пример #1
0
 public function testTraits()
 {
     $reflection = new \ReflectionClass(StandardQuery::class);
     $this->assertInstanceOf(Query::class, $reflection->newInstance());
     $this->assertContains(LimitQueryTrait::class, $reflection->getTraitNames());
     $this->assertContains(OffsetQueryTrait::class, $reflection->getTraitNames());
     $this->assertContains(SortingQueryTrait::class, $reflection->getTraitNames());
 }
Пример #2
0
 /**
  * クラスにプラグインをセットする
  * @param object $o
  * @param string $n
  */
 public static function set_class_plugin($o, $n = null)
 {
     if (is_array($o)) {
         foreach ($o as $c => $plugins) {
             $r = new \ReflectionClass('\\' . str_replace('.', '\\', $c));
             if (in_array(__CLASS__, $r->getTraitNames())) {
                 foreach ($plugins as $p) {
                     call_user_func_array([$r->getName(), 'set_class_plugin'], [$p]);
                 }
             }
         }
     } else {
         $g = get_called_class();
         if (is_string($o) && class_exists($c = '\\' . str_replace('.', '\\', $o))) {
             $o = new $c();
         }
         $t = (is_object($o) ? 1 : 0) + (is_callable($o) ? 2 : 0);
         if ($t === 1) {
             self::$_plug_funcs[$g][] = $o;
         } else {
             if ($t === 3 && !empty($n)) {
                 self::$_plug_funcs[$g][] = [$o, (string) $n];
             }
         }
     }
 }
Пример #3
0
 /**
  * Register plugins
  */
 public function register($path)
 {
     if (!file_exists($path) && !is_dir($path)) {
         throw new ExtensionException(sprintf('path %s not exists', $path));
     }
     $Cyan = \Cyan::initialize();
     $App = $Cyan->getContainer('application');
     $plugin_manager = $App->getContainer('factory_plugin');
     $plugin_types = glob($path . '/*', GLOB_ONLYDIR);
     foreach ($plugin_types as $plugin_type) {
         $plugin_paths = glob($plugin_type . '/*', GLOB_ONLYDIR);
         foreach ($plugin_paths as $plugin_path) {
             $class_name = sprintf('Plugin%s%s', ucfirst(strtolower(basename($plugin_type))), ucfirst(strtolower(basename($plugin_path))));
             $file_path = $plugin_path . DIRECTORY_SEPARATOR . basename($plugin_path) . '.php';
             if (file_exists($file_path)) {
                 $plugin_callback = (require_once $file_path);
                 if (is_callable($plugin_callback)) {
                     $type = basename($plugin_type);
                     $name = basename($plugin_path);
                     $plugin_manager->create($type, $name, $plugin_callback);
                 } elseif (class_exists($class_name)) {
                     $type = basename($plugin_type);
                     $name = basename($plugin_path);
                     $reflection_class = new ReflectionClass($class_name);
                     if (!in_array('Cyan\\Framework\\TraitSingleton', $reflection_class->getTraitNames())) {
                         throw new FactoryException(sprintf('%s class must use Cyan\\Trait\\Singleton', $class_name));
                     }
                     unset($reflection_class);
                     $plugin_manager->create($type, $name, $class_name::getInstance());
                 }
             }
         }
     }
 }
 /**
  * @testdox Uses the ListenerAggregateTrait from the Zend Framework
  */
 public function testUsesZfListenerAggregateTrait()
 {
     $reflection = new \ReflectionClass('\\Core\\EventManager\\ListenerAggregateTrait');
     $traits = $reflection->getTraitNames();
     $this->assertTrue($reflection->isTrait());
     $this->assertEquals(['Zend\\EventManager\\ListenerAggregateTrait'], $traits);
 }
Пример #5
0
 /** @coversNothing */
 public function test_traits()
 {
     $subjectReflection = new \ReflectionClass(testSubject::class);
     $subjectTraits = $subjectReflection->getTraitNames();
     $allTraits = getAllTraits($subjectReflection);
     self::assertContains(P\RootTypeTrait::class, $subjectTraits);
     self::assertContains(P\ClosedTrait::class, $allTraits);
 }
Пример #6
0
 /**
  * Check if the specified object has the `Collectable` trait.
  *
  * @param mixed $object
  *
  * @return bool
  */
 public function isCollectible($object)
 {
     $reflector = new \ReflectionClass($object);
     $filtered = array_filter($reflector->getTraitNames(), function ($trait) {
         return array_reverse(explode('\\', $trait))[0] == 'Collectable';
     });
     return count($filtered) ? true : false;
 }
 /**
  * Checks whether provided entity is supported.
  *
  * @param \ReflectionClass $reflClass
  *
  * @return bool
  */
 private function isEntitySupported(\ReflectionClass $reflClass)
 {
     $traitNames = [];
     while ($reflClass) {
         $traitNames = array_merge($traitNames, $reflClass->getTraitNames());
         $reflClass = $reflClass->getParentClass();
     }
     return in_array('Unifik\\DoctrineBehaviorsBundle\\Model\\SoftDeletable\\SoftDeletable', $traitNames);
 }
Пример #8
0
 /**
  * get all trait names
  *
  * @return array
  */
 public function getTraitNames()
 {
     $traits = parent::getTraitNames();
     if ($this->getParentClass()) {
         $parent = $this->getParentClass();
         $traits = array_merge($traits, $this->getParentClassTraits($this->getParentClass()));
     }
     return $traits;
 }
 private function getTraitNames(\ReflectionClass $class) : array
 {
     $traitNames = $class->getTraitNames();
     while ($class->getParentClass() !== false) {
         $traitNames = array_values(array_unique(array_merge($traitNames, $class->getParentClass()->getTraitNames())));
         $class = $class->getParentClass();
     }
     return $traitNames;
 }
Пример #10
0
 /**
  *
  * @param \ReflectionClass $reflClass
  * @param type $traitName
  * @param type $isRecursive
  * @return boolean
  */
 protected function hasTrait(\ReflectionClass $reflClass, $traitName, $isRecursive = false)
 {
     if (in_array($traitName, $reflClass->getTraitNames())) {
         return true;
     }
     $parentClass = $reflClass->getParentClass();
     if (false === $isRecursive || false === $parentClass || null === $parentClass) {
         return false;
     }
     return $this->hasTrait($parentClass, $traitName, $isRecursive);
 }
 /**
  * Check if the entity is a Translatable entity
  *
  * @param $entity
  *
  * @return bool
  */
 protected function isTranslatable($entity)
 {
     if (!is_object($entity)) {
         return false;
     }
     // Support Doctrine Proxies
     $realClass = ClassUtils::getRealClass($entity);
     $reflClass = new \ReflectionClass($realClass);
     $traitNames = $reflClass->getTraitNames();
     return in_array('Unifik\\DoctrineBehaviorsBundle\\Model\\Translatable\\Translatable', $traitNames) && $reflClass->hasProperty('translations');
 }
 protected function traitsUsedRecursive($class, $traitNames = [])
 {
     if (!$class instanceof ReflectionClass) {
         $class = new ReflectionClass($class);
     }
     $traitNames = array_merge($traitNames, $class->getTraitNames());
     if ($class->getParentClass() != false) {
         return array_merge($traitNames, $this->traitsUsedRecursive($class->getParentClass()));
     }
     return $traitNames;
 }
Пример #13
0
 protected function extractModule($module)
 {
     $Iterator = new \RecursiveIteratorIterator($iterator = new \RecursiveDirectoryIterator($module));
     $Regex = new \RegexIterator($Iterator, '/^.+\\.php$/i', \RecursiveRegexIterator::GET_MATCH);
     foreach ($Regex as $file => $vv) {
         $this->currentService = null;
         $this->currentSecurity = null;
         $this->currentMethod = null;
         $this->currentClass = $this->resolveClassName($file);
         $this->currentPrefix = '';
         try {
             $r = new \ReflectionClass($this->currentClass);
         } catch (\ReflectionException $e) {
             var_dump($e->getMessage());
             continue;
         }
         foreach ($this->parseAnnotations($r->getDocComment()) as $annotation) {
             $method = substr($annotation, 0, strpos($annotation, '('));
             if (method_exists($this, $method)) {
                 eval('$this->' . $annotation . ';');
             }
         }
         if ($r->isSubclassOf('eBuildy\\Container\\ContainerAware') || in_array('eBuildy\\Container\\ContainerAwareTrait', $r->getTraitNames()) || $r->hasProperty('container')) {
             $this->services[$this->currentService]['containerAware'] = true;
         }
         $methods = $r->getMethods(\ReflectionMethod::IS_PUBLIC);
         foreach ($methods as $method) {
             if ($this->currentClass === $method->getDeclaringClass()->getName()) {
                 $this->currentMethod = $method->getName();
                 foreach ($this->parseAnnotations($method->getDocComment()) as $annotation) {
                     if (strpos($annotation, '(') !== false) {
                         $annotationMethod = substr($annotation, 0, strpos($annotation, '('));
                         if (strpos($annotationMethod, ' ') === false && method_exists($this, $annotationMethod)) {
                             eval('$this->' . $annotation . ';');
                         }
                     }
                 }
             }
         }
         $properties = $r->getProperties(\ReflectionMethod::IS_PUBLIC);
         foreach ($properties as $property) {
             $this->currentProperty = $property->getName();
             foreach ($this->parseAnnotations($property->getDocComment()) as $annotation) {
                 $annotationMethod = substr($annotation, 0, strpos($annotation, '('));
                 if (method_exists($this, $annotationMethod)) {
                     eval('$this->' . $annotation . ';');
                 }
             }
         }
     }
 }
Пример #14
0
 /**
  * Return TRUE if the given object use the given trait, FALSE if not
  * Extends from ClassAnalyzer::hasTrait to check in ancestors traits as well
  * 
  * @param \ReflectionClass $class
  * @param string $traitName
  * @param boolean $isRecursive
  */
 public function hasTrait(\ReflectionClass $class, $traitName, $isRecursive = false)
 {
     $classTraits = $class->getTraitNames();
     // Trait directly present in final class
     if (in_array($traitName, $classTraits)) {
         return true;
     }
     // Check in parents traits
     foreach ($classTraits as $classTrait) {
         $traitObject = new \ReflectionClass($classTrait);
         if ($this->hasTrait($traitObject, $traitName, $isRecursive)) {
             return true;
         }
     }
     // Check in parents classes
     $parentClass = $class->getParentClass();
     if (false === $isRecursive || false === $parentClass || null === $parentClass) {
         return false;
     }
     return $this->hasTrait($parentClass, $traitName, $isRecursive);
 }
Пример #15
0
 /**
  * @param $type
  * @param TraitsEvent $target_object
  */
 public function assign($type, $target_object)
 {
     $type = strtolower($type);
     $required = __NAMESPACE__ . '\\TraitEvent';
     $reflection_class = new ReflectionClass($target_object);
     $parent = $reflection_class->getParentClass();
     $use = $reflection_class->getTraitNames();
     unset($reflection_class);
     if ($parent) {
         $use = array_merge($use, $parent->getTraitNames());
     }
     if (!in_array($required, $use)) {
         throw new FactoryException(sprintf('Cant assign %s Plugins to %s %s, because they are not use TraitEvent.', $type, get_class($target_object), gettype($target_object)));
     }
     if (!isset($this->factory_registry[$type]) || empty($this->factory_registry[$type])) {
         return;
     }
     foreach ($this->factory_registry[$type] as $plugin_name => $plugin) {
         $target_object->registerEventPlugin($plugin_name, $plugin);
     }
 }
 /**
  * {@inheritDoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $name = strtr($input->getArgument('name'), '/', '\\');
     if (false !== ($pos = strpos($name, ':'))) {
         $name = $this->getContainer()->get('doctrine')->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1);
     }
     if (class_exists($name)) {
         $output->writeln(sprintf('Updating search index for entity "<info>%s</info>"', $name));
     } else {
         throw new \RuntimeException(sprintf('%s class doesn\'t exist.', $name));
     }
     // Check if the entity has the Searchable trait
     $reflector = new \ReflectionClass($name);
     $traits = $reflector->getTraitNames();
     if (!in_array('Librinfo\\DoctrineBundle\\Entity\\Traits\\Searchable', $traits)) {
         throw new \RuntimeException(sprintf('%s class doesn\'t have the Searchable trait.', $reflector->getName()));
     }
     $em = $this->getContainer()->get('doctrine')->getEntityManager();
     $metadata = $em->getClassMetadata($name);
     $repo = new SearchableRepository($em, $metadata);
     $repo->batchUpdate();
     $output->writeln('DONE');
 }
Пример #17
0
 /**
  * @param $type
  * @throws ExtensionException
  */
 public static function get($type)
 {
     $type = strtolower($type);
     if ($file = FilesystemPath::find(self::addIncludePath(), $type . DIRECTORY_SEPARATOR . $type . '.php')) {
         require_once $file;
         $class_name = __NAMESPACE__ . '\\ExtensionType' . ucfirst($type);
         if (!class_exists($class_name)) {
             throw new ExtensionException(sprintf('Extension type "%s" not found!', ucfirst($type)));
         }
         $required_traits = ['Cyan\\Framework\\TraitSingleton'];
         $reflection_class = new ReflectionClass($class_name);
         foreach ($required_traits as $required_trait) {
             if (!in_array($required_trait, $reflection_class->getTraitNames())) {
                 throw new ExtensionException(sprintf('%s class must use %s', $class_name, $required_trait));
             }
         }
         if (!is_callable([$class_name, 'register'])) {
             throw new ExtensionException(sprintf('%s class must implement register method', $class_name));
         }
         return $class_name::getInstance();
     }
     throw new ExtensionException(sprintf('Extension "%s" not found!', ucfirst($type)));
 }
Пример #18
0
 protected function loadClassInfo(\ReflectionClass $reflectedObject)
 {
     $this->classFile = $reflectedObject->getFileName();
     if (empty($this->classFile)) {
         $this->classFile = self::CLASSINFO_BUILTIN;
     }
     $this->classInterfaces = implode(', ', $reflectedObject->getInterfaceNames());
     $this->classNamespace = $reflectedObject->getNamespaceName();
     $parent = $reflectedObject->getParentClass();
     if ($parent) {
         $this->classParent = $parent->getName();
     }
     if (version_compare(phpversion(), '5.4', '>=')) {
         $this->classTraits = implode(', ', $reflectedObject->getTraitNames());
     }
     $this->abstract = $reflectedObject->isAbstract();
     $this->final = $reflectedObject->isFinal();
 }
 /**
  * Checks whether provided entity is supported.
  *
  * @param \ReflectionClass $reflClass
  *
  * @return bool
  */
 protected function isEntitySupported(\ReflectionClass $reflClass)
 {
     $traitNames = [];
     $originalReflClass = $reflClass;
     while ($reflClass) {
         $traitNames = array_merge($traitNames, $reflClass->getTraitNames());
         $reflClass = $reflClass->getParentClass();
     }
     return in_array('Unifik\\DoctrineBehaviorsBundle\\Model\\Sluggable\\Sluggable', $traitNames) && (!in_array($originalReflClass->name, $this->excludedEntities) && !$this->entityName || $originalReflClass->name == $this->entityName);
 }
Пример #20
0
 /**
  * @param CodeBase $code_base
  * A reference to the entire code base in which this
  * context exists
  *
  * @param ReflectionClass $class
  * A reflection class representing a builtin class.
  *
  * @return Clazz
  * A Class structural element representing the given named
  * builtin.
  */
 public static function fromReflectionClass(CodeBase $code_base, \ReflectionClass $class) : Clazz
 {
     // Build a set of flags based on the constitution
     // of the built-in class
     $flags = 0;
     if ($class->isFinal()) {
         $flags = \ast\flags\CLASS_FINAL;
     } else {
         if ($class->isInterface()) {
             $flags = \ast\flags\CLASS_INTERFACE;
         } else {
             if ($class->isTrait()) {
                 $flags = \ast\flags\CLASS_TRAIT;
             }
         }
     }
     if ($class->isAbstract()) {
         $flags |= \ast\flags\CLASS_ABSTRACT;
     }
     $context = new Context();
     // Build a base class element
     $clazz = new Clazz($context, $class->getName(), UnionType::fromStringInContext($class->getName(), $context), $flags);
     // If this class has a parent class, add it to the
     // class info
     if ($parent_class = $class->getParentClass()) {
         $parent_class_fqsen = FullyQualifiedClassName::fromFullyQualifiedString('\\' . $parent_class->getName());
         $clazz->setParentClassFQSEN($parent_class_fqsen);
     }
     foreach ($class->getDefaultProperties() as $name => $value) {
         // TODO: whats going on here?
         $reflection_property = new \ReflectionProperty($class->getName(), $name);
         $property = new Property($context->withClassFQSEN($clazz->getFQSEN()), $name, Type::fromObject($value)->asUnionType(), 0);
         $clazz->addProperty($code_base, $property);
     }
     foreach ($class->getInterfaceNames() as $name) {
         $clazz->addInterfaceClassFQSEN(FullyQualifiedClassName::fromFullyQualifiedString('\\' . $name));
     }
     foreach ($class->getTraitNames() as $name) {
         $clazz->addTraitFQSEN(FullyQualifiedClassName::fromFullyQualifiedString('\\' . $name));
     }
     foreach ($class->getConstants() as $name => $value) {
         $clazz->addConstant($code_base, new Constant($context, $name, Type::fromObject($value)->asUnionType(), 0));
     }
     foreach ($class->getMethods() as $reflection_method) {
         $method_list = Method::methodListFromReflectionClassAndMethod($context->withClassFQSEN($clazz->getFQSEN()), $code_base, $class, $reflection_method);
         foreach ($method_list as $method) {
             $clazz->addMethod($code_base, $method);
         }
     }
     return $clazz;
 }
Пример #21
0
 /**
  * 与えられたクラスのtraitを全て返します
  * @param string $class
  * @return array
  */
 public static function get_class_traits($class)
 {
     $ref = new \ReflectionClass($class);
     $traits = [];
     while (true) {
         $traits = array_merge($traits, $ref->getTraitNames());
         if (($ref = $ref->getParentClass()) === false) {
             break;
         }
     }
     return array_unique($traits);
 }
 /**
  * @param \ReflectionClass $class
  * @param                  $traitName
  *
  * @return bool
  */
 protected function hasTrait(\ReflectionClass $class, $traitName)
 {
     if (in_array($traitName, $class->getTraitNames())) {
         return true;
     }
     if (!($parentClass = $class->getParentClass())) {
         return false;
     }
     return $this->hasTrait($parentClass, $traitName);
 }
 /**
  * Checks if entity supports Blameable
  *
  * @param ClassMetadata $classMetadata
  * @param bool          $isRecursive   true to check for parent classes until trait is found
  *
  * @return boolean
  */
 private function isEntitySupported(\ReflectionClass $reflClass, $isRecursive = false)
 {
     $isSupported = in_array('Knp\\DoctrineBehaviors\\Model\\Blameable\\Blameable', $reflClass->getTraitNames());
     while ($isRecursive and !$isSupported and $reflClass->getParentClass()) {
         $reflClass = $reflClass->getParentClass();
         $isSupported = $this->isEntitySupported($reflClass, true);
     }
     return $isSupported;
 }
Пример #24
0
 public static function _isNotProxyTrait($proxy)
 {
     $r = new \ReflectionClass($proxy);
     self::assertTrue(empty(array_intersect([ProxyTrait::class], $r->getTraitNames())));
 }
 /**
  * Checks if entity supports Geocodable
  *
  * @param  ClassMetadata $classMetadata
  * @return boolean
  */
 private function isEntitySupported(\ReflectionClass $reflClass)
 {
     return in_array('Knp\\DoctrineBehaviors\\Model\\Geocodable\\Geocodable', $reflClass->getTraitNames());
 }
 /**
  * Returns names of used traits.
  *
  * @return array
  */
 public function getTraitNames()
 {
     return NATIVE_TRAITS ? parent::getTraitNames() : array();
 }
Пример #27
0
 /**
  * Record Model files.
  */
 protected function recordModels()
 {
     //record main model (ONLY ONCE)
     if (!file_exists($this->path . '/' . $this->config_data['name'] . '.php')) {
         @mkdir($this->path);
         $stub = file_get_contents(__DIR__ . '/../views/stubs/model.stub');
         $stub = str_replace('[NAMESPACE]', $this->namespace, $stub);
         $stub = str_replace('[MODEL]', $this->config_data['name'], $stub);
         file_put_contents($this->path . '/' . $this->config_data['name'] . '.php', $stub);
     } else {
         if (count($this->useTraits)) {
             $insertTraits = [];
             $model = CrudModel::createInstance($this->config_data['name']);
             $reflection = new \ReflectionClass($model);
             $traits = $reflection->getTraitNames();
             foreach ($this->useTraits as $trait) {
                 $trait = ltrim($this->traits[$trait], '\\');
                 if (!in_array($trait, $traits)) {
                     $insertTraits[] = '    use \\' . $trait . ';';
                 }
             }
             if (count($insertTraits)) {
                 $fileCts = file_get_contents($this->path . '/' . $this->config_data['name'] . '.php');
                 if (preg_match('#.*(class.+' . $this->config_data['name'] . ".*\\{)#siUm", $fileCts, $matches) && !empty($matches[1])) {
                     $fileCts = str_replace($matches[1], $matches[1] . "\n" . implode("\n", $insertTraits), $fileCts);
                     file_put_contents($this->path . '/' . $this->config_data['name'] . '.php', $fileCts);
                 } else {
                     $this->errors[] = 'Unable to record traits.<br>Probably the model file is corrupt.<br>Please check the model file and add the following traits manually:<br><br>' . implode('<br>', $insertTraits);
                 }
             }
         }
     }
 }
Пример #28
0
 /**
  * @param CodeBase $code_base
  * A reference to the entire code base in which this
  * context exists
  *
  * @param ReflectionClass $class
  * A reflection class representing a builtin class.
  *
  * @return Clazz
  * A Class structural element representing the given named
  * builtin.
  */
 public static function fromReflectionClass(CodeBase $code_base, \ReflectionClass $class) : Clazz
 {
     // Build a set of flags based on the constitution
     // of the built-in class
     $flags = 0;
     if ($class->isFinal()) {
         $flags = \ast\flags\CLASS_FINAL;
     } elseif ($class->isInterface()) {
         $flags = \ast\flags\CLASS_INTERFACE;
     } elseif ($class->isTrait()) {
         $flags = \ast\flags\CLASS_TRAIT;
     }
     if ($class->isAbstract()) {
         $flags |= \ast\flags\CLASS_ABSTRACT;
     }
     $context = new Context();
     $class_fqsen = FullyQualifiedClassName::fromStringInContext($class->getName(), $context);
     // Build a base class element
     $clazz = new Clazz($context, $class->getName(), UnionType::fromStringInContext($class->getName(), $context), $flags, $class_fqsen);
     // If this class has a parent class, add it to the
     // class info
     if ($parent_class = $class->getParentClass()) {
         $parent_class_fqsen = FullyQualifiedClassName::fromFullyQualifiedString('\\' . $parent_class->getName());
         $parent_type = $parent_class_fqsen->asType();
         $clazz->setParentType($parent_type);
     }
     // n.b.: public properties on internal classes don't get
     //       listed via reflection until they're set unless
     //       they have a default value. Therefore, we don't
     //       bother iterating over `$class->getProperties()`
     //       `$class->getStaticProperties()`.
     foreach ($class->getDefaultProperties() as $name => $value) {
         $property_context = $context->withScope(new ClassScope(new GlobalScope(), $clazz->getFQSEN()));
         $property_fqsen = FullyQualifiedPropertyName::make($clazz->getFQSEN(), $name);
         $property = new Property($property_context, $name, Type::fromObject($value)->asUnionType(), 0, $property_fqsen);
         $clazz->addProperty($code_base, $property, new None());
     }
     foreach (UnionType::internalPropertyMapForClassName($clazz->getName()) as $property_name => $property_type_string) {
         $property_context = $context->withScope(new ClassScope(new GlobalScope(), $clazz->getFQSEN()));
         $property_type = UnionType::fromStringInContext($property_type_string, new Context());
         $property_fqsen = FullyQualifiedPropertyName::make($clazz->getFQSEN(), $property_name);
         $property = new Property($property_context, $property_name, $property_type, 0, $property_fqsen);
         $clazz->addProperty($code_base, $property, new None());
     }
     foreach ($class->getInterfaceNames() as $name) {
         $clazz->addInterfaceClassFQSEN(FullyQualifiedClassName::fromFullyQualifiedString('\\' . $name));
     }
     foreach ($class->getTraitNames() as $name) {
         $clazz->addTraitFQSEN(FullyQualifiedClassName::fromFullyQualifiedString('\\' . $name));
     }
     foreach ($class->getConstants() as $name => $value) {
         $constant_fqsen = FullyQualifiedClassConstantName::make($clazz->getFQSEN(), $name);
         $constant = new ClassConstant($context, $name, Type::fromObject($value)->asUnionType(), 0, $constant_fqsen);
         $clazz->addConstant($code_base, $constant);
     }
     foreach ($class->getMethods() as $reflection_method) {
         $method_context = $context->withScope(new ClassScope(new GlobalScope(), $clazz->getFQSEN()));
         $method_list = FunctionFactory::methodListFromReflectionClassAndMethod($method_context, $code_base, $class, $reflection_method);
         foreach ($method_list as $method) {
             $clazz->addMethod($code_base, $method, new None());
         }
     }
     return $clazz;
 }
Пример #29
0
function add_class($class_name)
{
    global $classes, $internal_arginfo;
    $lc = strtolower($class_name);
    $class = new \ReflectionClass($class_name);
    $flags = 0;
    if ($class->isFinal()) {
        $flags = \ast\flags\CLASS_FINAL;
    } else {
        if ($class->isInterface()) {
            $flags = \ast\flags\CLASS_INTERFACE;
        } else {
            if ($class->isTrait()) {
                $flags = \ast\flags\CLASS_TRAIT;
            }
        }
    }
    if ($class->isAbstract()) {
        $flags |= \ast\flags\CLASS_ABSTRACT;
    }
    $classes[$lc] = ['file' => 'internal', 'namespace' => $class->getNamespaceName(), 'conditional' => false, 'flags' => $flags, 'lineno' => 0, 'endLineno' => 0, 'name' => $class_name, 'docComment' => '', 'type' => '', 'traits' => []];
    foreach ($class->getDefaultProperties() as $name => $value) {
        $prop = new \ReflectionProperty($class_name, $name);
        $classes[$lc]['properties'][strtolower($name)] = ['flags' => $prop->getModifiers(), 'name' => $name, 'lineno' => 0, 'value' => $value];
    }
    $classes[$lc]['interfaces'] = $class->getInterfaceNames();
    $classes[$lc]['traits'] = $class->getTraitNames();
    $parents = [];
    $parent = $class->getParentClass();
    if ($parent) {
        $temp = $class;
        while ($parent = $temp->getParentClass()) {
            $parents[] = $parent->getName();
            $parents = array_merge($parents, $parent->getInterfaceNames());
            $temp = $parent;
        }
    }
    $types = [$class_name];
    $types = array_merge($types, $classes[$lc]['interfaces']);
    $types = array_merge($types, $parents);
    $classes[$lc]['type'] = implode('|', array_unique($types));
    foreach ($class->getConstants() as $name => $value) {
        $classes[$lc]['constants'][strtolower($name)] = ['name' => $name, 'lineno' => 0, 'value' => $value];
    }
    foreach ($class->getMethods() as $method) {
        $meth = new \ReflectionMethod($class_name, $method->name);
        $required = $meth->getNumberOfRequiredParameters();
        $optional = $meth->getNumberOfParameters() - $required;
        $lmname = strtolower($method->name);
        $classes[$lc]['methods'][$lmname] = ['file' => 'internal', 'namespace' => $class->getNamespaceName(), 'conditional' => false, 'flags' => $meth->getModifiers(), 'lineno' => 0, 'endLineno' => 0, 'name' => $method->name, 'docComment' => '', 'required' => $required, 'optional' => $optional, 'ret' => null];
        $arginfo = null;
        if (!empty($internal_arginfo["{$class_name}::{$method->name}"])) {
            $arginfo = $internal_arginfo["{$class_name}::{$method->name}"];
            $classes[$lc]['methods'][$lmname]['ret'] = $arginfo[0];
        }
        foreach ($method->getParameters() as $param) {
            $flags = 0;
            if ($param->isPassedByReference()) {
                $flags |= \ast\flags\PARAM_REF;
            }
            if ($param->isVariadic()) {
                $flags |= \ast\flags\PARAM_VARIADIC;
            }
            $classes[$lc]['methods'][strtolower($method->name)]['params'][] = ['file' => 'internal', 'flags' => $flags, 'lineno' => 0, 'name' => $param->name, 'type' => empty($arginfo) ? null : next($arginfo), 'def' => null];
        }
    }
}
Пример #30
0
 /**
  * @param \ReflectionClass $reflectionClass
  *
  * @return string
  */
 protected function documentClassSignature(\ReflectionClass $reflectionClass)
 {
     if ($this->processClassSignature === false) {
         return "";
     }
     $signature = "## {$reflectionClass->name}\n\n";
     if ($parent = $reflectionClass->getParentClass()) {
         $signature .= "* *Extends* `{$parent->name}`";
     }
     $interfaces = $reflectionClass->getInterfaceNames();
     if (count($interfaces)) {
         $signature .= "\n* *Implements* `" . implode('`, `', $interfaces) . '`';
     }
     $traits = $reflectionClass->getTraitNames();
     if (count($traits)) {
         $signature .= "\n* *Uses* `" . implode('`, `', $traits) . '`';
     }
     if (is_callable($this->processClassSignature)) {
         $signature = call_user_func($this->processClassSignature, $reflectionClass, $signature);
     }
     return $signature;
 }