Debian Codegraph @

CodeLite/PHPEntityVariable.cpp

raw
1 #include "PHPEntityVariable.h"
2 #include "PHPScannerTokens.h"
3 #include "PHPEntityFunction.h"
4 #include "PHPEntityClass.h"
5 #include "PHPLookupTable.h"
6
7 PHPEntityVariable::PHPEntityVariable() {}
8
9 PHPEntityVariable::~PHPEntityVariable() {}
10
11 void PHPEntityVariable::PrintStdout(int indent) const
12 {
13 wxString indentString(' ', indent);
14 wxPrintf("%s%s: %s", indentString, IsMember() ? "Member" : "Variable", GetShortName());
15 if(!GetTypeHint().IsEmpty()) {
16 wxPrintf(", TypeHint: %s", GetTypeHint());
17 }
18 if(!GetExpressionHint().IsEmpty()) {
19 wxPrintf(", ExpressionHint: %s", GetExpressionHint());
20 }
21 if(IsReference()) {
22 wxPrintf(", Reference");
23 }
24 if(!GetDefaultValue().IsEmpty()) {
25 wxPrintf(", Default: %s", GetDefaultValue());
26 }
27
28 wxPrintf(", Ln. %d", GetLine());
29 wxPrintf("\n");
30
31 PHPEntityBase::List_t::const_iterator iter = m_children.begin();
32 for(; iter != m_children.end(); ++iter) {
33 (*iter)->PrintStdout(indent + 4);
34 }
35 }
36
37 void PHPEntityVariable::SetVisibility(int visibility)
38 {
39 switch(visibility) {
40 case kPHP_T_PUBLIC:
41 m_flags &= ~kVar_Private;
42 m_flags &= ~kVar_Protected;
43 m_flags |= kVar_Public;
44 break;
45 case kPHP_T_PROTECTED:
46 m_flags &= ~kVar_Private;
47 m_flags &= ~kVar_Public;
48 m_flags |= kVar_Protected;
49 break;
50 case kPHP_T_PRIVATE:
51 m_flags &= ~kVar_Public;
52 m_flags &= ~kVar_Protected;
53 m_flags |= kVar_Private;
54 break;
55 default:
56 break;
57 }
58 }
59
60 wxString PHPEntityVariable::ToFuncArgString() const
61 {
62 if(!IsFunctionArg()) {
63 return "";
64 }
65
66 wxString str;
67 if(!GetTypeHint().IsEmpty()) {
68 if(IsNullable()) {
69 str << "?";
70 }
71 str << GetTypeHint() << " ";
72 }
73
74 if(IsReference()) {
75 str << "&";
76 }
77
78 str << GetShortName();
79 if(!GetDefaultValue().IsEmpty()) {
80 str << " = " << GetDefaultValue();
81 }
82 return str;
83 }
84 void PHPEntityVariable::Store(PHPLookupTable* lookup)
85 {
86 if(IsFunctionArg() || IsMember() || IsDefine()) {
87 try {
88 wxSQLite3Database& db = lookup->Database();
89 wxSQLite3Statement statement = db.PrepareStatement(
90 "INSERT OR REPLACE INTO VARIABLES_TABLE VALUES (NULL, "
91 ":SCOPE_ID, :FUNCTION_ID, :NAME, :FULLNAME, :SCOPE, :TYPEHINT, :DEFAULT_VALUE, "
92 ":FLAGS, :DOC_COMMENT, :LINE_NUMBER, :FILE_NAME)");
93 wxLongLong functionId, scopeId;
94 if(IsFunctionArg()) {
95 functionId = Parent()->GetDbId();
96 } else {
97 functionId = -1;
98 }
99
100 if(IsMember() || IsDefine()) {
101 scopeId = Parent()->GetDbId();
102 } else {
103 scopeId = -1;
104 }
105 statement.Bind(statement.GetParamIndex(":SCOPE_ID"), scopeId);
106 statement.Bind(statement.GetParamIndex(":FUNCTION_ID"), functionId);
107 statement.Bind(statement.GetParamIndex(":NAME"), GetShortName());
108 statement.Bind(statement.GetParamIndex(":FULLNAME"), GetFullName());
109 statement.Bind(statement.GetParamIndex(":SCOPE"), GetScope());
110 statement.Bind(statement.GetParamIndex(":TYPEHINT"), GetTypeHint());
111 statement.Bind(statement.GetParamIndex(":DEFAULT_VALUE"), GetDefaultValue());
112 statement.Bind(statement.GetParamIndex(":FLAGS"), (int)GetFlags());
113 statement.Bind(statement.GetParamIndex(":DOC_COMMENT"), GetDocComment());
114 statement.Bind(statement.GetParamIndex(":LINE_NUMBER"), GetLine());
115 statement.Bind(statement.GetParamIndex(":FILE_NAME"), GetFilename().GetFullPath());
116 statement.ExecuteUpdate();
117 SetDbId(db.GetLastRowId());
118
119 } catch(wxSQLite3Exception& exc) {
120 wxUnusedVar(exc);
121 }
122 }
123 }
124
125 void PHPEntityVariable::FromResultSet(wxSQLite3ResultSet& res)
126 {
127 SetDbId(res.GetInt("ID"));
128 SetFullName(res.GetString("FULLNAME"));
129 SetShortName(res.GetString("NAME"));
130 SetTypeHint(res.GetString("TYPEHINT"));
131 SetFlags(res.GetInt("FLAGS"));
132 SetDocComment(res.GetString("DOC_COMMENT"));
133 SetLine(res.GetInt("LINE_NUMBER"));
134 SetFilename(res.GetString("FILE_NAME"));
135 SetDefaultValue(res.GetString("DEFAULT_VALUE"));
136 }
137
138 wxString PHPEntityVariable::GetScope() const
139 {
140 PHPEntityBase* parent = Parent();
141 if(parent && parent->Is(kEntityTypeFunction) && IsFunctionArg()) {
142 return parent->Cast<PHPEntityFunction>()->GetScope();
143
144 } else if(parent && parent->Is(kEntityTypeClass) && IsMember()) {
145 return parent->GetFullName();
146
147 } else if(parent && parent->Is(kEntityTypeNamespace) && IsDefine()) {
148 return parent->GetFullName();
149
150 } else {
151 return "";
152 }
153 }
154
155 wxString PHPEntityVariable::Type() const { return GetTypeHint(); }
156 bool PHPEntityVariable::Is(eEntityType type) const { return type == kEntityTypeVariable; }
157 wxString PHPEntityVariable::GetDisplayName() const { return GetFullName(); }
158
159 wxString PHPEntityVariable::GetNameNoDollar() const
160 {
161 wxString name = GetShortName();
162 if(name.StartsWith("$")) {
163 name.Remove(0, 1);
164 }
165 name.Trim().Trim(false);
166 return name;
167 }
168 wxString PHPEntityVariable::FormatPhpDoc(const CommentConfigData& data) const
169 {
170 wxString doc;
171 doc << data.GetCommentBlockPrefix() << "\n"
172 << " * @var " << GetTypeHint() << "\n"
173 << " */";
174 return doc;
175 }
176
177 wxString PHPEntityVariable::ToTooltip() const
178 {
179 if(IsConst() && !GetDefaultValue().IsEmpty()) {
180 return GetDefaultValue();
181 } else {
182 return wxEmptyString;
183 }
184 }
185
186 void PHPEntityVariable::FromJSON(const JSONItem& json)
187 {
188 BaseFromJSON(json);
189 m_typeHint = json.namedObject("type-hint").toString();
190 m_expressionHint = json.namedObject("expr-hint").toString();
191 m_defaultValue = json.namedObject("defaultValue").toString();
192 }
193
194 JSONItem PHPEntityVariable::ToJSON() const
195 {
196 JSONItem json = BaseToJSON("v"); // type variable
197 json.addProperty("type-hint", m_typeHint);
198 json.addProperty("expr-hint", m_expressionHint);
199 json.addProperty("defaultValue", m_defaultValue);
200 return json;
201 }