VocabCite::terms()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TopviewDigital\TranslationHelper\Model;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class VocabCite extends Model
8
{
9
    protected $fillable = [
10
        'file', 'line', 'function', 'class', 'code',
11
    ];
12
13
    public function __construct(array $attributes = [])
14
    {
15
        $connection = config('trans-helper.database.connection') ?: config('database.default');
16
        $this->setConnection($connection);
17
        $this->setTable('_trans_helper_cites');
18
        parent::__construct($attributes);
19
    }
20
21
    public function terms()
22
    {
23
        return $this->belongsToMany(
24
            VocabTerm::class,
25
            '_trans_helper_links',
26
            'cited',
27
            'vocab',
28
            'id',
29
            'id'
30
        );
31
    }
32
33
    public function sweep()
34
    {
35
        $line = explode("\n", file_get_contents(base_path().$this->file));
0 ignored issues
show
Bug introduced by
The property file does not seem to exist on TopviewDigital\TranslationHelper\Model\VocabCite. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
36
        $line = $line[$this->line - 1] ?? '';
0 ignored issues
show
Bug introduced by
The property line does not seem to exist on TopviewDigital\TranslationHelper\Model\VocabCite. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
37
        $count = 0;
38
        if ($line) {
39
            foreach ($this->terms()->get() as $term) {
40
                $keywords = ["localize('{$term->term}')", 'localize("'.$term->term.'")'];
41
                $matched = array_filter(array_map(function ($u) use ($line) {
42
                    return strpos($line, $u);
43
                }, $keywords), 'strlen');
44
                if (empty($matched)) {
45
                    $this->terms()->detach($term->id);
46
                }
47
                $count += count($matched);
48
            }
49
        }
50
        if ($count == 0) {
0 ignored issues
show
introduced by
The condition $count == 0 is always true.
Loading history...
51
            $this->delete();
52
        }
53
    }
54
}
55