Detection Method, Device, Storage Medium and Electronic Device for Code Parameters
Through reflection and string matching strategies, the function parameters in the php code are identified, which solves the problem of inaccurate parameter detection in the php version upgrade, and realizes automated and accurate parameter detection, which improves detection efficiency and accuracy.
Patent Information
- Application Number
- CN202211718826.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-29
- Publication Date
- 2025-06-17
- Estimated Expiration
- 2042-12-29
AI Technical Summary
During the process of upgrading the php programming language from version 7.0 to version 7.1 and above, fatal errors caused by too few passed parameters are difficult to detect, and the detection effect of the existing technology is poor, especially in the complex dynamic object generation and inheritance relationships.
The functions in the target programming code are identified through the reflection mechanism, the required parameter data is counted, and the actual parameter data is determined using the string matching strategy, the required parameter number is compared, and the missing parameters are automatically detected, avoiding the missed detection problem of executing code or static tools.
It improves the accuracy of passing parameter detection during the php version upgrade process, reduces errors caused by inconsistent parameters, and realizes automated and accurate parameter detection.
Smart Images

Figure CN115964293B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technologies, and in particular, to a method, apparatus, storage medium, and electronic device for detecting code parameters. Background Art
[0002] Currently, the PHP (Hypertext Preprocessor, a general-purpose open-source scripting language) programming language is becoming increasingly strict. When upgrading, compatibility issues are faced. In the case of too few passed parameters in compatibility issues, there is no impact when passing too few parameters in the PHP 7.0 version. However, in the PHP 7.1 version and above, an error will be reported when passing too few parameters. When an error is reported due to too few passed parameters, for example: when defining a parameter $param, there is no default value for $param in this definition, but an error will be reported if the default value is not passed when calling, and a fatal error (such as: Fatal error) will occur. Specifically, such as: Fatal error: Uncaught ArgumentCountError: Too few arguments to function test(), 0 passed in %s on line %d and exactly 1 expected in %s:%d. However, if a default value is written (such as: function test($param = 123) {}), no error will be reported. When the project is upgraded from the PHP 7.0 version and earlier to the PHP 7.1 version and later, there are inconsistent parameters in the old code, and it is easy to have errors reported everywhere.
[0003] In the related art, for how to check and discover such error reporting problems, generally, the following two methods are used for checking;
[0004] Method 1: Detect and modify by executing business code, that is, modify where the error is reported. However, due to a large amount of code and many business logic conditions, this method may not necessarily execute the problematic code, and it is very easy to miss some modifications.
[0005] Method 2: Use a static code checking tool (for example: phpstan) to batch-check such errors. However, for code with clear inheritance and other dependency relationships, such as phpstan can check the matching situation of the number of parameters. But for many scenarios, the method definition on which it depends cannot be found. Specifically: an object can be created by multiple classes. The unique source cannot be found, such as:
[0006] function aaa($a, $b, $c) {} is defined in classA
[0007] $classObj = null;
[0008] if ($value === "v1") {
[0009] $classObj = new classA();
[0010] }
[0011] else {
[0012] $classObj = new classB();
[0013] }
[0014] $classObj->aaa(1, 2);
[0015] However, here phpstan cannot find the exact source of classA, so it cannot detect the problem.
[0016] An object generated using reflection. The class path of the object is dynamically generated by a configuration file, etc., such as:
[0017] function aaa($a, $b, $c) {} is defined in classA
[0018] $reflector = new ReflectionClass("class"."A");
[0019] $classObj = $reflector->newInstance();
[0020] $classObj->aaa(1, 2);
[0021] However, here phpstan cannot find the exact source of classA and cannot detect the problem.
[0022] Regarding the above problems, no effective solution has been proposed yet. Summary of the Invention
[0023] Embodiments of the present invention provide a method, apparatus, storage medium, and electronic device for detecting code parameters, so as to at least solve the technical problem of poor detection effect in the related art when detecting whether the passed parameters of the programming language php undergoing version upgrade are too few.
[0024] According to one aspect of an embodiment of the present invention, a method for detecting code parameters is provided, including: in response to a parameter detection request for target programming code, extracting a plurality of target classes in the target programming code, where the parameter detection request is used to detect whether there are missing parameters that cause the code to run incorrectly in the target programming code, the missing parameters are the parameters missing after the target programming code is upgraded from a historical version of the code, and the target class is the class for parameter detection in the target programming code; identifying functions in each of the target classes to obtain a plurality of functions, and determining first parameter data in each of the functions, where the first parameter data is the data of the parameters that do not have missing parameters after the target programming code is upgraded, and the first parameter data at least includes: required parameter data of the function; based on the required parameter data in each of the functions and a string matching strategy, determining a parameter detection result of the target programming code, where the string matching strategy is used to determine actual parameter data in each of the functions.
[0025] Further, identifying functions in each of the target classes to obtain a plurality of functions, and determining first parameter data in each of the functions includes: reflecting each of the target classes through a first reflection to obtain a plurality of the functions in each of the target classes; reflecting each of the functions through a second reflection to obtain a target parameter set in each of the functions, where the target parameter set includes all parameters of the function when there are no missing parameters; based on the target parameter set in each of the functions, counting the first parameter data in each of the functions.
[0026] Further, after counting the first parameter data in each of the functions based on the target parameter set in each of the functions, it further includes: counting duplicate functions in the plurality of functions based on the function identifier of each of the functions; judging whether the required parameter data of the duplicate functions is the same to obtain a judgment result; in the case where the judgment result indicates that the required parameter data of the duplicate functions is different, outputting the duplicate functions; sending the duplicate functions to an object terminal controlled by a target object, and the object terminal performs parameter detection on the duplicate functions.
[0027] Furthermore, the string matching strategy includes at least: a target character combination, based on the required parameter data in each of the functions and the string matching strategy, determining the parameter detection result of the target programming code, including: counting the data containing the target character combination in each of the functions to obtain second parameter data of each of the functions, wherein the second parameter data is the actual parameter data of each of the functions to be detected; and determining the parameter detection result of the target programming code by comparing the second parameter data of each of the functions with the required parameter data.
[0028] Furthermore, the target character combination includes at least: a combination of brackets and target punctuation marks, and counting the data containing the target character combination in each of the functions to obtain second parameter data of each of the functions includes: identifying the target code in each of the functions where the brackets and the target punctuation marks exist; based on the number of the target punctuation marks in the target code, counting the actual number of parameters of the function associated with the target code to obtain second parameter data of each of the functions, wherein the second parameter data includes at least the actual number of parameters.
[0029] Furthermore, the required parameter data at least includes: the number of required parameters, and the parameter detection result of the target programming code is determined by comparing the second parameter data of each of the functions with the required parameter data, including: obtaining a comparison result by comparing the actual parameter number of each function with the required parameter number; when the comparison result indicates that the actual parameter number is greater than or equal to the required parameter number, determining that the parameter detection result of the function associated with the comparison result is that the missing parameters do not exist; when the comparison result indicates that the actual parameter number is less than the required parameter number, determining that the parameter detection result of the function associated with the comparison result is that the missing parameters exist; and counting the parameter detection results of multiple functions to obtain the parameter detection result of the target programming code.
[0030] Furthermore, before identifying the functions in each of the target classes to obtain multiple functions, it includes: obtaining classes that come with target programming software, wherein the target programming software is used to write the target programming code; obtaining all classes of the target programming code; and determining multiple target classes in the target programming code based on the classes that come with the target programming software and all classes in the target programming code, wherein the multiple target classes include other classes except the classes that come with the target programming software.
[0031] According to another aspect of the embodiments of the present invention, there is also provided a detection device for code parameters, including: an extraction module, configured to respond to a parameter detection request for a target programming code, and extract multiple target classes in the target programming code, where the parameter detection request is used to detect whether there are missing parameters in the target programming code that cause the code to run incorrectly, the missing parameters are the parameters missing after the target programming code is upgraded from a historical version of the code, and the target class is a class in the target programming code for which parameter detection is performed; a processing module, configured to identify functions in each of the target classes to obtain multiple functions, and determine first parameter data in each of the functions, where the first parameter data is data for which there are no missing parameters in each of the functions after the target programming code is upgraded, and the first parameter data at least includes: required parameter data of the function; a first determination module, configured to determine a parameter detection result of the target programming code based on the required parameter data in each of the functions and a string matching strategy, where the string matching strategy is used to determine actual parameter data in each of the functions.
[0032] Further, the processing module includes: a first reflection unit, configured to perform reflection on each of the target classes through first reflection to obtain multiple functions in each of the target classes; a second reflection unit, configured to perform reflection on each of the functions through second reflection to obtain a target parameter set in each of the functions, where the target parameter set includes all parameters of the function when there are no missing parameters; a first statistics unit, configured to count the first parameter data in each of the functions based on the target parameter set in each of the functions.
[0033] Further, the processing module further includes: after counting the first parameter data in each of the functions based on the target parameter set in each of the functions, a second statistics unit, configured to count duplicate functions among the multiple functions based on function identifiers of each of the functions; a judgment unit, configured to judge whether the required parameter data of the duplicate functions is the same to obtain a judgment result; an output unit, configured to output the duplicate functions when the judgment result indicates that the required parameter data of the duplicate functions is different; a detection unit, configured to send the duplicate functions to an object terminal controlled by a target object, and the object terminal performs parameter detection on the duplicate functions.
[0034] Furthermore, the string matching strategy includes at least: a target character combination, and the first determination module includes: a third statistical unit, used to count the data containing the target character combination in each of the functions to obtain second parameter data of each of the functions, wherein the second parameter data is the actual parameter data of each of the functions to be detected; a comparison unit, used to determine the parameter detection result of the target programming code by comparing the second parameter data of each of the functions with the required parameter data.
[0035] Furthermore, the target character combination includes at least: a combination of brackets and target punctuation marks, and the third statistical unit includes: an identification submodule, used to identify the target code containing the brackets and the target punctuation marks in each of the functions; a first statistical submodule, used to count the number of actual parameters of the function associated with the target code based on the number of the target punctuation marks in the target code, and obtain second parameter data of each of the functions, wherein the second parameter data includes at least the actual number of parameters.
[0036] Furthermore, the required parameter data at least includes: the number of required parameters, and the comparison unit includes: a comparison submodule, used to compare the actual number of parameters of each function with the number of required parameters to obtain a comparison result; a first determination submodule, used to determine that the parameter detection result of the function associated with the comparison result is the absence of the missing parameters when the comparison result indicates that the actual number of parameters is greater than or equal to the number of required parameters; a second determination submodule, used to determine that the parameter detection result of the function associated with the comparison result is the presence of the missing parameters when the comparison result indicates that the actual number of parameters is less than the number of required parameters; a second statistical submodule, used to count the parameter detection results of multiple functions to obtain the parameter detection result of the target programming code.
[0037] Furthermore, the code parameter detection device also includes: a first acquisition module, used to acquire the classes that come with the target programming software before identifying the functions in each of the target classes to obtain multiple functions, wherein the target programming software is used to write the target programming code; a second acquisition module, used to acquire all classes of the target programming code; a second determination module, used to determine multiple target classes in the target programming code based on the classes that come with the target programming software and all classes in the target programming code, wherein the multiple target classes include other classes except the classes that come with the target programming software.
[0038] According to another aspect of an embodiment of the present invention, there is also provided an electronic device, comprising: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute any one of the above code parameter detection methods by executing the executable instructions.
[0039] According to another aspect of the embodiments of the present invention, there is also provided a computer-readable storage medium storing a computer program, wherein when the computer program runs, it controls the device where the computer-readable storage medium is located to execute the detection method of the code parameters in any one of the above.
[0040] In the present invention, in response to a parameter detection request for a target programming code, a plurality of target classes in the target programming code are extracted, wherein the parameter detection request is used to detect whether there are missing parameters in the target programming code that cause the code to run incorrectly, the missing parameters are the parameters missing after the target programming code upgrades the version of the historical version code, and the target class is the class for parameter detection in the target programming code; functions in each target class are identified to obtain a plurality of functions, and first parameter data in each function is determined, wherein the first parameter data is the data of the function that does not have missing parameters after the target programming code upgrades the version, and the first parameter data at least includes: required parameter data of the function; based on the required parameter data in each function and a string matching strategy, a parameter detection result of the target programming code is determined, wherein the string matching strategy is used to determine the actual parameter data in each function. Furthermore, it solves the technical problem of poor detection effect in the related art when detecting whether the passed parameters of the programming language php undergoing version upgrade are too few. In the present invention, according to the string matching strategy, the actual parameter data in each function within the target class in the target programming code and the required parameter data in each function are identified, and it is determined whether there are missing parameters in the target programming code, avoiding the situation of easy missed detection when batch detecting whether the parameters of the target programming code are too few by executing the code or through a static code checking tool, thereby achieving the technical effect of improving the detection accuracy of detecting whether the parameters of the target programming code are too few. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] The drawings described herein are used to provide a further understanding of the present invention, form a part of this application, and the illustrative embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation to the present invention. In the drawings:
[0042] Figure 1 is a flowchart of an optional method for detecting code parameters according to an embodiment of the present invention;
[0043] Figure 2 is a flowchart of an optional statistics of first parameter data according to an embodiment of the present invention;
[0044] Figure 3 is a flowchart of an optional parameter detection of repeated functions according to an embodiment of the present invention;
[0045] Figure 4Schematic diagram of a detection device for an optional code parameter according to an embodiment of the present invention. Detailed implementation manners
[0046] In order to enable those skilled in the art to better understand the solution of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0047] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and do not necessarily need to be used to describe a specific order or sequence. It should be understood that such data can be interchanged under appropriate circumstances so that the embodiments of the present invention described herein can be implemented in an order different from those illustrated or described herein. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device including a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0048] Embodiment 1
[0049] According to an embodiment of the present invention, a method embodiment of a method for detecting an optional code parameter is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. And although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than here.
[0050] Figure 1 It is a flowchart of a method for detecting an optional code parameter according to an embodiment of the present invention. As Figure 1 shown, the method includes the following steps:
[0051] Step S101, in response to a parameter detection request for a target programming code, extract multiple target classes in the target programming code, where the parameter detection request is used to detect whether there are missing parameters in the target programming code that cause the code to run incorrectly, the missing parameters are the parameters missing after the target programming code upgrades the version of the historical version code, and the target class is the class for parameter detection in the target programming code.
[0052] The above-mentioned target programming code may include, but is not limited to, code written in the PHP programming language. The above-mentioned parameter detection request can be used to detect whether there are missing parameters in the target programming code that cause errors in code operation. The missing parameters are the parameters missing after the target programming code upgrades the version of the historical version code. For example, when the project upgrades from PHP 7.0 and earlier versions to PHP 7.1 and later versions, there will be inconsistent parameters in the old code and too few passed parameters, resulting in various error messages. The above-mentioned target class can be the class in the target programming code to be parameter-detected.
[0053] Step S102: Identify the functions in each target class to obtain multiple functions, and determine the first parameter data in each function. The first parameter data is the data of the parameters that do not have missing parameters after the target programming code is upgraded. The first parameter data includes at least: the required parameter data of the function.
[0054] The above-mentioned functions can be the functions in each target class, that is, the methods (methods / functions in the programming language) in each target class. In the target programming language, there can be multiple target classes, and each target class can include multiple functions. Identifying the functions in each target class can also obtain all the functions included in all the target classes in the target programming code.
[0055] The above-mentioned required parameter data can refer to the parameter data that must be filled in the function. If the function lacks this required parameter data, it will cause an error during code operation. The above-mentioned first parameter data can be the data of the parameters that do not have missing parameters after the target programming code is upgraded. The above-mentioned first parameter data can include, but is not limited to: the required parameter data of each function, and can also include the total number of all parameters in the function when there are no missing parameters. The required parameter data of each function can include the number of required parameters.
[0056] Step S103: Based on the required parameter data in each function and the string matching strategy, determine the parameter detection result of the target programming code, where the string matching strategy is used to determine the actual parameter data in each function.
[0057] The above-mentioned string matching strategy can be used to determine the actual parameter data in each function. The actual parameter data of each function can be counted through the string matching strategy. By comparing the actual parameter data of each function with the required parameter data of the function, it can be determined whether each function has missing parameters, that is, the parameter detection result of each function is determined. Then, based on the parameter detection results of all the functions in the target programming code for which parameter detection is performed, the parameter detection result of the target programming code is determined.
[0058] The above parameter detection results may include, but are not limited to, whether there is a situation of missing parameters in the target programming code as described above, and which required parameters are missing. The above parameter detection results can also be used to determine whether there are too few parameters in the target programming code after version upgrade, which may cause an error.
[0059] In this embodiment, the above steps can be executed by running a preset script. By executing the above steps through the preset script, all php codes to be parameter-detected (corresponding to the above target programming code) can be automatically traversed, and it can be automatically detected whether there is a situation of too few parameters in the code and the code with too few parameters.
[0060] Through the above steps, in this embodiment, according to the string matching strategy, the actual parameter data in each function within the target class in the target programming code and the required parameter data in each function are identified, and the detection result of whether there are missing parameters in the target programming code is determined, avoiding the situation of easy missed detection when detecting whether there are too few parameters in the target programming code by executing the code or through a static code inspection tool in batches, thereby achieving the technical effect of improving the detection accuracy of whether there are too few parameters in the target programming code. Furthermore, it solves the technical problem of poor detection effect in detecting whether there are too few passed parameters in the programming language php for version upgrade in the related art.
[0061] Optionally, Figure 2 is a flowchart of an optional first parameter data statistics according to an embodiment of the present invention, as Figure 2 shown. Identify the functions in each target class to obtain a plurality of functions, and determine the first parameter data in each function, including:
[0062] Step S201, reflect each target class through the first reflection to obtain a plurality of functions in each target class;
[0063] Step S202, reflect each function through the second reflection to obtain a target parameter set in each function, where the target parameter set includes all parameters of the function when there are no missing parameters;
[0064] Step S203, based on the target parameter set in each function, count the first parameter data in each function.
[0065] The above-mentioned first reflection can be class reflection in a programming language. By performing reflection on each target class through the above-mentioned first reflection, the functions included in each target class can be obtained. The above-mentioned second reflection can refer to method reflection (i.e., function reflection) in a programming language. By performing reflection on each function through the second reflection, the parameters that each function should include (i.e., all parameters when the function does not lack parameters) can be obtained. With all the parameters that each function should include, the above-mentioned target parameter set can be formed. By counting the parameters in the target parameter set in each function, the data for counting can include but is not limited to: parameter identification, total number of parameters, number of required parameters. The data obtained from the counting can form the first parameter data in each function, achieving the technical effects of statistical accuracy and statistical efficiency in counting the first parameter data of each function.
[0066] The following uses a set of optional codes to illustrate the above-mentioned first reflection and second reflection by way of example:
[0067] $reflection = new ReflectionClass($className); / / Class reflection (corresponding to the above-mentioned first reflection);
[0068] $class_methods = $reflection->getMethods(); / / Obtain what methods each class has;
[0069] $reflection = new ReflectionMethod($className, $funName); / / Method reflection (corresponding to the above-mentioned second reflection);
[0070] $argsArr = $reflection->getParameters(); / / What parameters each method has;
[0071] $argObj->isDefaultValueAvailable(); / / Determine whether each parameter has a default value. If there is a default value, it is not a [required parameter]. If there is no default value, it is a [required parameter].
[0072] Optionally, Figure 3 is a flowchart of an optional parameter detection for repeated functions according to an embodiment of the present invention. As Figure 3 shown, after counting the first parameter data in each function based on the target parameter set in each function, it further includes:
[0073] Step S301, based on the function identification of each function, count the repeated functions among multiple functions;
[0074] Step S302, determine whether the required parameter data of the duplicate function is the same to obtain a determination result;
[0075] Step S303, when the determination result indicates that the required parameter data of the duplicate function is different, output the duplicate function;
[0076] Step S304, send the duplicate function to the object terminal controlled by the target object, and the object terminal performs parameter detection on the duplicate function.
[0077] In this embodiment, the above duplicate function may refer to a function in which the function identifier (such as: function name) has a duplicate definition in the target programming code. Multiple functions can be counted based on the function identifier (such as: function name) of each function. If the required parameter data in each function of the duplicate function is different, each function in the duplicate function is recorded in a log, and the log is sent to the above object terminal. The target object performs parameter detection on the duplicate function in the above object terminal. Among them, the above target object may be an object that performs parameter detection through the object terminal. If the required parameter data in each function of the duplicate function is the same, the parameter detection result of the target programming code can be determined based on the required parameter data in each function and the string matching strategy.
[0078] For example: extract functions / methods that appear multiple times in all target classes in the target programming code. The names of the functions / methods that appear multiple times and are not repeated, the total number of parameters, and the number of required parameters. If a function is defined multiple times (that is, the function is defined in multiple places in the target programming code), if one definition has 3 required parameters and another definition has 4 required parameters, then the function can be output to the log and viewed and processed separately by the target object in the object terminal. Among them, the way for the target object to view and process in the object terminal may include but is not limited to: the target object manually views and processes in the object terminal. If one definition has 3 required parameters and another definition has 3 required parameters, it means that the required parameter data definition is consistent, and the parameter detection can be automatically performed through the string matching strategy, achieving the technical effect of improving the detection accuracy of parameter detection.
[0079] Optionally, the string matching strategy at least includes: target character combinations. Based on the required parameter data in each function and the string matching strategy, determining the parameter detection result of the target programming code includes: counting the data containing the target character combinations in each function to obtain the second parameter data of each function, where the second parameter data is the actual parameter data of each function to be detected; comparing the second parameter data of each function with the required parameter data to determine the parameter detection result of the target programming code.
[0080] In this embodiment, it is possible to count the data containing the target character combination in each function. For example, if the target character combination is included in a function, based on the target character combination in the function, the parameter data included in the function can be counted, including but not limited to: the actual number of parameters included in the function, so as to obtain the second parameter data of each function. By comparing the second parameter data of each function with the required parameter data, and comparing the second parameter data domain of each function with the required parameter data, it is determined whether each function has missing parameters, and the parameter detection result of the target programming code is determined.
[0081] For example: by searching each php file (excluding comments). Separated according to the combination of parentheses and commas, calculate the number of parameters of the php file calling function (corresponding to each function above). For example, if there is one comma in funXxx(1,2), it can be determined that there are 2 parameters. By comparing with the required parameter data of the function, it is judged whether there are missing parameters. If there are missing parameters, they can be recorded. By counting the records, the parameter detection result of the target programming code can be obtained. Since it is string matching, there is a small possibility of false positives and manual judgment is required. In an optional way in this embodiment, a method (or function) whitelist can also be added. Function data that does not perform parameter detection can be stored in the whitelist, and the functions in the whitelist can not perform parameter detection, achieving the technical effect of improving the detection efficiency of parameter detection.
[0082] Optionally, the target character combination at least includes: the combination of parentheses and the target punctuation mark. Count the data containing the target character combination in each function to obtain the second parameter data of each function, including: identifying the target code with parentheses and the target punctuation mark in each function; based on the number of target punctuation marks in the target code, count the actual number of parameters of the function associated with the target code to obtain the second parameter data of each function, where the second parameter data at least includes the actual number of parameters.
[0083] For example: by searching each php file (excluding comments). Separated according to the combination of parentheses and commas (corresponding to the above target punctuation mark), calculate the number of parameters of the php file calling function (corresponding to the above actual number of parameters). For example, if there is one comma in funXxx(1,2), it can be determined that the function actually includes 2 parameters. By counting the actual number of parameters of each function, the second parameter data of each function can be obtained, achieving the technical effects of improving the statistical efficiency and statistical accuracy of the actual number of parameters of each function.
[0084] Optionally, the required parameter data includes at least: the number of required parameters, and the parameter detection result of the target programming code is determined by comparing the second parameter data of each function with the required parameter data, including: obtaining a comparison result by comparing the actual number of parameters of each function with the number of required parameters; when the comparison result indicates that the actual number of parameters is greater than or equal to the number of required parameters, determining that the parameter detection result of the function associated with the comparison result is that there are no missing parameters; when the comparison result indicates that the actual number of parameters is less than the number of required parameters, determining that the parameter detection result of the function associated with the comparison result is that there are missing parameters; and counting the parameter detection results of multiple functions to obtain the parameter detection result of the target programming code.
[0085] In this embodiment, the comparison result can be obtained by comparing the actual number of parameters of each function with the number of required parameters.
[0086] When the above comparison result indicates that the actual number of parameters of the function is greater than or equal to the number of required parameters, it is determined that the parameter detection result of the function associated with the comparison result is that there is no missing parameter.
[0087] When the comparison result indicates that the actual number of parameters is less than the required number of parameters, it is determined that the parameter detection result of the function associated with the comparison result is that there are missing parameters.
[0088] In this embodiment, the parameter detection results of each function can be statistically analyzed to obtain the parameter detection results of multiple functions, and the parameter detection results of multiple target classes in the target programming code can be obtained based on the parameter detection results of the multiple functions, so as to obtain the parameter detection results of the target programming code, thereby achieving the technical effect of improving the detection accuracy of whether the target programming code has too few parameters.
[0089] Optionally, before identifying the functions in each target class to obtain multiple functions, the process includes: obtaining classes that come with target programming software, wherein the target programming software is used to write target programming code; obtaining all classes of the target programming code; and determining multiple target classes in the target programming code based on the classes that come with the target programming software and all classes in the target programming code, wherein the multiple target classes include other classes except the classes that come with the target programming software.
[0090] In this embodiment, the classes that come with the target programming software can be obtained, and the classes that come with the target programming software can be obtained through the class initial array. By traversing the target programming code, all classes in the target programming code can be obtained; based on the classes that come with the target programming software and all classes in the target programming code, the classes that come with the target programming software are deleted from all classes in the target programming code, and multiple target classes in the target programming code can be obtained.
[0091] The following is an example with a set of code:
[0092] Step 1: Get the initial array of classes that comes with PHP after startup.
[0093] $classOldArr=get_declared_classes()
[0094] Step 2: Load all php files in the target programming code;
[0095] Traverse and read all PHP files in the PHP code directory;
[0096] require_once all files, and each class is loaded into memory;
[0097] It can detect incomplete PHP files, such as multiple curly braces or missing semicolons, and the script will report an error.
[0098] Step 3: Get all classes, including the classes of all files defined in the previous step.
[0099] $classNewArr=get_declared_classes()
[0100] Step 4: Extract only the classes (target classes) in the required code, that is, ignore the self-contained classes (classes in the initial array of classes) identified in step 1 among all classes.
[0101] $classArr=array_slice($classNewArr,count($classOldArr))
[0102] Through this embodiment, all PHP codes (corresponding to the target programming code mentioned above) are automatically traversed, the number of required parameters of the method (i.e., function) is determined through reflection, and string matching is performed on the PHP code to detect whether there is an error of too few parameters passed after the PHP version is upgraded, thereby achieving the technical effect of improving the detection accuracy of whether there are too few parameters in the PHP code.
[0103] Embodiment 2
[0104] This embodiment provides an optional code parameter detection device, and each implementation module in the detection device corresponds to each implementation step in the first embodiment.
[0105] Figure 4 is a schematic diagram of an optional code parameter detection device according to an embodiment of the present invention, such as Figure 4As shown in the figure, the detection device for code parameters includes: an extraction module 41, a processing module 42, and a first determination module 43.
[0106] Specifically, the extraction module 41 is configured to respond to a parameter detection request for a target programming code, and extract multiple target classes in the target programming code. Among them, the parameter detection request is used to detect whether there are missing parameters in the target programming code that cause the code to run incorrectly. The missing parameters are the parameters missing after the target programming code upgrades the version of the historical version code. The target class is the class in the target programming code for parameter detection.
[0107] The processing module 42 is configured to identify functions in each target class to obtain multiple functions, and determine the first parameter data in each function. Among them, the first parameter data is the data of each function that does not have missing parameters after the target programming code upgrades the version. The first parameter data at least includes: the required parameter data of the function.
[0108] The first determination module 43 is configured to determine the parameter detection result of the target programming code based on the required parameter data in each function and the string matching strategy. Among them, the string matching strategy is used to determine the actual parameter data in each function.
[0109] In the code parameter detection device provided in the second embodiment of the present application, the extraction module 41 can respond to a parameter detection request for a target programming code and extract multiple target classes in the target programming code. Among them, the parameter detection request is used to detect whether there are missing parameters in the target programming code that cause the code to run incorrectly. The missing parameters are the parameters missing after the target programming code upgrades the version of the historical version code. The target class is the class in the target programming code for parameter detection. The processing module 42 identifies the functions in each target class to obtain multiple functions and determines the first parameter data in each function. Among them, the first parameter data is the data of the parameters that do not have missing parameters after the target programming code upgrades the version. The first parameter data at least includes: the required parameter data of the function. The first determination module 43 determines the parameter detection result of the target programming code based on the required parameter data in each function and the string matching strategy. Among them, the string matching strategy is used to determine the actual parameter data in each function. Furthermore, it solves the technical problem of poor detection effect in detecting whether there are too few passed parameters in the programming language php that is upgraded in version in the related art. In this embodiment, according to the string matching strategy, the actual parameter data in each function in the target class in the target programming code and the required parameter data in each function are identified to determine the detection result of whether there are missing parameters in the target programming code, avoiding the situation of easy missed detection when detecting whether there are too few parameters in the target programming code by executing the code or through a static code inspection tool in batches, thus achieving the technical effect of improving the detection accuracy of detecting whether there are too few parameters in the target programming code.
[0110] Optionally, in the code parameter detection device provided in the second embodiment of the present application, the processing module 42 includes: a first reflection unit for reflecting each target class through the first reflection to obtain multiple functions in each target class; a second reflection unit for reflecting each function through the second reflection to obtain a target parameter set in each function, where the target parameter set includes all parameters of the function when there are no missing parameters; a first statistical unit for counting the first parameter data in each function based on the target parameter set in each function.
[0111] The above-mentioned first reflection can be class reflection in a programming language. By performing reflection on each target class through the above-mentioned first reflection, the functions included in each target class can be obtained. The above-mentioned second reflection can refer to method reflection (i.e., function reflection) in a programming language. By performing reflection on each function through the second reflection, the parameters that each function should include (i.e., all parameters when the function does not lack parameters) can be obtained. With all the parameters that each function should include, the above-mentioned target parameter set can be composed. By counting the parameters in the target parameter set of each function, the data for counting can include but is not limited to: parameter identification, total number of parameters, number of required parameters. The data obtained from the counting can form the first parameter data in each function, achieving the technical effects of statistical accuracy and statistical efficiency in counting the first parameter data of each function.
[0112] The following uses a set of optional code to illustrate the above-mentioned first reflection and second reflection:
[0113] $reflection = new ReflectionClass($className); / / Class reflection (corresponding to the above-mentioned first reflection);
[0114] $class_methods = $reflection->getMethods(); / / Obtain which methods each class has;
[0115] $reflection = new ReflectionMethod($className, $funName); / / Method reflection (corresponding to the above-mentioned second reflection);
[0116] $argsArr = $reflection->getParameters(); / / Which parameters each method has;
[0117] $argObj->isDefaultValueAvailable(); / / Determine whether each parameter has a default value. If there is a default value, it is not a [required parameter]. If there is no default value, it is a [required parameter].
[0118] Optionally, in the code parameter detection device provided in the second embodiment of the present application, the processing module 42 further includes: after counting the first parameter data in each function based on the target parameter set in each function, a second statistical unit for counting duplicate functions among multiple functions based on the function identifier of each function; a judgment unit for judging whether the required parameter data of the duplicate functions is the same to obtain a judgment result; an output unit for outputting the duplicate functions when the judgment result indicates that the required parameter data of the duplicate functions is different; and a detection unit for sending the duplicate functions to the object terminal controlled by the target object, and the object terminal performs parameter detection on the duplicate functions.
[0119] In this embodiment, the above-mentioned duplicate functions may refer to functions where the function identifier (such as: function name) is repeatedly defined in the target programming code. Duplicate functions among multiple functions can be counted based on the function identifier (such as: function name) of each function. If the required parameter data in each function of the duplicate functions is different, each function in the duplicate functions is recorded in a log, and the log is sent to the above-mentioned object terminal, and the target object performs parameter detection on the duplicate functions in the above-mentioned object terminal. Among them, the above-mentioned target object may be an object that performs parameter detection through the object terminal. If the required parameter data in each function of the duplicate functions is the same, the parameter detection result of the target programming code can be determined based on the required parameter data in each function and the string matching strategy.
[0120] For example: Take out the functions / methods that appear multiple times in all target classes in the target programming code. The repeatedly appearing and non-duplicate
function (method / function in programming) name
total number of parameters
number of required parameters
[0121] Optionally, in the code parameter detection device provided in the second embodiment of the present application, the string matching strategy at least includes: a target character combination. The first determination module 43 includes: a third statistics unit, configured to count the data including the target character combination in each function to obtain the second parameter data of each function, where the second parameter data is the actual parameter data of each function to be detected; a comparison unit, configured to determine the parameter detection result of the target programming code by comparing the second parameter data of each function with the required parameter data.
[0122] In this embodiment, it is possible to count the data including the target character combination in each function. For example, if the target character combination is included in the function, based on the target character combination in the function, the parameter data included in the function can be counted, which may include, but is not limited to: the actual number of parameters included in the function, so as to obtain the second parameter data of each function. By comparing the second parameter data of each function with the required parameter data, and comparing the second parameter data domain of each function with the required parameter data, it is determined whether each function has missing parameters, and the parameter detection result of the target programming code is determined.
[0123] For example: by searching each php file (excluding comments). Separated according to the combination of parentheses and commas, calculate the number of parameters of the function called by the php file (corresponding to each function above). For example, if the number of commas in funXxx(1,2) is one, it can be determined that there are 2 parameters. By comparing with the required parameter data of the function, it is judged whether there are missing parameters. If there are missing parameters, they can be recorded. By counting the records, the parameter detection result of the target programming code can be obtained. Since it is string matching, there is a small possibility of false positives and manual judgment is required. In an optional manner in this embodiment, a method (or function) whitelist can also be added. Function data that does not perform parameter detection can be stored in the whitelist, and the functions in the whitelist can not perform parameter detection, achieving the technical effect of improving the detection efficiency of parameter detection.
[0124] Optionally, in the code parameter detection device provided in the second embodiment of the present application, the target character combination at least includes: a combination of parentheses and a target punctuation mark. The third statistics unit includes: an identification sub-module, configured to identify the target code with parentheses and a target punctuation mark in each function; a first statistics sub-module, configured to count the actual number of parameters of the function associated with the target code based on the number of target punctuation marks in the target code to obtain the second parameter data of each function, where the second parameter data at least includes the actual number of parameters.
[0125] For example: by searching each PHP file (excluding comments), separating according to the combination of parentheses and commas (corresponding to the target punctuation marks mentioned above), and calculating the number of parameters of the function calls in the PHP file (corresponding to each function mentioned above) (corresponding to the actual number of parameters mentioned above). For example, if there is one comma in funXxx(1,2), it can be determined that the function actually includes 2 parameters. By counting the actual number of parameters of each function, the data of the second parameter of each function can be obtained, achieving the technical effects of improving the statistical efficiency and accuracy of the actual number of parameters of each function.
[0126] Optionally, in the code parameter detection device provided in the second embodiment of the present application, the required parameter data at least includes: the number of required parameters. The comparison unit includes: a comparison sub-module for obtaining a comparison result by comparing the actual number of parameters of each function with the number of required parameters; a first determination sub-module for determining that the parameter detection result of the function associated with the comparison result is that there is no missing parameter when the comparison result indicates that the actual number of parameters is greater than or equal to the number of required parameters; a second determination sub-module for determining that the parameter detection result of the function associated with the comparison result is that there is a missing parameter when the comparison result indicates that the actual number of parameters is less than the number of required parameters; a second statistics sub-module for counting the parameter detection results of multiple functions to obtain the parameter detection result of the target programming code.
[0127] In this embodiment, a comparison result can be obtained by comparing the actual number of parameters of each function with the number of required parameters.
[0128] When the above comparison result indicates that the actual number of parameters of the function is greater than or equal to the number of required parameters, it is determined that the parameter detection result of the function associated with the comparison result is that there is no missing parameter.
[0129] When the comparison result indicates that the actual number of parameters is less than the number of required parameters, it is determined that the parameter detection result of the function associated with the comparison result is that there is a missing parameter.
[0130] In this embodiment, the parameter detection results of each function can be counted to obtain the parameter detection results of multiple functions, and based on the parameter detection results of multiple functions, the parameter detection results of multiple target classes in the target programming code can be obtained, thereby obtaining the parameter detection result of the target programming code, achieving the technical effect of improving the detection accuracy of whether there are too few parameters in the target programming code.
[0131] Optionally, in the code parameter detection device provided in Example 2 of the present application, the code parameter detection device also includes: a first acquisition module, used to obtain the classes that come with the target programming software before identifying the functions in each target class to obtain multiple functions, wherein the target programming software is used to write the target programming code; a second acquisition module, used to obtain all classes of the target programming code; a second determination module, used to determine multiple target classes in the target programming code based on the classes that come with the target programming software and all classes in the target programming code, wherein the multiple target classes include other classes except the classes that come with the target programming software.
[0132] In this embodiment, the classes that come with the target programming software can be obtained, and the classes that come with the target programming software can be obtained through the class initial array. By traversing the target programming code, all classes in the target programming code can be obtained; based on the classes that come with the target programming software and all classes in the target programming code, the classes that come with the target programming software are deleted from all classes in the target programming code, and multiple target classes in the target programming code can be obtained.
[0133] The following is an example with a set of code:
[0134] Step 1: Get the initial array of classes that comes with PHP after startup.
[0135] $classOldArr=get_declared_classes()
[0136] Step 2: Load all php files in the target programming code;
[0137] Traverse and read all PHP files in the PHP code directory;
[0138] require_once all files, and each class is loaded into memory;
[0139] It can detect incomplete PHP files, such as multiple curly braces or missing semicolons, and the script will report an error.
[0140] Step 3: Get all classes, including the classes of all files defined in the previous step.
[0141] $classNewArr=get_declared_classes()
[0142] Step 4: Extract only the classes (target classes) in the required code, that is, ignore the self-contained classes (classes in the initial array of classes) identified in step 1 among all classes.
[0143] $classArr=array_slice($classNewArr,count($classOldArr))
[0144] Through this embodiment, all PHP codes (corresponding to the target programming code mentioned above) are automatically traversed, the number of required parameters of the method (i.e., function) is determined through reflection, and string matching is performed on the PHP code to detect whether there is an error of too few parameters passed after the PHP version is upgraded, thereby achieving the technical effect of improving the detection accuracy of whether there are too few parameters in the PHP code.
[0145] The above-mentioned code parameter detection device may also include a processor and a memory. The above-mentioned extraction module 41, processing module 42, first determination module 43, etc. are all stored in the memory as program units, and the processor executes the above-mentioned program units stored in the memory to realize corresponding functions.
[0146] The above processor includes a kernel, which retrieves the corresponding program unit from the memory. One or more kernels can be set, and the kernel parameters are adjusted to identify the actual parameter data in each function in the target class in the target programming code according to the string matching strategy, and the required parameter data in each function, to determine whether the target programming code has a missing parameter detection result, thereby avoiding the situation where missed detection is easy to occur when batch detecting whether the target programming code parameters are too few through executing code or through static code checking tools, thereby achieving the technical effect of improving the detection accuracy of whether the target programming code parameters are too few.
[0147] The above-mentioned memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one storage chip.
[0148] According to another aspect of an embodiment of the present invention, there is also provided an electronic device, comprising: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute any one of the above code parameter detection methods by executing the executable instructions.
[0149] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is provided, which stores a computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute any of the above-mentioned code parameter detection methods.
[0150] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0151] In the above embodiments of the present invention, the descriptions of the respective embodiments have their own emphases. For parts not detailed in a certain embodiment, reference may be made to the relevant descriptions of other embodiments.
[0152] In several embodiments provided by the present application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are merely illustrative. For example, the division of the modules can be a logical function division. In actual implementation, there may be other division methods. For example, multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection between each other can be through some interfaces. The indirect coupling or communication connection of units or modules can be in an electrical or other form.
[0153] The units described as separate components may or may not be physically separated. The components displayed as units may or may not be physical units, that is, they can be located in one place or distributed to multiple units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0154] In addition, in each embodiment of the present invention, the functional units can be integrated in a processing unit, or each unit can exist physically alone, or two or more units can be integrated in one unit. The above-mentioned integrated units can be implemented in the form of hardware or in the form of software functional units.
[0155] If the above-mentioned integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The foregoing storage medium includes: USB flash drives, read-only memories (ROMs), random access memories (RAMs), mobile hard disks, magnetic disks, or optical discs and other various media that can store program codes.
[0156] The above is only the preferred embodiment of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the principle of the present invention, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the present invention.
Claims
1. A method for detecting code parameters, characterized in that, Including: In response to a parameter detection request for the target programming code, extract multiple target classes in the target programming code, where the parameter detection request is used to detect whether there are missing parameters that cause the code to run incorrectly in the target programming code, the missing parameters are the parameters missing after the target programming code upgrades the version of the historical version code, and the target class is the class for parameter detection in the target programming code; Identify the functions in each of the target classes to obtain multiple functions, and determine the first parameter data in each of the functions, where the first parameter data is the data without missing parameters after the target programming code upgrades the version, and the first parameter data at least includes: the required parameter data of the function; Based on the required parameter data and the string matching strategy in each of the functions, determine the parameter detection result of the target programming code, where the string matching strategy is used to determine the actual parameter data in each of the functions; Wherein, the string matching strategy at least includes: a target character combination. Based on the required parameter data and the string matching strategy in each of the functions, determining the parameter detection result of the target programming code includes: counting the data containing the target character combination in each of the functions to obtain the second parameter data of each of the functions, where the second parameter data is the actual parameter data to be detected for each of the functions; comparing the second parameter data of each of the functions with the required parameter data to determine the parameter detection result of the target programming code.
2. The detection method according to claim 1, characterized in that, Identifying the functions in each of the target classes to obtain multiple functions, and determining the first parameter data in each of the functions, includes: Reflect each of the target classes through first reflection to obtain multiple functions in each of the target classes; Reflect each of the functions through second reflection to obtain a target parameter set in each of the functions, where the target parameter set includes all parameters of the function when there are no missing parameters; Based on the target parameter set in each of the functions, count the first parameter data in each of the functions.
3. The detection method according to claim 2, characterized in that, After counting the first parameter data in each of the functions based on the target parameter set in each of the functions, it further includes: Based on the function identifiers of each of the functions, count the duplicate functions among the multiple functions; Judge whether the required parameter data of the duplicate functions is the same to obtain a judgment result; In the case where the judgment result indicates that the required parameter data of the duplicate functions is different, output the duplicate functions; Send the duplicate functions to the object terminal controlled by the target object, and the object terminal performs parameter detection on the duplicate functions.
4. The detection method according to claim 1, characterized in that, The target character combination at least includes: a combination of parentheses and a target punctuation mark. Counting the data containing the target character combination in each of the functions to obtain the second parameter data of each of the functions, includes: Identify the target code with the parentheses and the target punctuation mark in each of the functions; Based on the number of the target punctuation marks in the target code, the actual number of parameters of the function associated with the target code is counted to obtain second parameter data of each of the functions, wherein the second parameter data at least includes the actual number of parameters.
5. The detection method according to claim 4, characterized in that, The required parameter data at least includes: the number of required parameters, and the parameter detection result of the target programming code is determined by comparing the second parameter data of each function with the required parameter data, including: By comparing the actual number of parameters of each function with the required number of parameters, a comparison result is obtained; In a case where the comparison result indicates that the actual number of parameters is greater than or equal to the required number of parameters, determining that a parameter detection result of the function associated with the comparison result is that the missing parameters do not exist; In a case where the comparison result indicates that the actual number of parameters is less than the required number of parameters, determining that a parameter detection result of the function associated with the comparison result is that the missing parameters exist; The parameter detection results of the multiple functions are counted to obtain the parameter detection results of the target programming code.
6. The detection method according to claim 1, characterized in that, Before identifying the functions in each target class to obtain multiple functions, the method includes: Obtaining a class provided by a target programming software, wherein the target programming software is used to write the target programming code; Obtaining all classes of the target programming code; Based on the classes provided by the target programming software and all the classes in the target programming code, a plurality of target classes in the target programming code are determined, wherein the plurality of target classes include other classes except the classes provided by the target programming software.
7. A device for detecting code parameters, characterized in that, include: An extraction module, for responding to a parameter detection request for a target programming code, and extracting a plurality of target classes in the target programming code, wherein the parameter detection request is used to detect whether there are missing parameters in the target programming code that cause code running errors, the missing parameters being the missing parameters after the target programming code performs a version upgrade on a historical version code, and the target class being the class for parameter detection in the target programming code; A processing module, configured to identify functions in each of the target classes, obtain multiple functions, and determine first parameter data in each of the functions, wherein the first parameter data is data of no missing parameters for each of the functions after the target programming code is upgraded, and the first parameter data at least includes: required parameter data of the function; A first determination module, configured to determine a parameter detection result of the target programming code based on the required parameter data and a string matching strategy in each of the functions, wherein the string matching strategy is used to determine actual parameter data in each of the functions; Among them, the string matching strategy at least includes: a target character combination. The first determination module includes: a third statistical unit, configured to count data including the target character combination in each of the functions, to obtain second parameter data of each of the functions, where the second parameter data is actual parameter data of each of the functions to be detected; and a comparison unit, configured to determine a parameter detection result of the target programming code by comparing the second parameter data of each of the functions with the required parameter data.
8. A computer-readable storage medium, characterized in that, A computer program is stored in the computer-readable storage medium, where, when the computer program runs, it controls the device where the computer-readable storage medium is located to execute the code parameter detection method according to any one of claims 1 to 6.
9. An electronic device, characterized in that, It includes one or more processors and a memory, where the memory is used to store one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors are caused to implement the code parameter detection method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Component configuration table generation method and device, electronic equipment and storage medium
CN112667223A
Method and device for anomaly detection and parameter filling of time series data
CN114826988A