杀死函数内部的脚本是不好的做法吗?

用户名

我指的是将die()函数用于除调试以外的其他操作。这是一种“行之有效”的情况,但这是不正确的做法吗?

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;

/**
 * This Command will help Cjw create a new demo site to start off
 * with the Multisite bundle.
 *
 * Class CreateSiteCommand
 * @package Cjw\GeneratorBundle\Command
 */
class RemoveSiteCommand extends ContainerAwareCommand
{
    private $vendor; //eg "Cjw"
    private $fullBundleName; //eg "SiteCjwNetworkBundle"
    private $fullBundleNameWithVendor; //eg "CjwSiteCjwNetworkBundle"
    (more vars)

    /**
     * this function is used to configure the Symfony2 console commands
     */
    protected function configure()
    {
        $this
            ->setName('cjw:delete-site')
            ->setDescription('Delete Cjw Site')
            ->addOption('db_user', null, InputOption::VALUE_REQUIRED, 'If set, the database user will be shown on the instructions');
    }

    /**
     * This function executes the code after the command input has been validated.
     *
     * @param InputInterface $input gets the user input
     * @param OutputInterface $output shows a message on the command line
     * @return int|null|void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // set optional db_username

        $dialog = $this->getHelper('dialog');
        $reusable = new \Reusable();
        $fs = new Filesystem();
        $cmd = new \ColorOutput();

        //push only vendors into the vendors array
        $vendors = $reusable->getFileList($reusable->getMainDirectory('src'),true);


        //select a vendor from the array
        $vendor = $dialog->select(
            $output,
            'Select your vendor [1]: ',
            $vendors,
            1
        );

        $bundles_in_vendor = $reusable->getFileList($reusable->getMainDirectory('src/'.$vendors[$vendor]),true);

        //push bundles that start with 'Site' into array
        $sites = array();

        foreach($bundles_in_vendor as $bundle)
        {
            if($reusable->startsWith($bundle,'Site'))
            {
                array_push($sites,$bundle);
            }
        }

        $site_to_delete = $dialog->select(
            $output,
            'Select site to remove: ',
            $sites,
            1
        );

        $bundle_deletion_path = $reusable->getMainDirectory('src/'.$vendors[$vendor]).'/'.$sites[$site_to_delete];

        $are_you_sure = array('yes','no');
        $confirmation = $dialog->select(
            $output,
            'Are you sure you want to delete: '.$sites[$site_to_delete],
            $are_you_sure,
            1
        );

        if($are_you_sure[$confirmation] == 'yes')
        {
            echo $cmd->color('yellow','attempting to remove bundle in: '.$bundle_deletion_path);
            $fs->remove($bundle_deletion_path);

            //returns demo
            $sitename = strtolower($sites[$site_to_delete]);
            $sitename = substr($sitename,0,-6);
            $sitename = substr($sitename,4);
            $this->setRawSiteNameInput($sitename);

            // returns acmedemo
            $symlinkname =  strtolower($vendors[$vendor].substr($sites[$site_to_delete],0,-6));
            $this->removeSymlinks($symlinkname,$this->getRawSiteNameInput());

            $this->createSetters($vendor,substr($sites[$site_to_delete],0,-6));

            $this->deleteEzPublishExtension();
            echo $this->getFullLegacyPath();

            echo $cmd->color('green','deletion process completed.');
        }
        else
        {
            echo "deletion canceled";
            die();
        }

        function_that_further_deletion_process();
    }

这是一个symfony2控制台脚本,用于从特定结构中删除网站

arkascha

如果您要问这个问题,那是绝对安全的,因为php作为一种准解释语言,在终止执行时不会留下任何痕迹或工件。

如果这是一个好习惯,那是另一回事。我想说它可以很好地用于测试目的,但是您应该在最终代码中避免使用它。原因是它使代码难以维护。考虑其他人深入研究您的代码并试图理解其逻辑。实际上,必须仔细阅读所有代码才能了解这一细节。可能是一个没有的机会,因此您的代码的行为似乎已损坏。

而是尝试执行以下操作之一:

  • 抛出异常以离开当前范围。这样的异常很可能会被调用范围捕获和吞噬,但这是一种清晰且可预测的返回方式。显然,您应该记录这种行为。

  • 返回的值显然超出了“通常”返回的范围。因此,例如nullfalse代替一个典型值。这将强制调用范围检查返回值,但这仍然是一个好习惯。

  • 重新组织代码,以便没有理由突然终止执行。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

让构造函数返回Promise是不好的做法吗?

重载基类函数是不好的做法吗?

从 if 条件中调用函数是不好的做法吗?

导出包含函数的默认函数是不好的做法吗?

使用“右”,“左”,“上”,“下”来放置文本是不好的做法吗?

隐藏函数中的内存分配是不好的做法吗?

函数返回不同类型是不好的做法吗?

在构造函数中使用 FormBuilder 是不好的做法吗?

向构造函数注入几个参数是不好的做法吗?

在自己的构造函数中将“ this”作为参数传递是不好的做法吗?

在NodeJS的构造函数中使用`require`是不好的做法吗?

循环依赖是不好的做法吗?

“抛出异常”是不好的做法吗?

NULL参数是不好的做法吗?

Optional<List> 是不好的做法吗?

这是MVVM不好的做法吗?

Java Reflection是不好的做法吗?

多态关系是不好的做法吗?

在Gemfile和.ruby-version Dotfile中都列出Ruby版本是一种不好的做法吗?

将强UI相关状态保持在react组件内部是不好的做法吗?

将运行时异常与内部经过检查的异常一起抛出是不好的做法吗?

在调用方法内部声明变量是一种不好的做法吗?

如果有条件,则为内部变量分配值-总是不好的做法吗?

让Jenkins Docker容器在Kubernetes集群本身内部处理CI / CD是不好的做法吗?

对我来说,在库中包含“内部”包对我来说是不好的做法吗?

在C ++中从构造函数中显式地调用析构函数是不好的做法吗?

这是多态吗,这是不好的做法吗?

通过循环反复检查整个脚本字典是不好的做法吗?VB6

使用内置函数名称作为属性或方法标识符是不好的做法吗?