Failed Conditions
Pull Request — experimental/sf (#3236)
by Kentaro
49:41 queued 37:58
created

AbstractMasterEntity::getConstantValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.4624

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 19
ccs 3
cts 11
cp 0.2727
crap 6.4624
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Entity\Master;
15
16
use Doctrine\ORM\Mapping as ORM;
17
18
/**
19
 * AbstractMasterentity
20
 *
21
 * @ORM\MappedSuperclass
22
 */
23
abstract class AbstractMasterEntity extends \Eccube\Entity\AbstractEntity
24
{
25
    /**
26
     * @return string
27
     */
28 210
    public function __toString()
29
    {
30 210
        return (string) $this->getName();
31
    }
32
33
    /**
34
     * @var int
35
     *
36
     * @ORM\Column(name="id", type="smallint", options={"unsigned":true})
37
     * @ORM\Id
38
     * @ORM\GeneratedValue(strategy="NONE")
39
     */
40
    protected $id;
41
42
    /**
43
     * @var string
44
     *
45
     * @ORM\Column(name="name", type="string", length=255)
46
     */
47
    protected $name;
48
49
    /**
50
     * @var int
51
     *
52
     * @ORM\Column(name="sort_no", type="smallint", options={"unsigned":true})
53
     */
54
    protected $sort_no;
55
56
    /**
57
     * Set id.
58
     *
59
     * @param int $id
60
     *
61
     * @return $this
62
     */
63 25
    public function setId($id)
64
    {
65 25
        $this->id = $id;
66
67 25
        return $this;
68
    }
69
70
    /**
71
     * Get id.
72
     *
73
     * @return int
74
     */
75 447
    public function getId()
76
    {
77 447
        return $this->id;
78
    }
79
80
    /**
81
     * Set name.
82
     *
83
     * @param string $name
84
     *
85
     * @return $this
86
     */
87 14
    public function setName($name)
88
    {
89 14
        $this->name = $name;
90
91 14
        return $this;
92
    }
93
94
    /**
95
     * Get name.
96
     *
97
     * @return string
98
     */
99 240
    public function getName()
100
    {
101 240
        return $this->name;
102
    }
103
104
    /**
105
     * Set sortNo.
106
     *
107
     * @param int $sortNo
108
     *
109
     * @return $this
110
     */
111 14
    public function setSortNo($sortNo)
112
    {
113 14
        $this->sort_no = $sortNo;
114
115 14
        return $this;
116
    }
117
118
    /**
119
     * Get sortNo.
120
     *
121
     * @return int
122
     */
123 10
    public function getSortNo()
124
    {
125 10
        return $this->sort_no;
126
    }
127
128
    public function __get($name)
129
    {
130
        return self::getConstantValue($name);
131
    }
132
133 1
    public function __set($name, $value)
134
    {
135 1
        throw new \InvalidArgumentException();
136
    }
137
138 1
    public static function __callStatic($name, $arguments)
139
    {
140 1
        return self::getConstantValue($name);
141
    }
142
143 1
    protected static function getConstantValue($name)
144
    {
145 1
        if (in_array($name, ['id', 'name', 'sortNo'])) {
146 1
            throw new \InvalidArgumentException();
147
        }
148
        // see also. http://qiita.com/Hiraku/items/71e385b56dcaa37629fe
149
        $class = get_class(new static());
150
        $ref = new \ReflectionClass($class);
151
        // クラス定数が存在していれば, クラス定数から値を取得する
152
        $constants = $ref->getConstants();
153
        if (array_key_exists($name, $constants)) {
154
            return $constants[$name];
155
        }
156
        // XXX $obj = new static(); とすると segmentation fault が発生するため, リフレクションで値を取得する
157
        $refProperty = $ref->getProperty($name);
158
        $refProperty->setAccessible(true);
159
160
        return $refProperty->getValue(new $class());
161
    }
162
}
163