Test Failed
Push — main ( ea931b...8f4107 )
by Bingo
06:03
created

ReflectUtil::getResourceAsStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Jabe\Engine\Impl\Util;
4
5
use Jabe\Engine\ProcessEngineException;
6
use Jabe\Engine\Impl\ProcessEngineLogger;
7
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl;
8
use Jabe\Engine\Impl\Context\Context;
9
use Jabe\Engine\Impl\Util\EnsureUtil;
10
11
class ReflectUtil
12
{
13
    //private static final EngineUtilLogger LOG = ProcessEngineLogger.UTIL_LOGGER;
14
15
    private const CHAR_ENCODINGS = [
16
        "ä" => "%C3%A4",
17
        "ö" => "%C3%B6",
18
        "ü" => "%C3%BC",
19
        "Ä" => "%C3%84",
20
        "Ö" => "%C3%96",
21
        "Ü" => "%C3%9C"
22
    ];
23
24
    public static function getResourceAsStream(string $name)
25
    {
26
        $resourceStream = null;
27
        if (file_exists($name)) {
28
            $resourceStream = fopen($name, 'r+');
29
        }
30
        return $resourceStream;
31
    }
32
33
    public static function getResource(string $name): ?string
34
    {
35
        return file_exists($name) ? $name : null;
36
    }
37
38
    public static function getResourceUrlAsString(string $name): string
39
    {
40
        $url = self::getResource($name);
41
        foreach (self::CHAR_ENCODINGS as $key => $value) {
42
            $url = str_replace($key, $value, $url);
43
        }
44
        return $url;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $url could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
45
    }
46
47
    public static function instantiate(string $className)
48
    {
49
        try {
50
            return new $className();
51
        } catch (\Exception $e) {
52
            //throw LOG.exceptionWhileInstantiatingClass(className, e);
53
            throw new \Exception(sprintf("exceptionWhileInstantiatingClass %s", $className));
54
        }
55
    }
56
57
    /**
58
     * Returns the field of the given object or null if it doesnt exist.
59
     */
60
    public static function getField(string $fieldName, $object): ?\ReflectionProperty
61
    {
62
        try {
63
            $ref = new \ReflectionClass($object);
64
            return $ref->getProperty($fieldName);
65
        } catch (\Exception $e) {
66
            return null;
67
        }
68
    }
69
70
    public static function setField(\ReflectionProperty $field, $object, $value): void
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

70
    public static function setField(\ReflectionProperty $field, /** @scrutinizer ignore-unused */ $object, $value): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        try {
73
            $field->setValue($value);
74
        } catch (\Exception $e) {
75
            //throw LOG.exceptionWhileSettingField(field, object, value, e);
76
            throw $e;
77
        }
78
    }
79
80
    /**
81
     * Returns the setter-method for the given field name or null if no setter exists.
82
     */
83
    public static function getSetter(string $fieldName, string $clazz): ?\ReflectionMethod
0 ignored issues
show
Unused Code introduced by
The parameter $clazz is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

83
    public static function getSetter(string $fieldName, /** @scrutinizer ignore-unused */ string $clazz): ?\ReflectionMethod

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
        $setterName = self::buildSetterName($fieldName);
86
        try {
87
            // Using getMathods(), getMathod(...) expects exact parameter type
88
            // matching and ignores inheritance-tree.
89
            $ref = new \ReflectionClass($object);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $object seems to be never defined.
Loading history...
90
            $methods = $ref->getMethods();
91
            foreach ($methods as $method) {
92
                if ($method->name == $setterName) {
93
                    return $method;
94
                }
95
            }
96
            return null;
97
        } catch (\Exception $e) {
98
            //throw LOG.unableToAccessMethod(setterName, clazz.getName());
99
            throw new \Exception("unableToAccessMethod");
100
        }
101
    }
102
103
    private static function buildSetterName(string $fieldName): string
104
    {
105
        return "set" . strtoupper($fieldName[0]) . substr($fieldName, 1);
106
    }
107
108
    /**
109
     * Finds a method by name
110
     *
111
     * @param declaringType the name of the class
112
     * @param methodName the name of the method to look for
113
     */
114
    public static function getMethod(string $declaringType, string $methodName): ?\ReflectionMethod
115
    {
116
        try {
117
            $ref = new \ReflectionClass($declaringType);
118
            $methods = $ref->getMethods();
119
            foreach ($methods as $method) {
120
                if ($method->name == $methodName) {
121
                    return $method;
122
                }
123
            }
124
            return null;
125
        } catch (\Exception $e) {
126
            return null;
127
        }
128
    }
129
}
130