| 1 |
#include "PHPDocVisitor.h" |
| 2 |
#include "PHPEntityBase.h" |
| 3 |
#include "PHPDocComment.h" |
| 4 |
#include "PHPEntityFunction.h" |
| 5 |
#include "PHPEntityVariable.h" |
| 6 |
#include <algorithm> |
| 7 |
|
| 8 |
PHPDocVisitor::PHPDocVisitor(PHPSourceFile& sourceFile, const std::vector<phpLexerToken>& comments) |
| 9 |
: m_sourceFile(sourceFile) |
| 10 |
{ |
| 11 |
for(size_t i = 0; i < comments.size(); ++i) { |
| 12 |
m_comments.insert(std::make_pair(comments.at(i).endLineNumber, comments.at(i))); |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
PHPDocVisitor::~PHPDocVisitor() {} |
| 17 |
|
| 18 |
void PHPDocVisitor::OnEntity(PHPEntityBase::Ptr_t entity) |
| 19 |
{ |
| 20 |
// Locate a comment for this entity |
| 21 |
entity->SetFilename(m_sourceFile.GetFilename()); |
| 22 |
|
| 23 |
if(!entity->GetDocComment().IsEmpty()) { |
| 24 |
// PHPDoc was already assigned to this entity during the parsing phase |
| 25 |
if(entity->Is(kEntityTypeClass)) { |
| 26 |
// Process @property tags here |
| 27 |
PHPDocComment docComment(m_sourceFile, entity->GetDocComment()); |
| 28 |
if(!docComment.GetProperties().empty()) { |
| 29 |
// Got some @properties |
| 30 |
std::for_each(docComment.GetProperties().begin(), docComment.GetProperties().end(), |
| 31 |
[&](PHPDocComment::Property::Map_t::value_type& p) { |
| 32 |
PHPEntityBase::Ptr_t child = entity->FindChild(p.second.name); |
| 33 |
if(!child) { |
| 34 |
// No child of this type, create a new property and add it |
| 35 |
child.Reset(new PHPEntityVariable()); |
| 36 |
child->SetFilename(m_sourceFile.GetFilename()); |
| 37 |
child->SetLine(entity->GetLine()); |
| 38 |
child->Cast<PHPEntityVariable>()->SetTypeHint(p.second.type); |
| 39 |
child->Cast<PHPEntityVariable>()->SetFlag(kVar_Member); // Member variable |
| 40 |
child->Cast<PHPEntityVariable>()->SetFlag(kVar_Public); // Public access |
| 41 |
child->SetShortName(p.second.name); |
| 42 |
child->SetFullName(p.second.name); |
| 43 |
entity->AddChild(child); |
| 44 |
} |
| 45 |
}); |
| 46 |
} else if(!docComment.GetMethods().empty()) { |
| 47 |
std::for_each(docComment.GetMethods().begin(), docComment.GetMethods().end(), |
| 48 |
[&](PHPEntityBase::Ptr_t method) { entity->AddChild(method); }); |
| 49 |
} |
| 50 |
} |
| 51 |
} else { |
| 52 |
// search for the comment placed at the top of the variable |
| 53 |
// this is why we use here -1 |
| 54 |
int lineNum = (entity->GetLine() - 1); |
| 55 |
|
| 56 |
// for debugging purposes |
| 57 |
wxString entityName = entity->GetShortName(); |
| 58 |
wxUnusedVar(entityName); |
| 59 |
|
| 60 |
std::map<int, phpLexerToken>::iterator iter = m_comments.find(lineNum); |
| 61 |
if(iter == m_comments.end()) { |
| 62 |
// try to locate a comment on the same line |
| 63 |
++lineNum; |
| 64 |
iter = m_comments.find(lineNum); |
| 65 |
} |
| 66 |
|
| 67 |
if(iter != m_comments.end()) { |
| 68 |
|
| 69 |
// we got a match |
| 70 |
entity->SetDocComment(iter->second.Text()); |
| 71 |
m_comments.erase(iter); |
| 72 |
|
| 73 |
PHPDocComment docComment(m_sourceFile, entity->GetDocComment()); |
| 74 |
if(entity->Is(kEntityTypeFunction) && !docComment.GetReturn().IsEmpty()) { |
| 75 |
entity->Cast<PHPEntityFunction>()->SetReturnValue(docComment.GetReturn()); |
| 76 |
if (docComment.IsReturnNullable()) { |
| 77 |
entity->Cast<PHPEntityFunction>()->SetFlag(kFunc_ReturnNullable); |
| 78 |
} |
| 79 |
} else if(entity->Is(kEntityTypeVariable) && !entity->Cast<PHPEntityVariable>()->IsFunctionArg()) { |
| 80 |
// A global variable, const or a member |
| 81 |
entity->Cast<PHPEntityVariable>()->SetTypeHint(docComment.GetVar()); |
| 82 |
} |
| 83 |
|
| 84 |
} else if(entity->Is(kEntityTypeVariable) && entity->Parent() && entity->Parent()->Is(kEntityTypeFunction) && |
| 85 |
entity->Cast<PHPEntityVariable>()->IsFunctionArg()) { |
| 86 |
// A function argument |
| 87 |
PHPDocComment docComment(m_sourceFile, entity->Parent()->GetDocComment()); |
| 88 |
wxString typeHint = docComment.GetParam(entity->GetFullName()); |
| 89 |
const wxString& currentTypeHint = entity->Cast<PHPEntityVariable>()->GetTypeHint(); |
| 90 |
if(!typeHint.IsEmpty() && currentTypeHint.IsEmpty()) { |
| 91 |
// The typehint of a functin argument should have more value than the one provided |
| 92 |
// in the documentation |
| 93 |
entity->Cast<PHPEntityVariable>()->SetTypeHint(typeHint); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |