EntityColumnCheckTrait::getEntityColumnCheck()   C
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 49
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 49
rs 5.5471
cc 10
eloc 23
nc 8
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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