Passed
Pull Request — master (#275)
by
unknown
02:10
created

MagicMethodsTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 81.08%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
c 2
b 0
f 0
dl 0
loc 91
ccs 30
cts 37
cp 0.8108
rs 10
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
B __set() 0 17 7
A convertValueToExpectedType() 0 13 3
A methodExists() 0 3 1
B getExpectedTypeFromSetter() 0 32 9
A exists() 0 3 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: gareth
5
 * Date: 27-8-15
6
 * Time: 10:27
7
 */
8
9
namespace garethp\ews\API;
10
11
use garethp\ews\Caster;
0 ignored issues
show
Bug introduced by
The type garethp\ews\Caster was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
trait MagicMethodsTrait
14
{
15 37
    public function __set($name, $value)
16
    {
17 37
        if (is_object($value) && !($value instanceof Type) && property_exists($value, "Entry")) {
18 7
            $value = $value->Entry;
19
        }
20
21 37
        if ($this->methodExists("set" . ucfirst($name))) {
22 37
            $convertedValue = $this->convertValueToExpectedType($value, $name);
23 37
            $this->{"set" . ucfirst($name)}($convertedValue);
24 37
            return;
25
        }
26
27 5
        if (!$this->exists($name) && $this->exists(lcfirst($name))) {
28
            $name = lcfirst($name);
29
        }
30
31 5
        $this->$name = $value;
32
    }
33
34 36
    public function exists($name)
35
    {
36 36
        return property_exists($this, $name);
37
    }
38
39 37
    public function methodExists($name)
40
    {
41 37
        return method_exists($this, $name);
42
    }
43
44
    /**
45
     * Automatically convert stdClass objects to proper EWS types
46
     *
47
     * @param mixed $value The value to convert
48
     * @param string $propertyName The property name
49
     * @return mixed Converted value
50
     */
51 37
    private function convertValueToExpectedType($value, string $propertyName)
52
    {
53 37
        if (!TypeConverter::containsStdClass($value)) {
54 37
            return $value;
55
        }
56
        
57 2
        $expectedType = $this->getExpectedTypeFromSetter($propertyName);
58
        
59 2
        if ($expectedType) {
60 2
            return TypeConverter::convertToType($value, $expectedType);
61
        }
62
        
63
        return $value;
64
    }
65
66
    /**
67
     * Get expected type from setter method using reflection
68
     *
69
     * @param string $propertyName Property name
70
     * @return string|null Expected type class name
71
     */
72 2
    private function getExpectedTypeFromSetter(string $propertyName): ?string
73
    {
74 2
        $methodName = "set" . ucfirst($propertyName);
75
76 2
        if (!method_exists($this, $methodName)) {
77
            return null;
78
        }
79
80 2
        $reflection = new \ReflectionMethod($this, $methodName);
81 2
        $parameters = $reflection->getParameters();
82
        
83 2
        if (empty($parameters)) {
84
            return null;
85
        }
86
87 2
        $type = $parameters[0]->getType();
88
89
        // Handle union types (e.g., array|InternetHeaderType)
90 2
        if ($type instanceof \ReflectionUnionType) {
91 2
            foreach ($type->getTypes() as $unionType) {
92 2
                if (!$unionType->isBuiltin() && $unionType->getName() !== 'array') {
93 2
                    return $unionType->getName();
94
                }
95
            }
96
        }
97
98
        // Handle single types
99
        if ($type && !$type->isBuiltin()) {
100
            return $type->getName();
0 ignored issues
show
Bug introduced by
The method getName() does not exist on ReflectionType. It seems like you code against a sub-type of ReflectionType such as ReflectionNamedType. ( Ignorable by Annotation )

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

100
            return $type->/** @scrutinizer ignore-call */ getName();
Loading history...
101
        }
102
103
        return null;
104
    }
105
}
106