public function testIfWillGetUseStatements() { $model = new ModelWithUseStatements(); $doc = new DocComment(); $info = new ReflectionObject($model); $fileName = $info->getFileName(); $fileData = $doc->forFile($fileName); $classData = $doc->forClass($info); $expected = [Addendum::class, Annotation::class, Meta::class]; foreach ($expected as $fqn) { $this->assertContains($fqn, $fileData['use']); $this->assertContains($fqn, $classData['use']); } }
/** * (PHP 5)<br/> * Constructs a ReflectionClass from file * @link http://php.net/manual/en/reflectionclass.construct.php * @param string $file <p> * Either a string containing the name of the class to * reflect, or an object. * </p> */ public function __construct($file) { $docExtractor = new DocComment(); $this->_docs = $docExtractor->forFile($file); $this->file = $file; $this->methods = $this->_docs['methods']; $this->fields = $this->_docs['fields']; $this->namespace = $this->_docs['namespace']; $this->shortName = $this->_docs['className']; if (empty($this->shortName)) { throw new UnexpectedValueException(sprintf("Could not find any class in file `%s`", $file)); } if (is_array($this->shortName)) { throw new UnexpectedValueException(sprintf("`%s` does not support multiple classes. Found in file `%s`", __CLASS__, $file)); } $this->name = $this->namespace . '\\' . $this->shortName; }
/** * TODO This should not be static * @param Reflector $reflection * @return mixed[] */ public static function getDocComment(Reflector $reflection) { // NOTE: Due to a nature of traits, raw doc parsing is always needed // When using reflection's method `getDocComment` it will return // doc comment like if it was pasted in code. Thus use statement // from traits would be omited. See https://github.com/Maslosoft/Addendum/issues/24 $docComment = new DocComment(); if ($reflection instanceof ReflectionClass) { $commentString = $docComment->get($reflection)['class']; } else { $commentString = $docComment->get($reflection); } return $commentString; }
/** * Annotate file without including php file and without using reflection. * This method returns raw annotation values. * <i>This is intented for various builders, which should not include files.</i> * This <b>ALWAYS</b> parses file. * @param string $file * @param string $className <b>NOT RECOMMENDED!</b> Optional class name if multiple classes are declared in one file * @return mixed[][] */ public static function rawAnnotate($file, $className = null) { $docExtractor = new DocComment(); $docs = $docExtractor->forFile($file, $className); $matcher = new AnnotationsMatcher(); $class = []; $matcher->setPlugins(new MatcherConfig(['addendum' => new Addendum(), 'reflection' => new ReflectionFile($file)])); $matcher->matches($docs['class'], $class); $methods = []; foreach ((array) $docs['methods'] as $name => $doc) { $methods[$name] = []; $matcher->matches($doc, $methods[$name]); } $fields = []; foreach ((array) $docs['fields'] as $name => $doc) { $fields[$name] = []; $matcher->matches($doc, $fields[$name]); } $result = ['namespace' => $docs['namespace'], 'className' => $docs['className'], 'class' => $class, 'methods' => $methods, 'fields' => $fields]; return $result; }