Completed
Push — master ( 49cc2f...b2199f )
by
unknown
02:53
created

TextsTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 64
ccs 0
cts 48
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C index() 0 39 7
A save() 0 20 4
1
<?php
2
3
namespace Longman\LaravelMultiLang;
4
5
use Illuminate\Http\Request;
6
use Longman\LaravelMultiLang\Models\Text;
7
8
trait TextsTrait
9
{
10
11
    public function index(Request $request)
12
    {
13
        $options['lang'] = config('multilang.default_locale');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
14
15
        if ($request->lang) {
16
            $options['lang'] = $request->lang;
1 ignored issue
show
Bug introduced by
The property lang does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
17
        }
18
19
        if ($request->keyword) {
20
            $options['keyword'] = $request->keyword;
1 ignored issue
show
Bug introduced by
The property keyword does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
21
        }
22
23
        if ($request->scope) {
24
            $options['scope'] = $request->scope;
1 ignored issue
show
Bug introduced by
The property scope does not seem to exist in Illuminate\Http\Request.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
25
        }
26
27
        $texts = Text::where(function ($q) use ($options) {
28
            foreach ($options as $k => $v) {
29
                if ($k == 'keyword') {
30
                    $q->where(function ($query) use ($v) {
31
                        $query->where('key', 'LIKE', '%' . $v . '%')->orWhere('value', 'LIKE', '%' . $v . '%');
32
                    });
33
                } else {
34
                    $q->where($k, $v);
35
                }
36
            }
37
        })->orderBy('value', 'asc')->get();
38
39
        if (isset($request->search)) {
40
            $options['search'] = true;
41
        }
42
43
        $options['keyword'] = $request->keyword;
44
45
        $data['texts'] = $texts;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
46
        $data['options'] = $options;
47
48
        return view($this->view, $data);
0 ignored issues
show
Bug introduced by
The property view does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
    }
50
51
    public function save(Request $request)
52
    {
53
        $this->validate($request, [
0 ignored issues
show
Bug introduced by
It seems like validate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
            'texts' => 'required|array',
55
        ]);
56
57
        $locales = array_keys(config('multilang.locales'));
58
        foreach ($request->texts as $lang => $items) {
59
            if (!in_array($lang, $locales)) {
60
                //to do must set errors
61
                continue;
62
            }
63
            foreach ($items as $key => $value) {
64
                Text::where('lang', $lang)
65
                    ->where('key', $key)
66
                    ->update(['value' => $value]);
67
            }
68
        }
69
        return redirect()->back();
70
    }
71
}
72