Reflection::getReflectionProperty()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 26
rs 9.2222
1
<?php
2
/**
3
 * Closure for Craft CMS
4
 *
5
 * Allows you to use arrow function closures in Twig
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2022 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\closure\helpers;
12
13
use ReflectionException;
14
use ReflectionObject;
15
use ReflectionProperty;
16
17
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
18
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
19
 * @package   Closure
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
20
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
21
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
22
class Reflection
23
{
24
    // Public static Methods
25
    // =========================================================================
26
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
27
     * @param object $object
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
28
     * @param string $propertyName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
29
     *
30
     * @return ReflectionProperty
31
     * @throws ReflectionException
32
     */
33
    public static function getReflectionProperty(object $object, string $propertyName): ReflectionProperty
34
    {
35
        $reflectionObject = new ReflectionObject($object);
36
37
        $reflectionProperty = null;
38
39
        if ($reflectionObject->hasProperty($propertyName)) {
40
            $reflectionProperty = $reflectionObject->getProperty($propertyName);
41
        } else {
42
43
            // This is needed for private parent properties only.
44
            $parent = $reflectionObject->getParentClass();
45
            while ($reflectionProperty === null && $parent !== false) {
46
                if ($parent->hasProperty($propertyName)) {
47
                    $reflectionProperty = $parent->getProperty($propertyName);
48
                }
49
50
                $parent = $parent->getParentClass();
51
            }
52
        }
53
54
        if (!$reflectionProperty) {
55
            throw new ReflectionException("Property not found: " . $propertyName);
56
        }
57
58
        return $reflectionProperty;
59
    }
60
}
61