Debian Codegraph @

CodeLite/PHPEntityFunction.cpp

raw
1 #include "PHPEntityFunction.h"
2
3 #include "PHPEntityVariable.h"
4 #include "PHPLookupTable.h"
5 #include "commentconfigdata.h"
6 #include "file_logger.h"
7
8 PHPEntityFunction::PHPEntityFunction() {}
9
10 PHPEntityFunction::~PHPEntityFunction() {}
11
12 void PHPEntityFunction::PrintStdout(int indent) const
13 {
14 wxString indentString(' ', indent);
15 // Print the indentation
16 wxPrintf("%sFunction: %s%s", indentString, GetShortName(), GetSignature());
17 wxPrintf(", (%s:%d)\n", GetFilename().GetFullPath(), GetLine());
18 if(!m_children.empty()) {
19 wxPrintf("%sLocals:\n", indentString);
20 PHPEntityBase::List_t::const_iterator iter = m_children.begin();
21 for(; iter != m_children.end(); ++iter) {
22 (*iter)->PrintStdout(indent + 4);
23 }
24 }
25 }
26
27 wxString PHPEntityFunction::GetSignature() const
28 {
29 if(!m_strSignature.IsEmpty()) {
30 return m_strSignature;
31 } else {
32
33 wxString strSignature = "(";
34 PHPEntityBase::List_t::const_iterator iter = m_children.begin();
35 for(; iter != m_children.end(); ++iter) {
36 PHPEntityVariable* var = (*iter)->Cast<PHPEntityVariable>();
37 if(var && var->IsFunctionArg()) {
38 strSignature << var->ToFuncArgString() << ", ";
39 } else {
40 break;
41 }
42 }
43 if(strSignature.EndsWith(", ")) {
44 strSignature.RemoveLast(2);
45 }
46 strSignature << ")";
47 if(!GetReturnValue().IsEmpty()) {
48 strSignature << ": ";
49 if(HasFlag(kFunc_ReturnNullable)) {
50 strSignature << "?";
51 }
52 strSignature << GetReturnValue();
53 }
54 return strSignature;
55 }
56 }
57
58 void PHPEntityFunction::Store(PHPLookupTable* lookup)
59 {
60 wxString fullname;
61 fullname << GetScope() << "\\" << GetShortName();
62 while(fullname.Replace("\\\\", "\\")) {}
63
64 try {
65 wxSQLite3Database& db = lookup->Database();
66 wxSQLite3Statement statement = db.PrepareStatement(
67 "INSERT OR REPLACE INTO FUNCTION_TABLE VALUES(NULL, :SCOPE_ID, :NAME, :FULLNAME, :SCOPE, :SIGNATURE, "
68 ":RETURN_VALUE, :FLAGS, :DOC_COMMENT, :LINE_NUMBER, :FILE_NAME)");
69 statement.Bind(statement.GetParamIndex(":SCOPE_ID"), Parent()->GetDbId());
70 statement.Bind(statement.GetParamIndex(":NAME"), GetShortName());
71 statement.Bind(statement.GetParamIndex(":FULLNAME"), fullname);
72 statement.Bind(statement.GetParamIndex(":SCOPE"), GetScope());
73 statement.Bind(statement.GetParamIndex(":SIGNATURE"), GetSignature());
74 statement.Bind(statement.GetParamIndex(":RETURN_VALUE"), GetReturnValue());
75 statement.Bind(statement.GetParamIndex(":FLAGS"), (int)GetFlags());
76 statement.Bind(statement.GetParamIndex(":DOC_COMMENT"), GetDocComment());
77 statement.Bind(statement.GetParamIndex(":LINE_NUMBER"), GetLine());
78 statement.Bind(statement.GetParamIndex(":FILE_NAME"), GetFilename().GetFullPath());
79 statement.ExecuteUpdate();
80 SetDbId(db.GetLastRowId());
81
82 } catch(wxSQLite3Exception& exc) {
83 clWARNING() << "PHPEntityFunction::Store:" << exc.GetMessage() << endl;
84 }
85 }
86
87 void PHPEntityFunction::FromResultSet(wxSQLite3ResultSet& res)
88 {
89 SetDbId(res.GetInt("ID"));
90 SetFullName(res.GetString("FULLNAME"));
91 SetShortName(res.GetString("NAME"));
92 m_strSignature = res.GetString("SIGNATURE");
93 m_strReturnValue = res.GetString("RETURN_VALUE");
94 SetFlags(res.GetInt("FLAGS"));
95 SetDocComment(res.GetString("DOC_COMMENT"));
96 SetLine(res.GetInt("LINE_NUMBER"));
97 SetFilename(res.GetString("FILE_NAME"));
98 }
99
100 wxString PHPEntityFunction::GetScope() const
101 {
102 if(Parent()) {
103 return Parent()->GetFullName();
104 }
105 return "";
106 }
107
108 wxString PHPEntityFunction::Type() const { return GetReturnValue(); }
109 bool PHPEntityFunction::Is(eEntityType type) const { return type == kEntityTypeFunction; }
110 wxString PHPEntityFunction::GetDisplayName() const { return wxString() << GetShortName() << GetSignature(); }
111 wxString PHPEntityFunction::FormatPhpDoc(const CommentConfigData& data) const
112 {
113 bool hasParams = false;
114 wxString doc;
115 doc << data.GetCommentBlockPrefix() << "\n"
116 << " * @brief \n";
117 PHPEntityBase::List_t::const_iterator iter = m_children.begin();
118 for(; iter != m_children.end(); ++iter) {
119 const PHPEntityVariable* var = (*iter)->Cast<PHPEntityVariable>();
120 if(var) {
121 hasParams = true;
122 doc << " * @param ";
123 if(var->IsNullable() || var->GetDefaultValue().Matches("null")) {
124 doc << "?";
125 }
126 doc << (var->GetTypeHint().IsEmpty() ? "mixed" : var->GetTypeHint()) << " " << var->GetFullName();
127 if(!var->GetDefaultValue().IsEmpty()) {
128 doc << " [" << var->GetDefaultValue() << "]";
129 }
130 doc << " \n";
131 }
132 }
133 if(!GetShortName().Matches("__construct")) {
134 if(hasParams) {
135 doc << " *\n";
136 }
137 doc << " * @return ";
138 if(HasFlag(kFunc_ReturnNullable)) {
139 doc << "?";
140 }
141 doc << (GetReturnValue().IsEmpty() ? "mixed" : GetReturnValue()) << " \n";
142 }
143 doc << " */";
144 return doc;
145 }
146
147 wxString PHPEntityFunction::GetFullPath() const
148 {
149 wxString fullpath = GetFullName();
150 size_t where = fullpath.rfind(GetShortName());
151 if(where != wxString::npos) {
152 if(where > 0) {
153 fullpath = fullpath.Mid(0, where - 1);
154 if(fullpath.IsEmpty()) {
155 fullpath << "\\";
156 } else {
157 fullpath << "::";
158 }
159 }
160 fullpath << GetShortName();
161 }
162 fullpath << GetSignature();
163 return fullpath;
164 }
165
166 void PHPEntityFunction::FromJSON(const JSONItem& json)
167 {
168 BaseFromJSON(json);
169 m_strReturnValue = json.namedObject("returns").toString();
170 m_strSignature = json.namedObject("signature").toString();
171 }
172
173 JSONItem PHPEntityFunction::ToJSON() const
174 {
175 JSONItem json = BaseToJSON("f");
176 json.addProperty("returns", m_strReturnValue);
177 json.addProperty("signature", m_strSignature);
178 return json;
179 }
180
181 wxString PHPEntityFunction::ToTooltip() const
182 {
183 wxString tip;
184 tip << GetShortName() << GetSignature();
185 if(!GetReturnValue().IsEmpty()) {
186 tip << " : " << GetReturnValue();
187 }
188 return tip;
189 }