ReflectionHelper   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 20
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A allowAccessToProperty() 0 13 1
1
<?php
2
3
namespace Sunnysideup\DashboardWelcomeQuicklinks\Api;
4
5
use ReflectionClass;
6
7
/**
8
 * usage
9
 * ```php
10
 *     $myObject = ReflectionHelper::allowAccessToProperty(MyClass::class, 'myProtectedProperty');
11
 *     $myObject->myProtectedProperty = 'new value';
12
 * ```
13
 */
14
15
class ReflectionHelper
16
{
17
    /**
18
     * returns a new instance of a class with the protected property available for changing.
19
     *
20
     * @return object object of the classname provided
21
     */
22
    public static function allowAccessToProperty(string $className, string $propertyName)
23
    {
24
        // Create a reflection object for the class
25
        $reflectionClass = new ReflectionClass($className);
26
27
        // Get the protected property
28
        $property = $reflectionClass->getProperty($propertyName);
29
30
        // Make the property accessible
31
        $property->setAccessible(true);
32
33
        // Set the value of the protected property
34
        return new $className();
35
    }
36
}
37