Completed
Pull Request — 2.4 (#2990)
by
unknown
06:20 queued 02:17
created

CloneTrait::clone()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Util;
15
16
/**
17
 * Clones given data if cloneable.
18
 *
19
 * @internal
20
 *
21
 * @author Quentin Barloy <[email protected]>
22
 */
23
trait CloneTrait
24
{
25
    /**
26
     * @param mixed $data
27
     *
28
     * @return mixed|null
29
     */
30
    public function clone($data)
31
    {
32
        if (\is_object($data)) {
33
            try {
34
                return (new \ReflectionClass(\get_class($data)))->isCloneable() ? clone $data : null;
35
            } catch (\ReflectionException $reflectionException) {
36
                return null;
37
            }
38
        }
39
40
        return $data;
41
    }
42
}
43