【问题标题】:Either remove this useless object instantiation of class "EligibilityImport" or use it要么删除类“EligibilityImport”的这个无用的对象实例化,要么使用它
【发布时间】:2021-11-23 07:48:04
【问题描述】:

我正在使用 SonarQube 扫描我的 Laravel 应用程序,但它不喜欢以下代码:

class EligibilityImportJob implements ShouldQueue
{
    use Dispatchable,
        InteractsWithQueue,
        Queueable,
        SerializesModels;

    /** @var string */
    protected $file;

    /** @var int */
    protected $mode;

    public function __construct(string $file, $mode)
    {
        $this->file = $file;
        $this->mode = $mode;
    }

    public function handle(): void
    {
        $file = $this->file;
        $mode = $this->mode;

        new EligibilityImport($file, $mode); // Doesn't like this line
    }
}

它给了我以下错误:Either remove this useless object instantiation of class "EligibilityImport" or use it。我怎样才能解决这个问题?下面是 EligibilityImport 类,它从数据库中导入或删除数据,来自 CSV 文件:

final class EligibilityImport
{
    const MODE_APPEND = 1;
    const MODE_PURGE = 2;

    const MODES = [
        self::MODE_APPEND => 'append',
        self::MODE_PURGE => 'purge'
    ];

    /** @var string */
    protected $file;

    /** @var int */
    protected $mode;

    /** @var array */
    protected $cache = [];

    public function __construct($file, $mode = self::MODE_APPEND)
    {
        $this->file = $file;
        $this->mode = $mode;

        $this->process();
    }

    protected function process()
    {
        $file = $this->file;
        $mode = $this->mode;
        $path = storage_path('app/imports/' . $file);

        if (is_file($path)) {
            $csv = Reader::createFromPath($path, 'r');
            $csv->setHeaderOffset(0);

            $records = $csv->getRecords();

            foreach ($records as $record) {
                $companyName = $record['company'] ?? null;

                if ( ! empty($companyName)) {
                    $company = $this->cache['companies'][$companyName] ?? null;

                    if (empty($company)) {
                        $company = Company::where('name', $companyName)->first();

                        if ($company !== null) {
                            $this->cache['companies'][$companyName] = $company;
                        }
                    }

                    if ($company !== null) {
                        $eligibility = null;
                        $skip = false;

                        $firstName = $record['first_name'] ?? null;
                        $lastName = $record['last_name'] ?? null;
                        $email = $record['email'] ?? null;
                        $ein = $record['ein'] ?? null;

                        if ( ! empty($email)) {
                            $eligibility = $company
                                ->eligibilities()
                                ->where('email_hash', sha1($email))
                                ->first();

                            if ($eligibility !== null) {
                                $skip = true;

                                if ($mode == self::MODE_PURGE) {
                                    $eligibility->delete();
                                }
                            }
                        }

                        if ( ! empty($ein)) {
                            $eligibility = $company
                                ->eligibilities()
                                ->where('ein_hash', sha1($ein))
                                ->first();

                            if ($eligibility !== null) {
                                $skip = true;

                                if ($mode == self::MODE_PURGE) {
                                    $eligibility->delete();
                                }
                            }
                        }

                        if ($mode == self::MODE_APPEND && ! $skip) {

                            if ( ! empty($firstName) && ! empty($lastName) && ( ! empty($email) || ! empty($ein))) {
                                $eligibility = new Eligibility();
                                $eligibility->fill($record);

                                $company->eligibilities()->save($eligibility);
                            }
                        }
                    }
                }
            }

            @unlink($path);
        }
    }
}

【问题讨论】:

  • 你是对的。我最初输入了另一个错误的代码。我已经更新了原始问题并提供了更多代码供您查看。
  • 谢谢。实际上,您现在已经发布了正确的代码。请看下面我的回答。 :-) 看看为什么以 MRE 的形式发布正确的代码会有所作为?

标签: laravel sonarqube sonarscanner


【解决方案1】:

我根本不使用 Laravel 或 Sonar,但我可以看到编译器正确地抱怨您指出的行。

public function handle(): void
{
    $file = $this->file;
    $mode = $this->mode;

    new EligibilityImport($file, $mode); // Doesn't like this line
}

它之所以抱怨是因为您正在创建一个 EligibilityImport 的新实例,然后立即将其丢弃,这意味着根本没有理由首先创建它(正如编译器消息所说)。如果你读到了

要么删除这个“EligibilityImport”类的无用对象实例化,要么使用它。

编译器准确地告诉你我写了什么 - 要么完全删除那行代码,要么对你正在创建的对象实例做一些事情,比如保存对它的引用。

创建对象实例的正确方法是表单

someVar = new SombObject()

创建新实例并存储对它的引用以在后续代码中使用。如果您需要使用该实例,则需要保存该引用,否则您将无法对其进行任何操作。如果您不需要使用它,请完全删除该行代码,因为它没有意义(在某些语言中可能是内存泄漏)。

【讨论】:

    猜你喜欢
    • 2021-02-11
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多