Passed
Push — master ( a1929c...27d925 )
by Dispositif
02:28
created

WorkerAnalyzedTitlesTrait::checkAlreadyAnalyzed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Application\Traits;
11
12
use Throwable;
13
14
/*
15
 * todo extract to service/adapter/infra
16
 */
17
trait WorkerAnalyzedTitlesTrait
18
{
19
    protected function initializePastAnalyzedTitles(): void
20
    {
21
        try {
22
            $analyzed = file(
23
                static::ARTICLE_ANALYZED_FILENAME,
0 ignored issues
show
Bug introduced by
The constant App\Application\Traits\W...TICLE_ANALYZED_FILENAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
24
                FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
25
            );
26
        } catch (Throwable $e) {
27
            $this->log->critical("Can't parse ARTICLE_ANALYZED_FILENAME : " . $e->getMessage());
28
            $analyzed = [];
29
        }
30
        $this->pastAnalyzed = ($analyzed !== false) ? $analyzed : [];
1 ignored issue
show
introduced by
The condition $analyzed !== false is always true.
Loading history...
Bug Best Practice introduced by
The property pastAnalyzed does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
    }
32
33
    protected function memorizeAndSaveAnalyzedTitle(string $title): void
34
    {
35
        if (!$this->checkAlreadyAnalyzed($title)) {
36
            $this->pastAnalyzed[] = $title; // skip doublon title
1 ignored issue
show
Bug Best Practice introduced by
The property pastAnalyzed does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
            @file_put_contents(static::ARTICLE_ANALYZED_FILENAME, $title . PHP_EOL, FILE_APPEND);
1 ignored issue
show
Bug introduced by
The constant App\Application\Traits\W...TICLE_ANALYZED_FILENAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
        }
39
    }
40
41
    protected function checkAlreadyAnalyzed(string $title): bool
42
    {
43
        return is_array($this->pastAnalyzed) && in_array($title, $this->pastAnalyzed);
44
    }
45
}