Skip to content

Commit

Permalink
base solution. added php aliases, quickfixes & reports
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed May 3, 2024
1 parent ccaafc7 commit 8de3f03
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/linter/block_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/linter/quickfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 51 in src/linter/quickfix.go

View workflow job for this annotation

GitHub Actions / Build

commentedOutCode: may want to remove commented-out code (gocritic)
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)

Expand Down
9 changes: 9 additions & 0 deletions src/linter/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
123 changes: 123 additions & 0 deletions src/php/php_aliases.go
Original file line number Diff line number Diff line change
@@ -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",
}

0 comments on commit 8de3f03

Please sign in to comment.