Completed
Push — master ( 0d3ad6...ebce91 )
by satoru
06:24
created

EntityColumnCheckTrait::getEntityColumnCheck()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 45
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 4.909
c 0
b 0
f 0
cc 9
eloc 21
nc 7
nop 1
1
<?php
2
3
namespace EntityColumnCheck\Model\Entity;
4
5
use Cake\Network\Exception\InternalErrorException;
6
use Cake\Utility\Inflector;
7
8
trait EntityColumnCheckTrait
9
{
10
    /**
11
     * getEntityColumnCheck
12
     * @author hagiwara
13
     */
14
    private function getEntityColumnCheck($property)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
15
    {
16
        // 設定されていなくても例外的にOKとするメソッド
17
        $exceptOkProperties = [
18
            'entityColumnCheckAllowField',
19
            '_method',
20
        ];
21
        // フィールド許可用のメソッドは対象外とする
22
        if (in_array($property, $exceptOkProperties, true)) {
23
            return;
24
        }
25
26
        // 新規の場合はチェックしない
27
        if ($this->isNew()) {
0 ignored issues
show
Bug introduced by
It seems like isNew() 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...
28
            return;
29
        }
30
31
        // プロパティに値がいる場合はOK
32
        if (array_key_exists($property, $this->_properties)) {
0 ignored issues
show
Bug introduced by
The property _properties 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...
33
            return;
34
        }
35
36
        // プロパティが設定されている場合はOK
37
        if (property_exists($this, $property)) {
38
            return;
39
        }
40
41
        // getterがセットされている場合はOK
42
        $snakeMethod = '_get' . Inflector::underscore($property);
43
        $titleMethod = '_get' . ucfirst($property);
44
        if (
45
            method_exists($this, $snakeMethod ) ||
46
            method_exists($this, $titleMethod )
47
        ) {
48
            return;
49
        }
50
51
        // entityColumnCheckAllowFieldに値がセットされている場合はOKとする
52
        if (is_array($this->entityColumnCheckAllowField) && in_array($property, $this->entityColumnCheckAllowField)) {
0 ignored issues
show
Bug introduced by
The property entityColumnCheckAllowField 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...
53
            return;
54
        }
55
56
       // 上記条件に当てはまらない場合はException
57
        throw new InternalErrorException('invalid entity(' . get_class($this) . ') paramater(' . $property . ')');
58
    }
59
}
60