ArrayAsPrimary::getKeyName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\Database\Eloquent\Traits;
12
13
use Illuminate\Database\Eloquent\Builder;
14
15
trait ArrayAsPrimary
16
{
17
18
    /**
19
     * Get key name
20
     *
21
     * @return mixed
22
     */
23
    public function getKeyName()
24
    {
25
        return $this->primaryKey[0];
0 ignored issues
show
Bug introduced by
The property primaryKey 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...
26
    }
27
28
    /**
29
     * Set the keys for a save update query.
30
     *
31
     * @param  \Illuminate\Database\Eloquent\Builder $query
32
     * @return \Illuminate\Database\Eloquent\Builder
33
     */
34 View Code Duplication
    protected function setKeysForSaveQuery(Builder $query)
0 ignored issues
show
Duplication introduced by
This method 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...
35
    {
36
        $key = $this->getKeyName();
37
38
        if (is_array($key)) {
39
            foreach ($key as $k) {
40
                $query->where($k, '=', $this->getKeyValueForSaveQuery($k));
41
            }
42
        } else {
43
            $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());
0 ignored issues
show
Bug introduced by
The method getKeyForSaveQuery() does not exist on Longman\Platfourm\Databa...t\Traits\ArrayAsPrimary. Did you maybe mean setKeysForSaveQuery()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
44
        }
45
46
        return $query;
47
    }
48
49
    /**
50
     * Get the primary key value for a save query.
51
     *
52
     * @return mixed
53
     */
54
    protected function getKeyValueForSaveQuery($key)
55
    {
56
        if (isset($this->original[$key])) {
57
            return $this->original[$key];
0 ignored issues
show
Bug introduced by
The property original 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...
58
        }
59
60
        return $this->getAttribute($key);
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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...
61
    }
62
63
}
64