TypeFromSetterTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 20
ccs 8
cts 10
cp 0.8
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getTypeFromSetter() 0 18 5
1
<?php
2
3
namespace Vox\Metadata\Driver;
4
5
use Exception;
6
use Metadata\ClassMetadata;
7
use Metadata\MethodMetadata;
8
use Metadata\PropertyMetadata;
9
10
/**
11
 * snipet to retreave a property type through it's setter type hint
12
 * 
13
 * @author Jhonatan Teixeira <[email protected]>
14
 */
15
trait TypeFromSetterTrait
16
{
17 28
    private function getTypeFromSetter(PropertyMetadata $propertyMetadata, ClassMetadata $classMetadata)
18
    {
19 28
        $setterName = sprintf('set%s', ucfirst($propertyMetadata->name));
20
        
21 28
        $setter = $classMetadata->methodMetadata[$setterName] ?? null;
22
        
23 28
        if ($setter instanceof MethodMetadata) {
24 15
            $params = $setter->reflection->getParameters();
25
            
26 15
            if (count($params) == 0) {
27
                throw new Exception("setter method {$classMetadata->name}:{$setterName} has no params");
28
            }
29
            
30 15
            if (count($params) > 1) {
31
                throw new Exception("setter method {$classMetadata->name}:{$setterName} has more than one param");
32
            }
33
            
34 15
            return $params[0]->getClass() ? $params[0]->getClass()->name : null;
35
        }
36 24
    }    
37
}
38