From 8de3f033801c9a904efacf8547a0a2f6774bbe0a Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Fri, 3 May 2024 14:32:14 +0300 Subject: [PATCH] base solution. added php aliases, quickfixes & reports --- src/linter/block_linter.go | 7 +++ src/linter/quickfix.go | 13 ++++ src/linter/report.go | 9 +++ src/php/php_aliases.go | 123 +++++++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 src/php/php_aliases.go diff --git a/src/linter/block_linter.go b/src/linter/block_linter.go index bd7bc368..19502618 100644 --- a/src/linter/block_linter.go +++ b/src/linter/block_linter.go @@ -11,6 +11,7 @@ import ( "github.com/VKCOM/noverify/src/ir/phpcore" "github.com/VKCOM/noverify/src/linter/autogen" "github.com/VKCOM/noverify/src/meta" + "github.com/VKCOM/noverify/src/php" "github.com/VKCOM/noverify/src/quickfix" "github.com/VKCOM/noverify/src/solver" "github.com/VKCOM/noverify/src/types" @@ -1005,6 +1006,12 @@ func (b *blockLinter) checkFunctionCall(e *ir.FunctionCallExpr) { call := resolveFunctionCall(b.walker.ctx.sc, b.classParseState(), b.walker.ctx.customTypes, e) fqName := call.funcName + var phpAlias = php.Aliases[fqName] + if phpAlias != "" { + b.report(e, LevelWarning, "phpAliases", "use %s instead of '%s'", phpAlias) + b.walker.r.addQuickFix("phpAliases", b.quickfix.PhpAliasesReplace(e.Function.(*ir.Name), phpAlias)) + } + if call.isClosure { varName := strings.TrimPrefix(fqName, `\`) b.walker.untrackVarName(varName) diff --git a/src/linter/quickfix.go b/src/linter/quickfix.go index ef83aa8a..33c3979c 100644 --- a/src/linter/quickfix.go +++ b/src/linter/quickfix.go @@ -47,6 +47,19 @@ func (g *QuickFixGenerator) NullForNotNullableProperty(prop *ir.PropertyStmt) qu } } +func (g *QuickFixGenerator) PhpAliasesReplace(prop *ir.Name, masterFunction string) quickfix.TextEdit { + /*from := prop.Position.StartPos + to := prop.Position.EndPos + + withoutAssign := g.file.Contents()[from:to]*/ + + return quickfix.TextEdit{ + StartPos: prop.Position.StartPos, + EndPos: prop.Position.EndPos, + Replacement: masterFunction, + } +} + func (g *QuickFixGenerator) GetType(node ir.Node, isFunctionName, nodeText string, isNegative bool) quickfix.TextEdit { pos := ir.GetPosition(node) diff --git a/src/linter/report.go b/src/linter/report.go index d1914da3..3eafeb57 100644 --- a/src/linter/report.go +++ b/src/linter/report.go @@ -44,6 +44,15 @@ return -9223372036854775808;`, After: `return PHP_INT_MIN;`, }, + { + Name: "phpAliases", + Default: true, + Quickfix: true, + Comment: `Report php aliases functions.`, + Before: `join("", []);`, + After: `implode("", []);`, + }, + { Name: "discardExpr", Default: true, diff --git a/src/php/php_aliases.go b/src/php/php_aliases.go new file mode 100644 index 00000000..3712e1e0 --- /dev/null +++ b/src/php/php_aliases.go @@ -0,0 +1,123 @@ +package php + +var Aliases = map[string]string{ + "\\chop": "rtrim", + "\\close": "closedir", + "\\com_get": "com_propget", + "\\com_propset": "com_propput", + "\\com_set": "com_propput", + "\\die": "exit", + "\\diskfreespace": "disk_free_space", + "\\doubleval": "floatval", + "\\fputs": "fwrite", + "\\gzputs": "gzwrite", + "\\i18n_convert": "mb_convert_encoding", + "\\i18n_discover_encoding": "mb_detect_encoding", + "\\i18n_http_input": "mb_http_input", + "\\i18n_http_output": "mb_http_output", + "\\i18n_internal_encoding": "mb_internal_encoding", + "\\i18n_ja_jp_hantozen": "mb_convert_kana", + "\\i18n_mime_header_decode": "mb_decode_mimeheader", + "\\i18n_mime_header_encode": "mb_encode_mimeheader", + "\\imap_create": "imap_createmailbox", + "\\imap_fetchtext": "imap_body", + "\\imap_getmailboxes": "imap_list_full", + "\\imap_getsubscribed": "imap_lsub_full", + "\\imap_header": "imap_headerinfo", + "\\imap_listmailbox": "imap_list", + "\\imap_listsubscribed": "imap_lsub", + "\\imap_rename": "imap_renamemailbox", + "\\imap_scan": "imap_listscan", + "\\imap_scanmailbox": "imap_listscan", + "\\ini_alter": "ini_set", + "\\is_double": "is_float", + "\\is_integer": "is_int", + "\\is_long": "is_int", + "\\is_real": "is_float", + "\\is_writeable": "is_writable", + "\\join": "implode", + "\\key_exists": "array_key_exists", + "\\ldap_close": "ldap_unbind", + "\\mbstrcut": "mb_strcut", + "\\mbstrlen": "mb_strlen", + "\\mbstrpos": "mb_strpos", + "\\mbstrrpos": "mb_strrpos", + "\\mbsubstr": "mb_substr", + "\\mysql": "mysql_db_query", + "\\mysql_createdb": "mysql_create_db", + "\\mysql_db_name": "mysql_result", + "\\mysql_dbname": "mysql_result", + "\\mysql_dropdb": "mysql_drop_db", + "\\mysql_fieldflags": "mysql_field_flags", + "\\mysql_fieldlen": "mysql_field_len", + "\\mysql_fieldname": "mysql_field_name", + "\\mysql_fieldtable": "mysql_field_table", + "\\mysql_fieldtype": "mysql_field_type", + "\\mysql_freeresult": "mysql_free_result", + "\\mysql_listdbs": "mysql_list_dbs", + "\\mysql_listfields": "mysql_list_fields", + "\\mysql_listtables": "mysql_list_tables", + "\\mysql_numfields": "mysql_num_fields", + "\\mysql_numrows": "mysql_num_rows", + "\\mysql_selectdb": "mysql_select_db", + "\\mysql_tablename": "mysql_result", + "\\ociassignelem": "OCICollection::assignElem", + "\\ocibindbyname": "oci_bind_by_name", + "\\ocicancel": "oci_cancel", + "\\ocicloselob": "OCILob::close", + "\\ocicollappend": "OCICollection::append", + "\\ocicollassign": "OCICollection::assign", + "\\ocicollmax": "OCICollection::max", + "\\ocicollsize": "OCICollection::size", + "\\ocicolltrim": "OCICollection::trim", + "\\ocicolumnisnull": "oci_field_is_null", + "\\ocicolumnname": "oci_field_name", + "\\ocicolumnprecision": "oci_field_precision", + "\\ocicolumnscale": "oci_field_scale", + "\\ocicolumnsize": "oci_field_size", + "\\ocicolumntype": "oci_field_type", + "\\ocicolumntyperaw": "oci_field_type_raw", + "\\ocicommit": "oci_commit", + "\\ocidefinebyname": "oci_define_by_name", + "\\ocierror": "oci_error", + "\\ociexecute": "oci_execute", + "\\ocifetch": "oci_fetch", + "\\ocifetchinto": "oci_fetch_object", + "\\ocifetchstatement": "oci_fetch_all", + "\\ocifreecollection": "OCICollection::free", + "\\ocifreecursor": "oci_free_statement", + "\\ocifreedesc": "oci_free_descriptor", + "\\ocifreestatement": "oci_free_statement", + "\\ocigetelem": "OCICollection::getElem", + "\\ociinternaldebug": "oci_internal_debug", + "\\ociloadlob": "OCILob::load", + "\\ocilogon": "oci_connect", + "\\ocinewcollection": "oci_new_collection", + "\\ocinewcursor": "oci_new_cursor", + "\\ocinewdescriptor": "oci_new_descriptor", + "\\ocinlogon": "oci_new_connect", + "\\ocinumcols": "oci_num_fields", + "\\ociparse": "oci_parse", + "\\ocipasswordchange": "oci_password_change", + "\\ociplogon": "oci_pconnect", + "\\ociresult": "oci_result", + "\\ocirollback": "oci_rollback", + "\\ocisavelob": "OCILob::save", + "\\ocisavelobfile": "OCILob::import", + "\\ociserverversion": "oci_server_version", + "\\ocisetprefetch": "oci_set_prefetch", + "\\ocistatementtype": "oci_statement_type", + "\\ociwritelobtofile": "OCILob::export", + "\\ociwritetemporarylob": "OCILob::writeTemporary", + "\\odbc_do": "odbc_exec", + "\\odbc_field_precision": "odbc_field_len", + "\\pg_clientencoding": "pg_client_encoding", + "\\pg_setclientencoding": "pg_set_client_encoding", + "\\pos": "current", + "\\recode": "recode_string", + "\\show_source": "highlight_file", + "\\sizeof": "count", + "\\snmpwalkoid": "snmprealwalk", + "\\strchr": "strstr", + "\\xptr_new_context": "xpath_new_context", +}