| 1 |
#include "PHPDocComment.h" |
| 2 |
#include "PHPDocParam.h" |
| 3 |
#include "PHPDocProperty.h" |
| 4 |
#include "PHPDocVar.h" |
| 5 |
#include "wxStringHash.h" |
| 6 |
#include <algorithm> |
| 7 |
#include <wx/regex.h> |
| 8 |
#include <wx/tokenzr.h> |
| 9 |
|
| 10 |
PHPDocComment::PHPDocComment(PHPSourceFile& sourceFile, const wxString& comment) |
| 11 |
: m_sourceFile(sourceFile) |
| 12 |
, m_comment(comment) |
| 13 |
, m_returnNullable(false) |
| 14 |
{ |
| 15 |
static std::unordered_set<wxString> nativeTypes; |
| 16 |
if(nativeTypes.empty()) { |
| 17 |
// List taken from https://www.php.net/manual/en/language.types.intro.php |
| 18 |
// Native types |
| 19 |
nativeTypes.insert("bool"); |
| 20 |
nativeTypes.insert("int"); |
| 21 |
nativeTypes.insert("float"); |
| 22 |
nativeTypes.insert("string"); |
| 23 |
nativeTypes.insert("array"); |
| 24 |
nativeTypes.insert("object"); |
| 25 |
nativeTypes.insert("iterable"); |
| 26 |
nativeTypes.insert("callable"); |
| 27 |
nativeTypes.insert("null"); |
| 28 |
nativeTypes.insert("mixed"); |
| 29 |
nativeTypes.insert("void"); |
| 30 |
// Types that are common in documentation |
| 31 |
nativeTypes.insert("boolean"); |
| 32 |
nativeTypes.insert("integer"); |
| 33 |
nativeTypes.insert("double"); |
| 34 |
nativeTypes.insert("real"); |
| 35 |
nativeTypes.insert("binery"); |
| 36 |
nativeTypes.insert("resource"); |
| 37 |
nativeTypes.insert("number"); |
| 38 |
nativeTypes.insert("callback"); |
| 39 |
} |
| 40 |
|
| 41 |
static wxRegEx reReturnStatement(wxT("@(return)[ \t]+([\\?\\a-zA-Z_]{1}[\\|\\a-zA-Z0-9_]*)")); |
| 42 |
if(reReturnStatement.IsValid() && reReturnStatement.Matches(m_comment)) { |
| 43 |
wxString returnValue = reReturnStatement.GetMatch(m_comment, 2); |
| 44 |
if(returnValue.StartsWith("?")) { |
| 45 |
returnValue.Remove(0, 1); |
| 46 |
m_returnNullable = true; |
| 47 |
} |
| 48 |
|
| 49 |
wxArrayString types = ::wxStringTokenize(returnValue, "|", wxTOKEN_STRTOK); |
| 50 |
if(types.size() > 1) { |
| 51 |
// Multiple return types, guess the best match |
| 52 |
wxString bestMatch; |
| 53 |
for(size_t i = 0; i < types.size(); ++i) { |
| 54 |
if(bestMatch.IsEmpty() && (nativeTypes.count(types.Item(i)) == 0)) { |
| 55 |
bestMatch = types.Item(i); |
| 56 |
break; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if(bestMatch.IsEmpty()) { |
| 61 |
bestMatch = types.Item(0); // just get the first match |
| 62 |
} |
| 63 |
m_returnValue = sourceFile.MakeIdentifierAbsolute(bestMatch); |
| 64 |
} else if(types.size() == 1) { |
| 65 |
m_returnValue = sourceFile.MakeIdentifierAbsolute(types.Item(0)); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
PHPDocVar var(sourceFile, m_comment); |
| 70 |
if(var.IsOk()) { |
| 71 |
m_varType = var.GetType(); |
| 72 |
m_varName = var.GetName(); |
| 73 |
} |
| 74 |
|
| 75 |
// @param <TYPE> <NAME> |
| 76 |
if(m_comment.Contains("@param")) { |
| 77 |
PHPDocParam params(sourceFile, m_comment); |
| 78 |
const PHPDocParam::Vec_t& paramsVec = params.Parse(); |
| 79 |
std::for_each(paramsVec.begin(), paramsVec.end(), [&](const std::pair<wxString, wxString>& p) { |
| 80 |
m_paramsArr.Add(p.second); |
| 81 |
m_params.insert(p); |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
// @property-read, @property-write, @property |
| 86 |
if(m_comment.Contains("@property")) { |
| 87 |
PHPDocProperty prop(sourceFile, m_comment); |
| 88 |
const PHPDocProperty::Tuple_t& properties = prop.ParseParams(); |
| 89 |
std::for_each(properties.begin(), properties.end(), [&](const PHPDocProperty::Tuple_t::value_type& vt) { |
| 90 |
PHPDocComment::Property property; |
| 91 |
property.type = std::get<0>(vt); |
| 92 |
property.name = std::get<1>(vt); |
| 93 |
property.desc = std::get<2>(vt); |
| 94 |
m_properties.insert(std::make_pair(property.name, property)); |
| 95 |
}); |
| 96 |
} |
| 97 |
|
| 98 |
// Attempt to parse and resolve @method entries in the PHPDoc |
| 99 |
ProcessMethods(); |
| 100 |
} |
| 101 |
|
| 102 |
PHPDocComment::~PHPDocComment() {} |
| 103 |
|
| 104 |
const wxString& PHPDocComment::GetParam(size_t n) const |
| 105 |
{ |
| 106 |
if(n >= m_paramsArr.GetCount()) { |
| 107 |
static wxString emptyString; |
| 108 |
return emptyString; |
| 109 |
} |
| 110 |
return m_paramsArr.Item(n); |
| 111 |
} |
| 112 |
|
| 113 |
const wxString& PHPDocComment::GetReturn() const { return m_returnValue; } |
| 114 |
|
| 115 |
const bool PHPDocComment::IsReturnNullable() const { return m_returnNullable; } |
| 116 |
|
| 117 |
const wxString& PHPDocComment::GetVar() const { return m_varType; } |
| 118 |
|
| 119 |
const wxString& PHPDocComment::GetParam(const wxString& name) const |
| 120 |
{ |
| 121 |
if(m_params.count(name) == 0) { |
| 122 |
static wxString emptyString; |
| 123 |
return emptyString; |
| 124 |
} |
| 125 |
return m_params.find(name)->second; |
| 126 |
} |
| 127 |
|
| 128 |
void PHPDocComment::ProcessMethods() |
| 129 |
{ |
| 130 |
// The phpdoc for method does not conform to the PHP syntax. |
| 131 |
// We need to alter the signature so we can use our parse to parse |
| 132 |
// the signature |
| 133 |
// @method syntax is: |
| 134 |
// @method [return type] [name]([[type] [parameter]<, ...>]) [<description>] |
| 135 |
// While PHP's syntax is: |
| 136 |
// function [name] ([[type] [parameter]<, ...>]) [ : return_type ] |
| 137 |
PHPDocProperty property(m_sourceFile, m_comment); |
| 138 |
const PHPDocProperty::Tuple_t& methods = property.ParseMethods(); |
| 139 |
std::for_each(methods.begin(), methods.end(), [&](const PHPDocProperty::Tuple_t::value_type& vt) { |
| 140 |
wxString returnType = std::get<0>(vt); |
| 141 |
wxString methodName = std::get<1>(vt); |
| 142 |
wxString signature = std::get<2>(vt); |
| 143 |
|
| 144 |
wxString strBuffer; |
| 145 |
strBuffer << "<?php function " << methodName << signature; |
| 146 |
if(!returnType.IsEmpty()) { |
| 147 |
strBuffer << " : " << returnType << " "; |
| 148 |
} |
| 149 |
strBuffer << " {} "; |
| 150 |
|
| 151 |
PHPSourceFile buffer(strBuffer, NULL); |
| 152 |
buffer.SetTypeAbsoluteConverter(&m_sourceFile); |
| 153 |
buffer.Parse(); |
| 154 |
|
| 155 |
if(!buffer.CurrentScope()->GetChildren().empty()) { |
| 156 |
PHPEntityBase::Ptr_t func = *buffer.CurrentScope()->GetChildren().begin(); |
| 157 |
if(func && func->Is(kEntityTypeFunction)) { |
| 158 |
if(func->Parent()) { |
| 159 |
func->Parent()->RemoveChild(func); |
| 160 |
} |
| 161 |
m_methods.push_back(func); |
| 162 |
} |
| 163 |
} |
| 164 |
}); |
| 165 |
} |