TextRepository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 64
loc 64
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A model() 8 8 2
A updateValue() 20 20 1
B autosave() 22 22 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Text\Repositories\Eloquent;
12
13
use Longman\Platfourm\Contracts\Repository\Repository;
14
use Longman\Platfourm\Contracts\Repository\RepositoryCriteria;
15
use Longman\Platfourm\Repository\Eloquent\BaseRepository;
16
use Longman\Platfourm\Repository\Events\RepositoryEntityUpdated;
17
use Longman\Platfourm\Text\Models\Eloquent\Text;
18
19 View Code Duplication
class TextRepository extends BaseRepository implements Repository, RepositoryCriteria
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
22
    public function model()
23
    {
24
        if (class_exists(\App\Models\Text::class)) {
25
            return \App\Models\Text::class;
26
        }
27
28
        return Text::class;
29
    }
30
31
    /**
32
     * Update a entity in repository by id.
33
     *
34
     * @param  array $attributes
0 ignored issues
show
Bug introduced by
There is no parameter named $attributes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
35
     * @param        $id
36
     * @return mixed
37
     */
38
    public function updateValue(array $options, $value)
39
    {
40
        $this->applyScope();
41
        $_skipPresenter = $this->skipPresenter;
42
43
        $this->skipPresenter(true);
44
45
        $model = $this->model->where($options)->first();
46
47
        $model->setAttribute('value', $value);
48
49
        $model->saveOrFail();
50
51
        $this->skipPresenter($_skipPresenter);
52
        $this->resetModel();
53
54
        event(new RepositoryEntityUpdated($this, $model));
55
56
        return $this->parserResult($model);
57
    }
58
59
    public function autosave($locales, $lang, $scope, array $data)
0 ignored issues
show
Unused Code introduced by
The parameter $lang is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        $ins          = [];
62
        $ins['scope'] = $scope;
63
        foreach ($data as $key) {
64
            foreach ($locales as $locale => $locale_data) {
65
                $model  = $this->model->newInstance();
66
                $exists = $model->where(['key' => $key, 'lang' => $locale])->exists();
67
                if ($exists) {
68
                    continue;
69
                }
70
71
                $ins['key']   = $key;
72
                $ins['lang']  = $locale;
73
                $ins['value'] = $key;
74
                $model->fill($ins);
75
                $model->save();
76
                $this->resetModel();
77
            }
78
        }
79
        return true;
80
    }
81
82
}
83