Debian Codegraph @

CodeLite/PHPSourceFile.cpp

raw
1
2 #include "PHPDocVisitor.h"
3 #include "PHPEntityClass.h"
4 #include "PHPEntityFunction.h"
5 #include "PHPEntityFunctionAlias.h"
6 #include "PHPEntityNamespace.h"
7 #include "PHPEntityVariable.h"
8 #include "PHPLookupTable.h"
9 #include "PHPScannerTokens.h"
10 #include "PHPSourceFile.h"
11 #include "fileutils.h"
12 #include <unordered_set>
13 #include <wx/arrstr.h>
14 #include <wx/ffile.h>
15
16 #define NEXT_TOKEN_BREAK_IF_NOT(t, action) \
17 { \
18 if(!NextToken(token)) \
19 break; \
20 if(token.type != t) { \
21 action; \
22 break; \
23 } \
24 }
25
26 PHPSourceFile::PHPSourceFile(const wxString& content, PHPLookupTable* lookup)
27 : m_text(content)
28 , m_parseFunctionBody(false)
29 , m_depth(0)
30 , m_reachedEOF(false)
31 , m_converter(NULL)
32 , m_lookup(lookup)
33 {
34 m_scanner = ::phpLexerNew(content, kPhpLexerOpt_ReturnComments);
35 }
36
37 PHPSourceFile::PHPSourceFile(const wxFileName& filename, PHPLookupTable* lookup)
38 : m_filename(filename)
39 , m_parseFunctionBody(false)
40 , m_depth(0)
41 , m_reachedEOF(false)
42 , m_converter(NULL)
43 , m_lookup(lookup)
44 {
45 // Filename is kept in absolute path
46 m_filename.MakeAbsolute();
47
48 wxString content;
49 if(FileUtils::ReadFileContent(filename, content, wxConvISO8859_1)) {
50 m_text.swap(content);
51 }
52 m_scanner = ::phpLexerNew(m_text, kPhpLexerOpt_ReturnComments);
53 }
54
55 PHPSourceFile::~PHPSourceFile()
56 {
57 if(m_scanner) {
58 ::phpLexerDestroy(&m_scanner);
59 }
60 }
61
62 bool PHPSourceFile::IsInPHPSection(const wxString& buffer)
63 {
64 PHPScanner_t scanner = ::phpLexerNew(buffer);
65 if(!scanner)
66 return false;
67 phpLexerToken tok;
68 bool inPhp = false;
69 while(::phpLexerNext(scanner, tok)) {
70 if(::phpLexerIsPHPCode(scanner)) {
71 inPhp = true;
72 } else {
73 inPhp = false;
74 }
75 }
76 ::phpLexerDestroy(&scanner);
77 return inPhp;
78 }
79
80 void PHPSourceFile::Parse(int exitDepth)
81 {
82 int retDepth = exitDepth;
83 phpLexerToken token;
84 while(NextToken(token)) {
85 switch(token.type) {
86 case '=':
87 m_lookBackTokens.clear();
88 break;
89 case '{':
90 m_lookBackTokens.clear();
91 break;
92 case '}':
93 m_lookBackTokens.clear();
94 if(m_depth == retDepth) {
95 return;
96 }
97 break;
98 case ';':
99 m_lookBackTokens.clear();
100 break;
101 case kPHP_T_VARIABLE:
102 if(!CurrentScope()->Is(kEntityTypeClass)) {
103 // A global variable
104 OnVariable(token);
105 }
106 break;
107 case kPHP_T_CATCH:
108 // found 'catch (...)'
109 OnCatch();
110 break;
111 case kPHP_T_PUBLIC:
112 case kPHP_T_PRIVATE:
113 case kPHP_T_PROTECTED: {
114 int visibility = token.type;
115 PHPEntityClass* cls = CurrentScope()->Cast<PHPEntityClass>();
116 if(cls) {
117 /// keep the current token
118 m_lookBackTokens.push_back(token);
119
120 // Now we have a small problem here:
121 // public can be a start for a member or a function
122 // we let the lexer run forward until it finds kPHP_T_VARIABLE (for variable)
123 // or kPHP_T_IDENTIFIER
124 int what = ReadUntilFoundOneOf(kPHP_T_VARIABLE, kPHP_T_FUNCTION, token);
125 if(what == kPHP_T_VARIABLE) {
126 // A variable
127 PHPEntityBase::Ptr_t member(new PHPEntityVariable());
128 member->SetFilename(m_filename.GetFullPath());
129 member->Cast<PHPEntityVariable>()->SetVisibility(visibility);
130 member->Cast<PHPEntityVariable>()->SetFullName(token.Text());
131 size_t flags = LookBackForVariablesFlags();
132 member->Cast<PHPEntityVariable>()->SetFlag(kVar_Member);
133 member->Cast<PHPEntityVariable>()->SetFlag(kVar_Const, flags & kVar_Const);
134 member->Cast<PHPEntityVariable>()->SetFlag(kVar_Static, flags & kVar_Static);
135 member->Cast<PHPEntityVariable>()->SetLine(token.lineNumber);
136 CurrentScope()->AddChild(member);
137
138 // Handle member assignment
139 // public $memberVar = new Something();
140 // for such cases, assign $memberVar type of Something()
141 phpLexerToken t;
142 if(!NextToken(t)) {
143 // EOF
144 return;
145 }
146
147 if(t.type == '=') {
148 // assignment
149 wxString expr;
150 if(!ReadExpression(expr)) {
151 return;
152 }
153
154 // Optimize 'new ClassName(..)' expression
155 if(expr.StartsWith("new")) {
156 expr = expr.Mid(3);
157 expr.Trim().Trim(false);
158 expr = expr.BeforeFirst('(');
159 expr.Trim().Trim(false);
160 member->Cast<PHPEntityVariable>()->SetTypeHint(MakeIdentifierAbsolute(expr));
161
162 } else {
163 // keep the expression
164 member->Cast<PHPEntityVariable>()->SetExpressionHint(expr);
165 }
166
167 } else {
168 // restore the token
169 UngetToken(t);
170 if(!ConsumeUntil(';'))
171 return;
172 }
173
174 } else if(what == kPHP_T_FUNCTION) {
175 // A function...
176 OnFunction();
177 m_lookBackTokens.clear();
178 }
179 }
180 break;
181 }
182 case kPHP_T_DEFINE:
183 // Define statement
184 OnDefine(token);
185 break;
186 case kPHP_T_CONST:
187 OnConstant(token);
188 break;
189 case kPHP_T_REQUIRE:
190 case kPHP_T_REQUIRE_ONCE:
191 case kPHP_T_INCLUDE:
192 case kPHP_T_INCLUDE_ONCE:
193 // Handle include files
194 m_lookBackTokens.clear();
195 break;
196 case kPHP_T_FOREACH:
197 // found "foreach" statement
198 OnForEach();
199 m_lookBackTokens.clear();
200 break;
201 case kPHP_T_USE:
202 // Found outer 'use' statement - construct the alias table
203 if(Class()) {
204 // inside a class, this means that this is a 'use <trait>;'
205 OnUseTrait();
206 } else {
207 // alias table
208 OnUse();
209 }
210 m_lookBackTokens.clear();
211 break;
212 case kPHP_T_CLASS:
213 case kPHP_T_INTERFACE:
214 case kPHP_T_TRAIT:
215 // Found class
216 OnClass(token);
217 m_lookBackTokens.clear();
218 break;
219 case kPHP_T_NAMESPACE:
220 // Found a namespace
221 OnNamespace();
222 m_lookBackTokens.clear();
223 break;
224 case kPHP_T_FUNCTION:
225 // Found function
226 OnFunction();
227 m_lookBackTokens.clear();
228 break;
229 default:
230 // Keep the token
231 break;
232 }
233 }
234 PhaseTwo();
235 }
236
237 void PHPSourceFile::OnUse()
238 {
239 wxString fullname, alias, temp;
240 phpLexerToken token;
241 bool cont = true;
242 while(cont && NextToken(token)) {
243 switch(token.type) {
244 case ',':
245 case ';': {
246 if(fullname.IsEmpty()) {
247 // no full name yet
248 fullname.swap(temp);
249
250 } else if(alias.IsEmpty()) {
251 alias.swap(temp);
252 }
253
254 if(alias.IsEmpty()) {
255 // no alias provided, use the last part of the fullname
256 alias = fullname.AfterLast('\\');
257 }
258
259 if(!fullname.IsEmpty() && !alias.IsEmpty()) {
260 // Use namespace is alway refered as fullpath namespace
261 // So writing:
262 // use Zend\Mvc\Controll\Action;
263 // is equal for writing:
264 // use \Zend\Mvc\Controll\Action;
265 // For simplicitiy, we change it to fully qualified path
266 // so parsing is easier
267 if(!fullname.StartsWith("\\")) {
268 fullname.Prepend("\\");
269 }
270 m_aliases.insert(std::make_pair(alias, MakeIdentifierAbsolute(fullname)));
271 }
272 temp.clear();
273 fullname.clear();
274 alias.clear();
275 if(token.type == ';') {
276 cont = false;
277 }
278 } break;
279 case kPHP_T_AS: {
280 fullname.swap(temp);
281 temp.clear();
282 } break;
283 default:
284 temp << token.Text();
285 break;
286 }
287 }
288 }
289
290 void PHPSourceFile::OnNamespace()
291 {
292 // Read until we find the line delimiter ';' or EOF found
293 wxString path;
294 phpLexerToken token;
295 while(NextToken(token)) {
296 if(token.type == ';') {
297 break;
298 }
299
300 // Make sure that the namespace path is alway set in absolute path
301 // i.e. starts with kPHP_T_NS_SEPARATOR
302 if(path.IsEmpty() && token.type != kPHP_T_NS_SEPARATOR) {
303 path << "\\";
304 }
305 path << token.Text();
306 }
307
308 if(m_scopes.empty()) {
309 // no scope is set, push the global scope
310 m_scopes.push_back(PHPEntityBase::Ptr_t(new PHPEntityNamespace()));
311 PHPEntityNamespace* ns = CurrentScope()->Cast<PHPEntityNamespace>();
312 if(ns) {
313 ns->SetFullName(path); // Global namespace
314 }
315 } else {
316 // PHP parsing error... (namespace must be the first thing on the file)
317 }
318 }
319
320 void PHPSourceFile::OnFunction()
321 {
322 // read the next token
323 phpLexerToken token;
324 if(!NextToken(token)) {
325 return;
326 }
327
328 bool funcReturnRef = false;
329 if(token.type == '&') {
330 funcReturnRef = true;
331 if(!NextToken(token)) {
332 return;
333 }
334 }
335
336 PHPEntityFunction* func(NULL);
337 int funcDepth(0);
338 if(token.type == kPHP_T_IDENTIFIER) {
339 // the function name
340 func = new PHPEntityFunction();
341 func->SetFullName(token.Text());
342 func->SetLine(token.lineNumber);
343
344 } else if(token.type == '(') {
345 funcDepth = 1; // Since we already consumed the open brace
346 // anonymous function
347 func = new PHPEntityFunction();
348 func->SetLine(token.lineNumber);
349 }
350
351 if(!func)
352 return;
353 PHPEntityBase::Ptr_t funcPtr(func);
354 if(funcReturnRef) {
355 funcPtr->SetFlag(kFunc_ReturnReference);
356 }
357
358 // add the function to the current scope
359 CurrentScope()->AddChild(funcPtr);
360
361 // Set the function as the current scope
362 m_scopes.push_back(funcPtr);
363
364 // update function attributes
365 ParseFunctionSignature(funcDepth);
366 func->SetFlags(LookBackForFunctionFlags());
367 if(LookBackTokensContains(kPHP_T_ABSTRACT) || // The 'abstract modifier was found for this this function
368 (funcPtr->Parent() && funcPtr->Parent()->Is(kEntityTypeClass) &&
369 funcPtr->Parent()->Cast<PHPEntityClass>()->IsInterface())) // We are inside an interface
370 {
371 // Mark this function as an abstract function
372 func->SetFlags(func->GetFlags() | kFunc_Abstract);
373 }
374
375 if(func->HasFlag(kFunc_Abstract)) {
376 // an abstract function - it has no body
377 if(!ConsumeUntil(';')) {
378 // could not locate the function delimiter, remove it from the stack
379 // we probably reached EOF here
380 m_scopes.pop_back();
381 }
382
383 } else {
384 if(!NextToken(token))
385 return;
386 if(token.type == ':') {
387 // PHP 7 signature type
388 // function foobar(...) : RETURN_TYPE
389
390 if(!NextToken(token))
391 return;
392 if(token.type == '?') {
393 // PHP 7.1 nullable return
394 funcPtr->SetFlag(kFunc_ReturnNullable);
395 } else {
396 UngetToken(token);
397 }
398
399 wxString returnValuetype = ReadFunctionReturnValueFromSignature();
400 if(returnValuetype.IsEmpty())
401 return; // parse error
402 func->SetReturnValue(returnValuetype);
403
404 } else {
405 // untake the token and place it back on the "unget" list
406 UngetToken(token);
407 }
408
409 if(ReadUntilFound('{', token)) {
410 // found the function body starting point
411 if(IsParseFunctionBody()) {
412 ParseFunctionBody();
413 } else {
414 // Consume the function body
415 ConsumeFunctionBody();
416 }
417 } else {
418 // could not locate the open brace!
419 // remove this function from the stack
420 m_scopes.pop_back();
421 }
422 }
423
424 // Remove the current function from the scope list
425 if(!m_reachedEOF) {
426 m_scopes.pop_back();
427 }
428 m_lookBackTokens.clear();
429 }
430
431 PHPEntityBase::Ptr_t PHPSourceFile::CurrentScope()
432 {
433 if(m_scopes.empty()) {
434 // no scope is set, push the global scope
435 m_scopes.push_back(PHPEntityBase::Ptr_t(new PHPEntityNamespace()));
436 CurrentScope()->SetFullName("\\"); // Global namespace
437 }
438 return m_scopes.back();
439 }
440
441 size_t PHPSourceFile::LookBackForFunctionFlags()
442 {
443 size_t flags(0);
444 for(size_t i = 0; i < m_lookBackTokens.size(); ++i) {
445 const phpLexerToken& tok = m_lookBackTokens.at(i);
446 if(tok.type == kPHP_T_ABSTRACT) {
447 flags |= kFunc_Abstract;
448
449 } else if(tok.type == kPHP_T_FINAL) {
450 flags |= kFunc_Final;
451 } else if(tok.type == kPHP_T_STATIC) {
452 flags |= kFunc_Static;
453
454 } else if(tok.type == kPHP_T_PUBLIC) {
455 flags |= kFunc_Public;
456 flags &= ~kFunc_Private;
457 flags &= ~kFunc_Protected;
458
459 } else if(tok.type == kPHP_T_PRIVATE) {
460 flags |= kFunc_Private;
461 flags &= ~kFunc_Public;
462 flags &= ~kFunc_Protected;
463
464 } else if(tok.type == kPHP_T_PROTECTED) {
465 flags |= kFunc_Protected;
466 flags &= ~kFunc_Public;
467 flags &= ~kFunc_Private;
468 }
469 }
470 return flags;
471 }
472
473 void PHPSourceFile::ParseFunctionSignature(int startingDepth)
474 {
475 phpLexerToken token;
476 if(startingDepth == 0) {
477 // loop until we find the open brace
478 while(NextToken(token)) {
479 if(token.type == '(') {
480 ++startingDepth;
481 break;
482 }
483 }
484 if(startingDepth == 0)
485 return;
486 }
487
488 // at this point the 'depth' is 1, as we already read the open brace
489 int depth = 1;
490 wxString typeHint;
491 wxString defaultValue;
492 wxString name;
493 PHPEntityVariable* var(NULL);
494 bool collectingDefaultValue = false;
495 while(NextToken(token)) {
496 switch(token.type) {
497 case kPHP_T_VARIABLE:
498 if(!var) {
499 // var can be non null if we are parsing PHP-7 function arguments
500 // with type-hinting
501 var = new PHPEntityVariable();
502 }
503
504 name = token.Text();
505 var->SetLine(token.lineNumber);
506 var->SetFilename(m_filename);
507 // Mark this variable as function argument
508 var->SetFlag(kVar_FunctionArg);
509 if(name.StartsWith("&")) {
510 var->SetIsReference(true);
511 name.Remove(0, 1);
512 } else if(typeHint.EndsWith("&")) {
513 var->SetIsReference(true);
514 typeHint.RemoveLast();
515 }
516 var->SetFullName(name);
517 var->SetTypeHint(MakeIdentifierAbsolute(typeHint));
518 break;
519 case '(':
520 depth++;
521 if(collectingDefaultValue) {
522 defaultValue << "(";
523 }
524 break;
525 case ')':
526 depth--;
527 // if the depth goes under 1 - we are done
528 if(depth < 1) {
529 if(var) {
530 var->SetDefaultValue(defaultValue);
531 CurrentScope()->AddChild(PHPEntityBase::Ptr_t(var));
532 }
533 return;
534
535 } else if(depth) {
536 defaultValue << token.Text();
537 }
538 break;
539 case '=':
540 // default value
541 collectingDefaultValue = true;
542 break;
543 case ',':
544 if(var) {
545 var->SetDefaultValue(defaultValue);
546 CurrentScope()->AddChild(PHPEntityBase::Ptr_t(var));
547 }
548 var = NULL;
549 typeHint.Clear();
550 defaultValue.Clear();
551 collectingDefaultValue = false;
552 break;
553 case '?':
554 if(!var) {
555 var = new PHPEntityVariable();
556 }
557 var->SetIsNullable(true);
558 break;
559 case kPHP_T_NS_SEPARATOR:
560 case kPHP_T_IDENTIFIER:
561 if(!var) {
562 // PHP-7 type hinting function arguments
563 var = new PHPEntityVariable();
564 UngetToken(token);
565 typeHint = ReadType();
566 if(!typeHint.IsEmpty()) {
567 break;
568 }
569 }
570 // all "else" cases simply fall into the default case
571 default:
572 if(collectingDefaultValue) {
573 defaultValue << token.Text();
574 } else {
575 typeHint << token.Text();
576 }
577 break;
578 }
579 }
580 }
581
582 void PHPSourceFile::PrintStdout()
583 {
584 // print the alias table
585 wxPrintf("Alias table:\n");
586 wxPrintf("===========\n");
587 std::map<wxString, wxString>::iterator iter = m_aliases.begin();
588 for(; iter != m_aliases.end(); ++iter) {
589 wxPrintf("%s => %s\n", iter->first, iter->second);
590 }
591 wxPrintf("===========\n");
592 if(m_scopes.empty())
593 return;
594 m_scopes.front()->PrintStdout(0);
595 }
596
597 bool PHPSourceFile::ReadUntilFound(int delim, phpLexerToken& token)
598 {
599 // loop until we find the open brace
600 while(NextToken(token)) {
601 if(token.type == delim) {
602 return true;
603 }
604 }
605 return false;
606 }
607
608 void PHPSourceFile::ConsumeFunctionBody()
609 {
610 int depth = m_depth;
611 phpLexerToken token;
612 while(NextToken(token)) {
613 switch(token.type) {
614 case '}':
615 if(m_depth < depth) {
616 return;
617 }
618 break;
619 default:
620 break;
621 }
622 }
623 }
624
625 void PHPSourceFile::ParseFunctionBody()
626 {
627 m_lookBackTokens.clear();
628
629 // when we reach the current depth-1 -> leave
630 int exitDepth = m_depth - 1;
631 phpLexerToken token;
632 PHPEntityBase::Ptr_t var(NULL);
633 while(NextToken(token)) {
634 switch(token.type) {
635 case '{':
636 m_lookBackTokens.clear();
637 break;
638 case '}':
639 m_lookBackTokens.clear();
640 if(m_depth == exitDepth) {
641 return;
642 }
643 break;
644 case ';':
645 m_lookBackTokens.clear();
646 break;
647 case kPHP_T_CATCH:
648 OnCatch();
649 break;
650 case kPHP_T_VARIABLE: {
651 OnVariable(token);
652 break;
653 } break;
654 default:
655 break;
656 }
657 }
658 }
659
660 wxString PHPSourceFile::ReadType()
661 {
662 bool cont = true;
663 wxString type;
664 phpLexerToken token;
665
666 while(cont && NextToken(token)) {
667 switch(token.type) {
668 case kPHP_T_IDENTIFIER:
669 type << token.Text();
670 break;
671
672 case kPHP_T_NS_SEPARATOR:
673 type << token.Text();
674 break;
675 default:
676 // restore the token so next call to NextToken
677 // will pick it up again
678 UngetToken(token);
679 cont = false;
680 break;
681 }
682 }
683
684 type = MakeTypehintAbsolute(type);
685 return type;
686 }
687
688 PHPEntityBase::Ptr_t PHPSourceFile::Namespace()
689 {
690 if(m_scopes.empty()) {
691 return CurrentScope();
692 }
693 return *m_scopes.begin();
694 }
695
696 wxString PHPSourceFile::LookBackForTypeHint()
697 {
698 if(m_lookBackTokens.empty())
699 return wxEmptyString;
700 wxArrayString tokens;
701
702 for(int i = (int)m_lookBackTokens.size() - 1; i >= 0; --i) {
703 if(m_lookBackTokens.at(i).type == kPHP_T_IDENTIFIER || m_lookBackTokens.at(i).type == kPHP_T_NS_SEPARATOR) {
704 tokens.Insert(m_lookBackTokens.at(i).Text(), 0);
705 } else {
706 break;
707 }
708 }
709
710 wxString type;
711 for(size_t i = 0; i < tokens.GetCount(); ++i) {
712 type << tokens.Item(i);
713 }
714 return type;
715 }
716
717 void PHPSourceFile::PhaseTwo()
718 {
719 // Visit each entity found during the parsing stage
720 // and try to match it with its phpdoc comment block (we do this by line number)
721 // the visitor also makes sure that each entity is properly assigned with the current file name
722 PHPDocVisitor visitor(*this, m_comments);
723 visitor.Visit(Namespace());
724 }
725
726 bool PHPSourceFile::NextToken(phpLexerToken& token)
727 {
728 bool res = ::phpLexerNext(m_scanner, token);
729 if(res && (token.type == kPHP_T_C_COMMENT)) {
730 m_comments.push_back(token);
731 // We keep comments made in the class body
732 if(!m_scopes.empty() && CurrentScope()->Is(kEntityTypeClass)) {
733 PHPDocVar::Ptr_t var(new PHPDocVar(*this, token.Text()));
734 if(var->IsOk()) {
735 var->SetLineNumber(token.lineNumber);
736 // this comment is a @var comment
737 CurrentScope()->Cast<PHPEntityClass>()->AddVarPhpDoc(var);
738 }
739 }
740 }
741
742 if(token.type == '{') {
743 m_depth++;
744 } else if(token.type == '}') {
745 m_depth--;
746 } else if(token.type == ';') {
747 m_lookBackTokens.clear();
748 }
749 if(!res)
750 m_reachedEOF = true;
751 if(res)
752 m_lookBackTokens.push_back(token);
753 return res;
754 }
755
756 wxString PHPSourceFile::MakeIdentifierAbsolute(const wxString& type) { return DoMakeIdentifierAbsolute(type, false); }
757
758 void PHPSourceFile::OnClass(const phpLexerToken& tok)
759 {
760 wxString classDoc;
761 const phpLexerToken& prevToken = GetPreviousToken();
762 if(!prevToken.IsNull() && prevToken.IsDocComment()) {
763 classDoc = prevToken.Text();
764 }
765
766 // A "complex" example: class A extends BaseClass implements C, D {}
767 bool isAbstractClass = LookBackTokensContains(kPHP_T_ABSTRACT);
768
769 // Read until we get the class name
770 phpLexerToken token;
771 while(NextToken(token)) {
772 if(token.IsAnyComment())
773 continue;
774 if(token.type != kPHP_T_IDENTIFIER) {
775 // expecting the class name
776 return;
777 }
778 break;
779 }
780
781 // create new class entity
782 PHPEntityBase::Ptr_t klass(new PHPEntityClass());
783 klass->SetFilename(m_filename.GetFullPath());
784 klass->SetDocComment(classDoc);
785
786 PHPEntityClass* pClass = klass->Cast<PHPEntityClass>();
787
788 // Is the class an interface?
789 pClass->SetIsInterface(tok.type == kPHP_T_INTERFACE);
790 pClass->SetIsAbstractClass(isAbstractClass);
791 pClass->SetIsTrait(tok.type == kPHP_T_TRAIT);
792 pClass->SetFullName(PrependCurrentScope(token.Text()));
793 pClass->SetLine(token.lineNumber);
794
795 while(NextToken(token)) {
796 if(token.IsAnyComment())
797 continue;
798 switch(token.type) {
799 case kPHP_T_EXTENDS: {
800 // inheritance
801 wxString extends = ReadExtends();
802 if(extends.IsEmpty())
803 return;
804 // No need to call 'MakeIdentifierAbsolute' it was called internally by
805 pClass->SetExtends(extends);
806 } break;
807 case kPHP_T_IMPLEMENTS: {
808 wxArrayString implements;
809 ReadImplements(implements);
810 pClass->SetImplements(implements);
811 } break;
812 case '{': {
813 // entering the class body
814 // add the current class to the current scope
815 CurrentScope()->AddChild(klass);
816 m_scopes.push_back(klass);
817 Parse(m_depth - 1);
818 if(!m_reachedEOF) {
819 m_scopes.pop_back();
820 }
821 return;
822 }
823 default:
824 break;
825 }
826 }
827 }
828
829 bool PHPSourceFile::ReadCommaSeparatedIdentifiers(int delim, wxArrayString& list)
830 {
831 phpLexerToken token;
832 wxString temp;
833 while(NextToken(token)) {
834 if(token.IsAnyComment())
835 continue;
836 if(token.type == delim) {
837 if(!temp.IsEmpty() && list.Index(temp) == wxNOT_FOUND) {
838 list.Add(MakeIdentifierAbsolute(temp));
839 }
840 UngetToken(token);
841 return true;
842 }
843
844 switch(token.type) {
845 case ',':
846 if(list.Index(temp) == wxNOT_FOUND) {
847 list.Add(MakeIdentifierAbsolute(temp));
848 }
849 temp.clear();
850 break;
851 default:
852 temp << token.Text();
853 break;
854 }
855 }
856 return false;
857 }
858
859 bool PHPSourceFile::ConsumeUntil(int delim)
860 {
861 phpLexerToken token;
862 while(NextToken(token)) {
863 if(token.type == delim) {
864 return true;
865 }
866 }
867 return false;
868 }
869
870 bool PHPSourceFile::ReadExpression(wxString& expression)
871 {
872 expression.clear();
873 phpLexerToken token;
874 int depth(0);
875 while(NextToken(token)) {
876 if(token.type == ';') {
877 return true;
878
879 } else if(token.type == '{') {
880 UngetToken(token);
881 return true;
882 }
883
884 switch(token.type) {
885 case kPHP_T_FUNCTION:
886 OnFunction();
887 break;
888 case kPHP_T_REQUIRE:
889 case kPHP_T_REQUIRE_ONCE:
890 expression.clear();
891 return false;
892
893 case kPHP_T_STRING_CAST:
894 case kPHP_T_CONSTANT_ENCAPSED_STRING:
895 case kPHP_T_C_COMMENT:
896 case kPHP_T_CXX_COMMENT:
897 // skip comments and strings
898 break;
899 case '(':
900 depth++;
901 expression << "(";
902 break;
903 case ')':
904 depth--;
905 if(depth == 0) {
906 expression << ")";
907 }
908 break;
909 case kPHP_T_NEW:
910 if(depth == 0) {
911 expression << token.Text() << " ";
912 }
913 break;
914 default:
915 if(depth == 0) {
916 expression << token.Text();
917 }
918 break;
919 }
920 }
921 // reached EOF
922 return false;
923 }
924
925 void PHPSourceFile::UngetToken(const phpLexerToken& token)
926 {
927 ::phpLexerUnget(m_scanner);
928 // undo any depth / comments
929 if(token.type == '{') {
930 m_depth--;
931 } else if(token.type == '}') {
932 m_depth++;
933 } else if(token.type == kPHP_T_C_COMMENT && !m_comments.empty()) {
934 m_comments.erase(m_comments.begin() + m_comments.size() - 1);
935 }
936 }
937
938 const PHPEntityBase* PHPSourceFile::Class()
939 {
940 PHPEntityBase::Ptr_t curScope = CurrentScope();
941 PHPEntityBase* pScope = curScope.Get();
942 while(pScope) {
943 PHPEntityClass* cls = pScope->Cast<PHPEntityClass>();
944 if(cls) {
945 // this scope is a class
946 return pScope;
947 }
948 pScope = pScope->Parent();
949 }
950 return NULL;
951 }
952
953 int PHPSourceFile::ReadUntilFoundOneOf(int delim1, int delim2, phpLexerToken& token)
954 {
955 // loop until we find the open brace
956 while(NextToken(token)) {
957 if(token.type == delim1) {
958 return delim1;
959 } else if(token.type == delim2) {
960 return delim2;
961 }
962 }
963 return wxNOT_FOUND;
964 }
965
966 bool PHPSourceFile::LookBackTokensContains(int type) const
967 {
968 for(size_t i = 0; i < m_lookBackTokens.size(); ++i) {
969 if(m_lookBackTokens.at(i).type == type)
970 return true;
971 }
972 return false;
973 }
974
975 size_t PHPSourceFile::LookBackForVariablesFlags()
976 {
977 size_t flags(kVar_Public);
978 for(size_t i = 0; i < m_lookBackTokens.size(); ++i) {
979 const phpLexerToken& tok = m_lookBackTokens.at(i);
980 if(tok.type == kPHP_T_STATIC) {
981 flags |= kVar_Static;
982
983 } else if(tok.type == kPHP_T_CONST) {
984 flags |= kVar_Const;
985
986 } else if(tok.type == kPHP_T_PUBLIC) {
987 flags |= kVar_Public;
988 flags &= ~kVar_Private;
989 flags &= ~kVar_Protected;
990
991 } else if(tok.type == kPHP_T_PRIVATE) {
992 flags |= kVar_Private;
993 flags &= ~kVar_Public;
994 flags &= ~kVar_Protected;
995
996 } else if(tok.type == kPHP_T_PROTECTED) {
997 flags |= kVar_Protected;
998 flags &= ~kVar_Private;
999 flags &= ~kVar_Public;
1000 }
1001 }
1002 return flags;
1003 }
1004
1005 void PHPSourceFile::OnVariable(const phpLexerToken& tok)
1006 {
1007 PHPEntityBase::Ptr_t var(new PHPEntityVariable());
1008 var->SetFullName(tok.Text());
1009 var->SetFilename(m_filename.GetFullPath());
1010 var->SetLine(tok.lineNumber);
1011 if(!CurrentScope()->FindChild(var->GetFullName(), true)) {
1012 CurrentScope()->AddChild(var);
1013 }
1014
1015 if(!ReadVariableInitialization(var)) {
1016 m_lookBackTokens.clear();
1017 return;
1018 }
1019 }
1020
1021 bool PHPSourceFile::ReadVariableInitialization(PHPEntityBase::Ptr_t var)
1022 {
1023 phpLexerToken token;
1024 if(!NextToken(token)) {
1025 return false;
1026 }
1027
1028 if(token.type != '=') {
1029 // restore the token
1030 UngetToken(token);
1031 return false;
1032 }
1033
1034 wxString expr;
1035 if(!ReadExpression(expr)) {
1036 return false; // EOF
1037 }
1038
1039 // Optimize 'new ClassName(..)' expression
1040 if(expr.StartsWith("new")) {
1041 expr = expr.Mid(3);
1042 expr.Trim().Trim(false);
1043 expr = expr.BeforeFirst('(');
1044 expr.Trim().Trim(false);
1045 var->Cast<PHPEntityVariable>()->SetTypeHint(MakeIdentifierAbsolute(expr));
1046
1047 } else {
1048 // keep the expression
1049 var->Cast<PHPEntityVariable>()->SetExpressionHint(expr);
1050 }
1051 return true;
1052 }
1053
1054 PHPEntityBase::List_t PHPSourceFile::GetAliases() const
1055 {
1056 PHPEntityBase::List_t aliases;
1057 std::map<wxString, wxString>::const_iterator iter = m_aliases.begin();
1058 for(; iter != m_aliases.end(); ++iter) {
1059 // wrap each alias with class entity
1060 PHPEntityBase::Ptr_t klass(new PHPEntityClass());
1061 klass->SetFullName(iter->second);
1062 klass->SetShortName(iter->first);
1063 klass->SetFilename(GetFilename());
1064 aliases.push_back(klass);
1065 }
1066 return aliases;
1067 }
1068
1069 void PHPSourceFile::OnDefine(const phpLexerToken& tok)
1070 {
1071 phpLexerToken token;
1072 if(!NextToken(token))
1073 return; // EOF
1074 if(token.type != '(') {
1075 ConsumeUntil(';');
1076 return;
1077 }
1078 if(!NextToken(token))
1079 return; // EOF
1080 if(token.type != kPHP_T_CONSTANT_ENCAPSED_STRING) {
1081 ConsumeUntil(';');
1082 return;
1083 }
1084 // Remove the quotes
1085 wxString varName = token.Text();
1086 if((varName.StartsWith("\"") && varName.EndsWith("\"")) || (varName.StartsWith("'") && varName.EndsWith("'"))) {
1087 varName.Remove(0, 1);
1088 varName.RemoveLast();
1089 // define() defines constants exactly as it was instructed
1090 // i.e. it does not take the current namespace into consideration
1091 PHPEntityBase::Ptr_t var(new PHPEntityVariable());
1092
1093 // Convert the variable into fullpath + relative name
1094 if(!varName.StartsWith("\\")) {
1095 varName.Prepend("\\");
1096 }
1097 wxString shortName = varName.AfterLast('\\');
1098 var->SetFullName(varName);
1099 var->SetShortName(shortName);
1100 var->SetFlag(kVar_Define);
1101 var->SetFilename(GetFilename());
1102 var->SetLine(tok.lineNumber);
1103
1104 // We keep the defines in a special list
1105 // this is because 'define' does not obay to the current scope
1106 m_defines.push_back(var);
1107 }
1108 // Always consume the 'define' statement
1109 ConsumeUntil(';');
1110 }
1111
1112 void PHPSourceFile::OnUseTrait()
1113 {
1114 PHPEntityBase::Ptr_t clas = CurrentScope();
1115 if(!clas)
1116 return;
1117
1118 // Collect the identifiers followed the 'use' statement
1119 wxArrayString identifiers;
1120 wxString tempname;
1121 phpLexerToken token;
1122 while(NextToken(token)) {
1123 switch(token.type) {
1124 case ',': {
1125 if(!tempname.IsEmpty()) {
1126 identifiers.Add(MakeIdentifierAbsolute(tempname));
1127 }
1128 tempname.clear();
1129 } break;
1130 case '{': {
1131 // we are looking at a case like:
1132 // use A, B { ... }
1133 if(!tempname.IsEmpty()) {
1134 identifiers.Add(MakeIdentifierAbsolute(tempname));
1135 ParseUseTraitsBody();
1136 }
1137 tempname.clear();
1138 // add the traits as list of 'extends'
1139 clas->Cast<PHPEntityClass>()->SetTraits(identifiers);
1140 return;
1141 } break;
1142 case ';': {
1143 if(!tempname.IsEmpty()) {
1144 identifiers.Add(MakeIdentifierAbsolute(tempname));
1145 }
1146 tempname.clear();
1147
1148 // add the traits as list of 'extends'
1149 clas->Cast<PHPEntityClass>()->SetTraits(identifiers);
1150 return;
1151 } break;
1152 default:
1153 tempname << token.Text();
1154 break;
1155 }
1156 }
1157 }
1158
1159 void PHPSourceFile::OnCatch()
1160 {
1161 // Read until we find the kPHP_T_VARIABLE
1162 bool cont(true);
1163 phpLexerToken token;
1164 wxString typehint;
1165 wxString varname;
1166 while(cont && NextToken(token)) {
1167 switch(token.type) {
1168 case kPHP_T_VARIABLE:
1169 cont = false;
1170 varname = token.Text();
1171 break;
1172 case kPHP_T_IDENTIFIER:
1173 case kPHP_T_NS_SEPARATOR:
1174 typehint << token.Text();
1175 break;
1176 default:
1177 break;
1178 }
1179 }
1180
1181 if(!varname.IsEmpty()) {
1182 // we found the variable
1183 PHPEntityBase::Ptr_t var(new PHPEntityVariable());
1184 var->SetFullName(varname);
1185 var->SetFilename(m_filename.GetFullPath());
1186 var->SetLine(token.lineNumber);
1187 var->Cast<PHPEntityVariable>()->SetTypeHint(MakeIdentifierAbsolute(typehint));
1188
1189 // add the variable to the current scope
1190 if(!CurrentScope()->FindChild(var->GetFullName(), true)) {
1191 CurrentScope()->AddChild(var);
1192 }
1193 }
1194 }
1195
1196 wxString PHPSourceFile::ReadExtends()
1197 {
1198 wxString type;
1199 phpLexerToken token;
1200 while(NextToken(token)) {
1201 if(token.type == kPHP_T_IDENTIFIER || token.type == kPHP_T_NS_SEPARATOR) {
1202 type << token.Text();
1203 } else {
1204 UngetToken(token);
1205 break;
1206 }
1207 }
1208 type = MakeIdentifierAbsolute(type);
1209 return type;
1210 }
1211
1212 void PHPSourceFile::ReadImplements(wxArrayString& impls)
1213 {
1214 wxString type;
1215 phpLexerToken token;
1216 while(NextToken(token)) {
1217 switch(token.type) {
1218 case kPHP_T_IDENTIFIER:
1219 case kPHP_T_NS_SEPARATOR:
1220 type << token.Text();
1221 break;
1222 case ',':
1223 // More to come
1224 if(!type.IsEmpty()) {
1225 wxString fullyQualifiedType = MakeIdentifierAbsolute(type);
1226 if(impls.Index(fullyQualifiedType) == wxNOT_FOUND) {
1227 impls.Add(fullyQualifiedType);
1228 }
1229 type.clear();
1230 }
1231 break;
1232 default:
1233 // unexpected token
1234 if(!type.IsEmpty()) {
1235 wxString fullyQualifiedType = MakeIdentifierAbsolute(type);
1236 if(impls.Index(fullyQualifiedType) == wxNOT_FOUND) {
1237 impls.Add(fullyQualifiedType);
1238 }
1239 type.clear();
1240 }
1241 UngetToken(token);
1242 return;
1243 }
1244 }
1245 }
1246
1247 /*foreach (array_expression as $value)
1248 statement
1249 foreach (array_expression as $key => $value)
1250 statement*/
1251 void PHPSourceFile::OnForEach()
1252 {
1253 // read until the "as" keyword
1254 phpLexerToken token;
1255 if(!ReadUntilFound(kPHP_T_AS, token))
1256 return;
1257
1258 // Found the "as" key word and consumed it
1259 if(!NextToken(token))
1260 return;
1261
1262 phpLexerToken peekToken;
1263 if(!NextToken(peekToken))
1264 return;
1265
1266 // Ensure we got a variable
1267 if(token.type != kPHP_T_VARIABLE)
1268 return;
1269
1270 // Add the key
1271 {
1272 PHPEntityBase::Ptr_t var(new PHPEntityVariable());
1273 var->SetFullName(token.Text());
1274 var->SetFilename(m_filename.GetFullPath());
1275 var->SetLine(token.lineNumber);
1276 if(!CurrentScope()->FindChild(var->GetFullName(), true)) {
1277 CurrentScope()->AddChild(var);
1278 }
1279 }
1280 // Check to see if we are using the syntax of:
1281 // foreach (array_expression as $key => $value)
1282 if(peekToken.type == kPHP_T_DOUBLE_ARROW) {
1283 if(!NextToken(token) || token.type != kPHP_T_VARIABLE) {
1284 return;
1285 }
1286
1287 // Add the value as well
1288 // Create a new variable
1289 PHPEntityBase::Ptr_t var(new PHPEntityVariable());
1290 var->SetFullName(token.Text());
1291 var->SetFilename(m_filename.GetFullPath());
1292 var->SetLine(token.lineNumber);
1293
1294 if(!CurrentScope()->FindChild(var->GetFullName(), true)) {
1295 CurrentScope()->AddChild(var);
1296 }
1297
1298 } else {
1299 UngetToken(peekToken);
1300 }
1301 }
1302
1303 void PHPSourceFile::ParseUseTraitsBody()
1304 {
1305 wxString fullname, alias, temp;
1306 phpLexerToken token;
1307 bool cont = true;
1308 while(cont && NextToken(token)) {
1309 switch(token.type) {
1310 case '}': {
1311 cont = false;
1312 } break;
1313 case ',':
1314 case ';': {
1315 if(fullname.IsEmpty()) {
1316 // no full name yet
1317 fullname.swap(temp);
1318
1319 } else if(alias.IsEmpty()) {
1320 alias.swap(temp);
1321 }
1322
1323 if(alias.IsEmpty()) {
1324 // no alias provided, use the last part of the fullname
1325 alias = fullname.AfterLast('\\');
1326 }
1327
1328 if(!fullname.IsEmpty() && !alias.IsEmpty()) {
1329 // Use namespace is alway refered as fullpath namespace
1330 // So writing:
1331 // use Zend\Mvc\Controll\Action;
1332 // is equal for writing:
1333 // use \Zend\Mvc\Controll\Action;
1334 // For simplicitiy, we change it to fully qualified path
1335 // so parsing is easier
1336 if(!fullname.StartsWith("\\")) {
1337 fullname.Prepend("\\");
1338 }
1339 PHPEntityBase::Ptr_t funcAlias(new PHPEntityFunctionAlias());
1340 funcAlias->Cast<PHPEntityFunctionAlias>()->SetRealname(MakeIdentifierAbsolute(fullname));
1341 funcAlias->Cast<PHPEntityFunctionAlias>()->SetScope(CurrentScope()->GetFullName());
1342 funcAlias->SetShortName(alias);
1343 funcAlias->SetFullName(CurrentScope()->GetFullName() + "\\" + alias);
1344 funcAlias->SetFilename(GetFilename());
1345 funcAlias->SetLine(token.lineNumber);
1346 CurrentScope()->AddChild(funcAlias);
1347 }
1348
1349 temp.clear();
1350 fullname.clear();
1351 alias.clear();
1352 } break;
1353 case kPHP_T_PAAMAYIM_NEKUDOTAYIM: {
1354 // Convert "::" into "\\"
1355 temp << "\\";
1356 } break;
1357 case kPHP_T_AS: {
1358 fullname.swap(temp);
1359 temp.clear();
1360 } break;
1361 case kPHP_T_INSTEADOF: {
1362 // For now, we are not interested in
1363 // A insteadof b; statements, so just clear the collected data so far
1364 fullname.clear();
1365 temp.clear();
1366 alias.clear();
1367 if(!ConsumeUntil(';'))
1368 return;
1369 } break;
1370 default:
1371 temp << token.Text();
1372 break;
1373 }
1374 }
1375 }
1376
1377 void PHPSourceFile::OnConstant(const phpLexerToken& tok)
1378 {
1379 // Parse constant line (possibly multiple constants)
1380 phpLexerToken token;
1381 PHPEntityBase::Ptr_t member;
1382 while(NextToken(token)) {
1383 if(token.type == '=') {
1384
1385 // The next value should contain the constant value
1386 wxString constantValue;
1387 while(NextToken(token)) {
1388 if(token.type == ';') {
1389 UngetToken(token);
1390 break;
1391 } else if(token.type == ',') {
1392 break;
1393 }
1394 constantValue << token.Text();
1395 }
1396
1397 if(member && !constantValue.IsEmpty()) {
1398 // Keep the constant value, we will be using it later for tooltip
1399 member->Cast<PHPEntityVariable>()->SetDefaultValue(constantValue);
1400 }
1401 }
1402 if(token.type == ';') {
1403 if(member) {
1404 CurrentScope()->AddChild(member);
1405 break;
1406 }
1407 } else if(token.type == ',') {
1408 if(member) {
1409 CurrentScope()->AddChild(member);
1410 member.Reset(NULL);
1411 }
1412 } else if(token.type == kPHP_T_IDENTIFIER) {
1413 // found the constant name
1414 member.Reset(new PHPEntityVariable());
1415 member->Cast<PHPEntityVariable>()->SetFlag(kVar_Const);
1416 member->Cast<PHPEntityVariable>()->SetFlag(kVar_Member);
1417 member->SetFullName(token.Text());
1418 member->SetLine(token.lineNumber);
1419 member->SetFilename(m_filename.GetFullPath());
1420 } else {
1421 // do nothing
1422 }
1423 }
1424 }
1425
1426 phpLexerToken& PHPSourceFile::GetPreviousToken()
1427 {
1428 static phpLexerToken NullToken;
1429 if(m_lookBackTokens.size() >= 2) {
1430 // The last token in the list is the current one. We want the previous one
1431 return m_lookBackTokens.at(m_lookBackTokens.size() - 2);
1432 }
1433 return NullToken;
1434 }
1435
1436 wxString PHPSourceFile::ReadFunctionReturnValueFromSignature()
1437 {
1438 bool cont = true;
1439 wxString type;
1440 phpLexerToken token;
1441
1442 while(cont && NextToken(token)) {
1443 switch(token.type) {
1444 case kPHP_T_SELF:
1445 case kPHP_T_ARRAY:
1446 return token.Text();
1447
1448 case kPHP_T_IDENTIFIER:
1449 type << token.Text();
1450 break;
1451
1452 case kPHP_T_NS_SEPARATOR:
1453 type << token.Text();
1454 break;
1455 default:
1456 // restore the token so next call to NextToken
1457 // will pick it up again
1458 UngetToken(token);
1459 cont = false;
1460 break;
1461 }
1462 }
1463
1464 type = MakeIdentifierAbsolute(type);
1465 return type;
1466 }
1467
1468 wxString PHPSourceFile::PrependCurrentScope(const wxString& className)
1469 {
1470 wxString currentScope = Namespace()->GetFullName();
1471 if(!currentScope.EndsWith("\\")) {
1472 currentScope << "\\";
1473 }
1474 return currentScope + className;
1475 }
1476
1477 wxString PHPSourceFile::MakeTypehintAbsolute(const wxString& type) { return DoMakeIdentifierAbsolute(type, true); }
1478
1479 wxString PHPSourceFile::DoMakeIdentifierAbsolute(const wxString& type, bool exactMatch)
1480 {
1481 if(m_converter) {
1482 return m_converter->MakeIdentifierAbsolute(type);
1483 }
1484
1485 static std::unordered_set<std::string> phpKeywords;
1486 if(phpKeywords.empty()) {
1487 // List taken from https://www.php.net/manual/en/language.types.intro.php
1488 // Native types
1489 phpKeywords.insert("bool");
1490 phpKeywords.insert("int");
1491 phpKeywords.insert("float");
1492 phpKeywords.insert("string");
1493 phpKeywords.insert("array");
1494 phpKeywords.insert("object");
1495 phpKeywords.insert("iterable");
1496 phpKeywords.insert("callable");
1497 phpKeywords.insert("null");
1498 phpKeywords.insert("mixed");
1499 phpKeywords.insert("void");
1500 // Types that are common in documentation
1501 phpKeywords.insert("boolean");
1502 phpKeywords.insert("integer");
1503 phpKeywords.insert("double");
1504 phpKeywords.insert("real");
1505 phpKeywords.insert("binery");
1506 phpKeywords.insert("resource");
1507 phpKeywords.insert("number");
1508 phpKeywords.insert("callback");
1509 }
1510 wxString typeWithNS(type);
1511 typeWithNS.Trim().Trim(false);
1512
1513 if(phpKeywords.count(type.ToStdString())) {
1514 // primitives, don't bother...
1515 return typeWithNS;
1516 }
1517
1518 if(typeWithNS.IsEmpty())
1519 return "";
1520
1521 // A fully qualified type? don't touch it
1522 if(typeWithNS.StartsWith("\\")) {
1523 return typeWithNS;
1524 }
1525
1526 // Handle 'use' cases:
1527 // use Zend\Form; // create an alias entry: Form => Zend\Form
1528 // class A extends Form\Form {}
1529 // The extends should be expanded to Zend\Form\Form
1530 if(typeWithNS.Contains("\\")) {
1531 wxString scopePart = typeWithNS.BeforeLast('\\');
1532 wxString className = typeWithNS.AfterLast('\\');
1533 if(m_aliases.find(scopePart) != m_aliases.end()) {
1534 typeWithNS.clear();
1535 typeWithNS << m_aliases.find(scopePart)->second << "\\" << className;
1536 // Remove duplicate NS separators
1537 typeWithNS.Replace("\\\\", "\\");
1538 if(!typeWithNS.StartsWith("\\")) {
1539 typeWithNS << "\\";
1540 }
1541 return typeWithNS;
1542 }
1543 }
1544
1545 // Use the alias table first
1546 if(m_aliases.find(type) != m_aliases.end()) {
1547 return m_aliases.find(type)->second;
1548 }
1549
1550 wxString ns = Namespace()->GetFullName();
1551 if(!ns.EndsWith("\\")) {
1552 ns << "\\";
1553 }
1554
1555 if(exactMatch && m_lookup && !typeWithNS.Contains("\\") && !m_lookup->ClassExists(ns + typeWithNS)) {
1556 // Only when "exactMatch" apply this logic, otherwise, we might be getting a partialy typed string
1557 // which we will not find by calling FindChild()
1558 typeWithNS.Prepend("\\"); // Use the global NS
1559 } else {
1560 typeWithNS.Prepend(ns);
1561 }
1562 return typeWithNS;
1563 }
1564
1565 const PHPEntityBase::List_t& PHPSourceFile::GetAllMatchesInOrder()
1566 {
1567 if(m_allMatchesInorder.empty()) {
1568 auto ns = Namespace();
1569 if(!ns) {
1570 return m_allMatchesInorder;
1571 }
1572 if(ns->GetChildren().empty()) {
1573 return m_allMatchesInorder;
1574 }
1575
1576 PHPEntityBase::List_t Q;
1577 Q.insert(Q.end(), ns->GetChildren().begin(), ns->GetChildren().end());
1578
1579 while(!Q.empty()) {
1580 auto cur = Q.front();
1581 Q.erase(Q.begin());
1582 m_allMatchesInorder.push_back(cur);
1583 if(!cur->GetChildren().empty()) {
1584 // we want to process these children on the next iteration
1585 // so we prepend them to the queue
1586 Q.reserve(Q.size() + cur->GetChildren().size());
1587 Q.insert(Q.begin(), cur->GetChildren().begin(), cur->GetChildren().end());
1588 }
1589 }
1590 }
1591 return m_allMatchesInorder;
1592 }