Completed
Push — master ( 31fba3...844081 )
by JHONATAN
02:27
created

PropertyMetadata::getClassUses()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Vox\Metadata;
4
5
use Metadata\PropertyMetadata as BaseMetadata;
6
use ProxyManager\Proxy\AccessInterceptorValueHolderInterface;
7
use ReflectionClass;
8
9
class PropertyMetadata extends BaseMetadata
10
{
11
    use AnnotationsTrait;
12
    
13
    public $type;
14
    
15
    /**
16
     * @param ReflectionClass $class
17
     * @param string $name
18
     */
19 18
    public function __construct($class, $name)
20
    {
21 18
        parent::__construct($class, $name);
22
        
23 18
        $this->type = $this->parseType();
24 18
    }
25
    
26 10
    public function getValue($obj)
27
    {
28 10
        if ($obj instanceof AccessInterceptorValueHolderInterface) {
29 5
            $obj = $obj->getWrappedValueHolderValue();
30
        }
31
        
32 10
        return parent::getValue($obj);
33
    }
34
    
35 15
    public function setValue($obj, $value)
36
    {
37 15
        if ($obj instanceof AccessInterceptorValueHolderInterface) {
38
            $obj = $obj->getWrappedValueHolderValue();
39
        }
40
        
41 15
        parent::setValue($obj, $value);
42 15
    }
43
    
44 18
    private function parseType()
45
    {
46 18
        $docComment = $this->reflection->getDocComment();
47
        
48 18
        preg_match('/@var\s+(([^\[\]\s]+)(\[\])?)/', $docComment, $matches);
49
        
50 18
        $fullType = $matches[1] ?? null;
51 18
        $type     = $matches[2] ?? null;
52
        
53 18
        if (null === $type) {
54 12
            return;
55
        }
56
        
57 17
        $uses = $this->getClassUses();
58
        
59 17
        foreach ($uses as $use) {
60 13
            if (preg_match("/{$type}$/", $use)) {
61
                return $use . ($matches[3] ?? null);
62
            }
63
            
64 13
            if (class_exists("$use\\$type")) {
65 13
                return "$use\\$type" . ($matches[3] ?? null);
66
            }
67
        }
68
        
69 15
        return $fullType;
70
    }
71
    
72 17
    private function getClassUses(): array
73
    {
74 17
        $filename = $this->reflection->getDeclaringClass()->getFileName();
75
        
76 17
        if (is_file($filename)) {
77 13
            $contents = file_get_contents($filename);
78
            
79 13
            preg_match_all('/use\s+(.*);/', $contents, $matches);
80
            
81 13
            $uses = $matches[1] ?? [];
82
            
83 13
            $matches = [];
84
            
85 13
            preg_match('/namespace\s+(.*);/', $contents, $matches);
0 ignored issues
show
Bug introduced by
It seems like $contents can also be of type false; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

85
            preg_match('/namespace\s+(.*);/', /** @scrutinizer ignore-type */ $contents, $matches);
Loading history...
86
            
87 13
            if (!empty($matches[1])) {
88 13
                array_push($uses, $matches[1]);
89
            }
90
            
91 13
            return $uses;
92
        }
93
        
94 5
        return [];
95
    }
96
}
97