Completed
Push — 4.0 ( 87d096...bcc1be )
by Kiyotaka
05:44 queued 11s
created

src/Eccube/Util/EntityUtil.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.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\Util;
15
16
use Doctrine\ORM\EntityNotFoundException;
17
use Doctrine\ORM\Proxy\Proxy;
18
19
class EntityUtil
20
{
21
    /**
22
     * LAZY loading したエンティティの有無をチェックする.
23
     *
24
     * 削除済みのエンティティを LAZY loading した場合、 soft_delete filter で
25
     * フィルタリングされてしまい、正常に取得することができない.
26
     * しかし、 Proxy オブジェクトとして取得されるため、この関数を使用して
27
     * 有無をチェックする.
28
     * この関数を使用せず、該当のオブジェクトのプロパティを取得しようとすると、
29
     * EntityNotFoundException がスローされてしまう.
30
     *
31
     * @param $entity LAZY loading したエンティティ
32
     *
33
     * @return bool エンティティが削除済みの場合 true
34
     *
35
     * @see https://github.com/EC-CUBE/ec-cube/pull/602#issuecomment-125431246
36
     * @deprecated
37 5
     */
38
    public static function isEmpty($entity)
39 5
    {
40
        @trigger_error('The '.__METHOD__.' method is deprecated.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
41 1
        if ($entity instanceof Proxy) {
42
            try {
43
                $entity->__load();
44
            } catch (EntityNotFoundException $e) {
45
                return true;
46 1
            }
47
48 5
            return false;
49
        } else {
50
            return empty($entity);
51
        }
52
    }
53
54
    /**
55
     * LAZY loading したエンティティの有無をチェックする.
56
     *
57
     * EntityUtil::isEmpty() の逆の結果を返します.
58
     *
59
     * @param $entity
60
     *
61
     * @return bool
62
     *
63 4
     * @see EntityUtil::isEmpty()
64
     * @deprecated
65 4
     */
66
    public static function isNotEmpty($entity)
67
    {
68
        @trigger_error('The '.__METHOD__.' method is deprecated.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
69
70
        return !self::isEmpty($entity);
0 ignored issues
show
Deprecated Code introduced by
The method Eccube\Util\EntityUtil::isEmpty() has been deprecated.

This method has been deprecated.

Loading history...
71
    }
72
73
    /**
74
     * エンティティのプロパティを配列で返す.
75
     *
76
     * このメソッドはエンティティの内容をログ出力する際などに使用する.
77
     * AbstractEntity::toArray() と異なり再帰処理しない.
78
     * プロパティの値がオブジェクトの場合は、クラス名を出力する.
79 1
     *
80
     * @param object $entity 対象のエンティティ
81 1
     *
82 1
     * @return array エンティティのプロパティの配列
83 1
     */
84 1
    public static function dumpToArray($entity)
85 1
    {
86 1
        $objReflect = new \ReflectionClass($entity);
87 1
        $arrProperties = $objReflect->getProperties();
88 1
        $arrResults = [];
89
        foreach ($arrProperties as $objProperty) {
90
            $objProperty->setAccessible(true);
91 1
            $name = $objProperty->getName();
92
            $value = $objProperty->getValue($entity);
93
            $arrResults[$name] = is_object($value) ? get_class($value) : $value;
94
        }
95
96
        return $arrResults;
97
    }
98
}
99