Passed
Push — master ( ff9c9e...a4cc29 )
by Robin
02:37
created

VocabCite   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A terms() 0 9 1
A sweep() 0 23 3
A __construct() 0 6 2
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(config('trans-helper.database.table.cite'));
18
        parent::__construct($attributes);
19
    }
20
21
    public function terms()
22
    {
23
        return $this->belongsToMany(
24
            config('trans-helper.model.term'),
25
            config('trans-helper.database.table.link'),
26
            'cited',
27
            'vocab',
28
            'id',
29
            'id'
30
        );
31
    }
32
33
    public function sweep()
34
    {
35
        $line = explode("\n", file_get_contents($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] ?? '';
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
        if ($line) {
38
            $terms = call_user_func_array(
39
                'array_merge',
40
                array_map(
41
                    function ($u) {
42
                        return ["localize('{$u->term}')", 'localize("' . $u->term . '")'];
43
                    },
44
                    $this->terms()->get()->all()
45
                )
46
            );
47
            $matched = array_filter(array_map(function ($u) use ($line) {
48
                return strpos($line, $u);
49
            }, $terms), 'strlen');
50
51
            if (!empty($matched)) {
52
                return;
53
            }
54
        }
55
        $this->delete();
56
    }
57
}
58