简体   繁体   English

我怎么能用大量的isset检查和重复的生成参数来减少PHP代码?

[英]How could I reduce PHP code with lots of isset checks and repetitive, but variable generation arguments?

Greetings. 问候。 I am struggling to reduce a code segment that looks rather lengthy, leaving me unconvinced about its essentiality. 我正在努力减少看起来相当冗长的代码段,让我不相信它的必要性。 It's a function to generate a multitude of session arrays used to fill out forms, and it has me verifying the existence of certain values in the argument array, with cases for every single one to generate the requested arrays. 它是一个生成用于填充表单的多个会话数组的函数,它让我验证参数数组中是否存在某些值,每个单独的值都可以生成所请求的数组。 Here goes: 开始:

function prepOptional($formData) {

    $baseInfo=getBaseInfo();

    $_SESSION['fooData'] = (isset($formData['cbFoo']) ? prepBaseForm($baseInfo, 'foo',
            'Option foo') : '');

    $_SESSION['opt1Data'] = (isset($formData['cbOpt1']) ? prepBaseForm($baseInfo, 'opt1',
            'Option 1') : '');

    $_SESSION['barData'] = (isset($formData['cbBar']) ? prepBaseForm(prepOtherArray($formData), 'bar',
            'Option bar', 'Optional argument') : '');

}

The function accepts a formdata array as argument, and depending on the contained values it generates the corresponding arrays in session; 该函数接受formdata数组作为参数,并根据包含的值在会话中生成相应的数组; this happens only after checking the existence of the matching flag, hence the issets and the ternary operator. 只有在检查匹配标志的存在之后才会发生这种情况,因此是issets和三元运算符。

The called function, prepBaseForm, separates the appending of the second and third arguments, corresponding to a filename and a friendly name, to the first argument, which is an array; 被调用函数prepBaseForm将第二个和第三个参数(对应于文件名和友好名称)的附加分隔到第一个参数,即数组; it also accepts an optional fourth parameter, indicating a file to concatenate to the filled form. 它还接受一个可选的第四个参数,指示要连接到填充表单的文件。

If the value is not present in the array, the code intentionally clears the corresponding session variable to '' in order to keep the generation order intact (to this end: is there a better way of doing this, considering that there are scenarios in which the given generation order will be broken?). 如果数组中不存在该值,则代码会故意将相应的会话变量清除为''以保持生成顺序不变(为此:有更好的方法可以做到这一点,考虑到有些情况,其中给定的生成顺序会被打破吗?)。

The actual code has about twenty generation blocks with this format, and it's getting quite lengthy; 实际的代码有大约二十个具有这种格式的生成块,并且它变得非常冗长; one optimization I thought of was to loop through the given fields and generalize the generation process, but as you can see, many times the arguments differ, or the input array for the prepBaseForm function has to be generated another way. 我想到的一个优化是循环遍历给定的字段并概括生成过程,但正如您所看到的,很多时候参数不同,或者必须以另一种方式生成prepBaseForm函数的输入数组。 Any ideas on how I could tackle this refactoring? 关于如何解决这种重构的任何想法? Thanks in advance for your response. 在此先感谢您的回复。

One option would be to provide defaults, and just run it if the values are changed using normal conditionals. 一种选择是提供默认值,如果使用正常条件更改值,则运行它。 Then you can clean up further by putting everything into a simple loop.: 然后你可以通过将所有内容放入一个简单的循环来进一步清理:

function prepOptional($formData) {
    $baseInfo=getBaseInfo();

    $options = array(
        'foo' => 'Option foo', 
        'opt1' => 'Option Opt1',
        'bar' => 'Option Bar',
    );
    foreach ($options as $name => $text) {
        //Add defaults for formData and Session arrays)
        $formData += array('cb' . ucfirst($name) => '');
        $_SESSION += array($name . 'Data' => '');

        if ($formData['cb' . ucfirst($name)]) {
            $_SESSION[$name . 'Data'] = prepBaseForm(
                $baseInfo, 
                $name, 
                $text
            );
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM