TypeFromSetterTrait::getTypeFromSetter()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 10
cp 0.8
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 5
nop 2
crap 5.2
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