VocabCite   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A terms() 0 9 1
A __construct() 0 6 2
A sweep() 0 19 5
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