Skip to content

Commit e53a3cd

Browse files
committed
removed FQCN passing to parser
Removed parser argument FQCN and added Uses instead. Splitted Index and Scope processing into two traversing. Added one level @inheritdoc parsing
1 parent 2c4f0fc commit e53a3cd

File tree

11 files changed

+72
-38
lines changed

11 files changed

+72
-38
lines changed

src/Complete/CompleteEngine.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,25 @@ protected function processFileContent(Project $project, $lines, $line, $file){
129129
$this->indexProcessor->clearResultNodes();
130130
$parser = $this->parser;
131131
$parser->addProcessor($this->indexProcessor);
132-
$nodes = $parser->parseContent($fqcn, $file, $content);
132+
$nodes = $parser->parseContent($file, $content);
133133
$this->generator->processFileNodes(
134134
$project->getIndex(),
135135
$nodes
136136
);
137+
/** @var \Entity\Node\Uses */
138+
$uses = $parser->getUses();
137139
$this->scopeProcessor->setIndex($project->getIndex());
138140
$this->scopeProcessor->setLine($line);
139141
$this->scopeProcessor->clearResultNodes();
140142
$parser->addProcessor($this->scopeProcessor);
141-
$scopeNodes = $parser->parseContent($fqcn, $file, $content);
143+
$scopeNodes = $parser->parseContent($file, $content, $uses);
142144
$contentHash = hash('sha1', $content);
143145
$this->cachePool[$file] = [$contentHash, $nodes, $scopeNodes];
144146
}
145-
return $scopeNodes[0];
147+
if(count($scopeNodes)){
148+
return $scopeNodes[0];
149+
}
150+
return null;
146151
}
147152

148153
private function isValidCache($file, $content){
@@ -151,7 +156,9 @@ private function isValidCache($file, $content){
151156
return $hash === $contentHash;
152157
}
153158

159+
/** @var Parser */
154160
private $parser;
161+
/** @property IndexGenerator */
155162
private $generator;
156163
private $contextResolver;
157164
private $completerFactory;

src/Entity/FQCN.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public function __construct($className, $namespace = "", $isArray=false){
3535
}
3636
$this->addPart($className);
3737
}
38+
39+
/**
40+
* @inheritdoc
41+
*/
3842
public function join(FQN $join){
3943
$result = new self($join->getLast());
4044
$resultParts = $this->getParts();

src/Entity/FQN.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public function __construct($namespace = ""){
2020
$this->parts = [];
2121
}
2222
}
23+
24+
/**
25+
* Joins FQN to the clone of the current FQN and returns it
26+
*
27+
* @return FQN
28+
*/
2329
public function join(FQN $join){
2430
$result = new self();
2531
$resultParts = $this->getParts();

src/Entity/Node/ClassData.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ public function getName(){
5353
}
5454
public function setParent($parent){
5555
$this->parent = $parent;
56+
if($parent instanceof ClassData){
57+
foreach($this->methods->all() as $method){
58+
if($method->doc === Comment::INHERIT_MARK){
59+
$parentMethod = $parent->methods->get($method->name);
60+
if($parentMethod instanceof MethodData){
61+
$method->doc = $parentMethod->doc;
62+
$method->setReturn($parentMethod->getReturn());
63+
}
64+
}
65+
}
66+
}
5667
}
5768
public function addInterface($interface){
5869
$fqcn = $interface instanceof InterfaceData ? $interface->fqcn : $interface;

src/Entity/Node/Comment.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
use Entity\Node\Variable;
88

99
class Comment {
10+
const INHERIT_MARK = 'inheritdoc';
1011

1112
public function __construct($doc){
1213
$this->doc = $doc;
1314
}
1415
public function addVar(Variable $var){
1516
$this->vars[$var->getName()] = $var;
16-
1717
}
1818
public function addProperty(ClassProperty $prop){
1919
$this->properties[$prop->name] = $prop;
@@ -55,7 +55,7 @@ public function getVar($name){
5555
if(array_key_exists($name, $this->vars)){
5656
$var = $this->vars[$name];
5757
}
58-
if($var instanceof Variable && array_key_exists('', $this->vars)){
58+
if(!$var instanceof Variable && array_key_exists('', $this->vars)){
5959
$var = $this->vars[''];
6060
}
6161
return $var;
@@ -69,10 +69,17 @@ public function getReturn(){
6969
public function getDoc(){
7070
return $this->doc;
7171
}
72+
public function markInheritDoc(){
73+
$this->inheritDoc = true;
74+
}
75+
public function isInheritDoc(){
76+
return $this->inheritDoc;
77+
}
7278

7379
private $return;
7480
private $doc = "";
7581
private $vars = [];
7682
private $throws = [];
7783
private $properties = [];
84+
private $inheritDoc = false;
7885
}

src/Entity/Node/MethodData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getReturnStr(){
5353
public function getReturn(){
5454
return $this->return;
5555
}
56-
public function setReturn(FQCN $fqcn){
56+
public function setReturn(FQCN $fqcn = null){
5757
$this->return = $fqcn;
5858
}
5959
public function isPublic() {

src/Generator/IndexGenerator.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ public function generateProjectIndex(Index $index){
113113
$all = count($classMap);
114114
foreach($index->getClassMap() as $fqcn => $file) {
115115
$start = microtime(1);
116-
$this->processFile($index, $fqcn, $file, false, false);
116+
$this->processFile($index, $file, false, false);
117117
$end = microtime(1) - $start;
118+
118119
$this->getLogger()->addDebug("Indexing: [$end]s");
119120
$this->getLogger()->addDebug("Memory: ". memory_get_usage());
120121
$globalTime += $end;
@@ -126,16 +127,11 @@ public function generateProjectIndex(Index $index){
126127
gc_enable();
127128
}
128129

129-
public function processFile(Index $index, $fqcn, $file,
130+
public function processFile(Index $index, $file,
130131
$rewrite=false, $createCache=true
131132
){
132133
$this->getLogger()
133134
->addInfo("processing $file");
134-
135-
$fqcn = $this->getClassUtils()->getParser()
136-
->parseFQCN($fqcn);
137-
$this->getLogger()
138-
->addInfo(sprintf("FQCN: %s", $fqcn->toString()));
139135
if($index->isParsed($file) && !$rewrite){
140136
return;
141137
}
@@ -145,7 +141,7 @@ public function processFile(Index $index, $fqcn, $file,
145141
$parser = $this->getClassUtils()->getParser();
146142
$parser->addProcessor($processor);
147143
$nodes = $this->getClassUtils()->getParser()
148-
->parseFile($fqcn, $file, $createCache);
144+
->parseFile($file, null, $createCache);
149145
$end = microtime(1) - $startParser;
150146
$this->getLogger()
151147
->addInfo("Parsing: [$end]s");

src/Parser/CommentParser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class CommentParser {
1515
public function __construct(UseParser $useParser){
1616
$this->useParser = $useParser;
1717
}
18+
1819
/**
1920
* Parses DocComment block
2021
*
@@ -66,6 +67,9 @@ protected function parseDoc(Comment $comment, $text){
6667
$this->createProperty($tag)
6768
);
6869
break;
70+
case "inheritdoc":
71+
$comment->markInheritDoc();
72+
break;
6973
}
7074
}
7175
}

src/Parser/MethodParser.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Entity\Node\MethodData;
66
use Entity\Node\MethodParam;
7+
use Entity\Node\Comment;
78
use PhpParser\Node\Stmt\ClassMethod;
89
use PhpParser\Node\Param;
910
use PhpParser\Node\Name;
@@ -39,15 +40,20 @@ public function parse(ClassMethod $node)
3940
$method->setType($node->type);
4041
$comments = $node->getAttribute("comments");
4142
if(is_array($comments)){
42-
/** @var \Entity\Node\Comment $comment */
43+
/** @var Comment */
4344
$comment = $this->commentParser->parse(
4445
$comments[count($comments)-1]->getText()
4546
);
46-
$method->doc = $comment->getDoc();
47-
$method->return = $comment->getReturn();
48-
foreach($comment->getVars() as $var){
49-
if($var instanceof MethodParam){
50-
$method->addParam($var);
47+
if($comment->isInheritDoc()){
48+
$method->doc = Comment::INHERIT_MARK;
49+
}
50+
else {
51+
$method->doc = $comment->getDoc();
52+
$method->return = $comment->getReturn();
53+
foreach($comment->getVars() as $var){
54+
if($var instanceof MethodParam){
55+
$method->addParam($var);
56+
}
5157
}
5258
}
5359
}

src/Parser/Parser.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Parser;
44

55
use Entity\FQN;
6-
use Entity\FQCN;
76
use Entity\Node\Uses;
87
use Utils\PathResolver;
98
use PhpParser\Parser AS ASTGenerator;
@@ -30,25 +29,21 @@ public function __construct(
3029
$this->astPool = [];
3130
$this->logger = $logger;
3231
}
33-
public function parseFile(FQN $fqcn, $file, $createCache=true){
32+
public function parseFile($file, Uses $uses = null, $createCache=true){
3433
$file = $this->path->getAbsolutePath($file);
3534
$content = $this->path->read($file);
36-
return $this->parseContent($fqcn, $file, $content, $createCache);
35+
return $this->parseContent($file, $content, $uses, $createCache);
3736
}
38-
public function parseContent(FQN $fqcn, $file, $content, $createCache=true){
37+
public function parseContent($file, $content, Uses $uses = null, $createCache=true){
3938
if($createCache){
4039
$hash = hash('md5', $content);
4140
if(!array_key_exists($file, $this->astPool)){
4241
$this->astPool[$file] = [0,0];
4342
}
4443
list($oldHash, $ast) = $this->astPool[$file];
4544
}
46-
if($fqcn instanceof FQCN){
47-
$uses = new Uses($this->parseFQCN($fqcn->getNamespace()));
48-
}
49-
else {
50-
$uses = new Uses($fqcn);
51-
$fqcn = new FQCN('', $fqcn->getParts());
45+
if(!$uses instanceof Uses){
46+
$uses = new Uses(new FQN);
5247
}
5348
$this->setUses($uses);
5449
$this->setFileInfo($uses, $file);
@@ -77,9 +72,13 @@ public function parseContent(FQN $fqcn, $file, $content, $createCache=true){
7772
return $nodes;
7873
}
7974
public function setUses(Uses $uses){
75+
$this->uses = $uses;
8076
$this->useParser->setUses($uses);
8177
$this->namespaceParser->setUses($uses);
8278
}
79+
public function getUses(){
80+
return $this->uses;
81+
}
8382
public function parseFQCN($fqcn){
8483
return $this->useParser->parseFQCN($fqcn);
8584
}
@@ -121,4 +120,5 @@ protected function setFileInfo(Uses $uses, $file){
121120
/** @var NamespaceParser */
122121
private $namespaceParser;
123122
private $useParser;
123+
private $uses;
124124
}

0 commit comments

Comments
 (0)