94% of 293OPs |
96% of 50Lines |
91% of 64Branches |
1% of 700Paths |
Method | OPs | OPs % | Lines | Line % | Branches | Branches % | Paths | Path % |
---|---|---|---|---|---|---|---|---|
mageekguy\atoum\factory\builder\closure::build() | 226 | 92% | 41 | 95% | 50 | 90% | 694 | 1% |
mageekguy\atoum\factory\builder\closure::get() | 6 | 100% | 1 | 100% | 1 | 100% | 1 | 100% |
mageekguy\atoum\factory\builder\closure::addToAssertionManager() | 43 | 100% | 7 | 100% | 9 | 100% | 3 | 100% |
mageekguy\atoum\factory\builder\closure::isVariadic() | 18 | 100% | 1 | 100% | 4 | 75% | 2 | 50% |
# | |
---|---|
1 |
<?php |
2 |
|
3 |
namespace mageekguy\atoum\factory\builder; |
4 |
|
5 |
use |
6 |
mageekguy\atoum\test, |
7 |
mageekguy\atoum\factory |
8 |
; |
9 |
|
10 |
class closure implements factory\builder |
11 |
{ |
12 |
private $factory = null; |
13 |
private $allArgumentsAreOptional = true; |
14 |
|
15 |
public function build(\reflectionClass $class, & $instance = null)95% |
16 |
{ |
17 |
$this->factory = null; |
18 |
|
19 |
if ($class->isInterface() === false && $class->isAbstract() === false) |
20 |
{ |
21 |
$constructor = $class->getConstructor(); |
22 |
|
23 |
if ($constructor === null || $constructor->isPublic() === true) |
24 |
{ |
25 |
$constructorParameters = $closureParameters = array(); |
26 |
|
27 |
if ($constructor !== null) |
28 |
{ |
29 |
$this->allArgumentsAreOptional = ($constructor->getNumberOfRequiredParameters() === 0); |
30 |
|
31 |
foreach ($constructor->getParameters() as $position => $parameter) |
32 |
{ |
33 |
$closureParameters[$position] = ($parameter->isPassedByReference() === false ? '' : '& ') . $constructorParameters[$position] = '$' . $parameter->getName(); |
34 |
|
35 |
if (self::isVariadic($parameter)) |
36 |
{ |
37 |
$closureParameters[$position] = '...' . $closureParameters[$position]; |
38 |
$constructorParameters[$position] = '...' . $constructorParameters[$position]; |
39 |
} |
40 |
|
41 |
switch (true) |
42 |
{ |
43 |
case $parameter->isDefaultValueAvailable(): |
44 |
$defaultValue = var_export($parameter->getDefaultValue(), true); |
45 |
break; |
46 |
|
47 |
case $parameter->isOptional() && self::isVariadic($parameter) === false: |
48 |
$defaultValue = 'null'; |
49 |
break; |
50 |
|
51 |
default: |
52 |
$defaultValue = null; |
53 |
} |
54 |
|
55 |
if ($defaultValue !== null) |
56 |
{ |
57 |
$closureParameters[$position] .= ' = ' . $defaultValue; |
58 |
} |
59 |
} |
60 |
} |
61 |
|
62 |
if ($constructor === null || sizeof($closureParameters) <= 0) |
63 |
{ |
64 |
$this->factory = function() use (& $instance, $class) { return ($instance = $class->newInstanceArgs(func_get_args())); }; |
65 |
} |
66 |
else |
67 |
{ |
68 |
$this->factory = eval('return function(' . join(', ', $closureParameters) . ') use (& $instance) { return ($instance = new ' . $class->getName() . '(' . join(', ', $constructorParameters) . ')); };'); |
69 |
} |
70 |
} |
71 |
} |
72 |
|
73 |
return $this; |
74 |
} |
75 |
|
76 |
public function get()100% |
77 |
{ |
78 |
return $this->factory; |
79 |
} |
80 |
|
81 |
public function addToAssertionManager(test\assertion\manager $assertionManager, $factoryName, $defaultHandler)100% |
82 |
{ |
83 |
if ($this->factory === null) |
84 |
{ |
85 |
$assertionManager->setHandler($factoryName, $defaultHandler); |
86 |
} |
87 |
else |
88 |
{ |
89 |
$assertionManager |
90 |
->setMethodHandler($factoryName, $this->factory) |
91 |
->setPropertyHandler($factoryName, $this->allArgumentsAreOptional === false ? $defaultHandler : $this->factory) |
92 |
; |
93 |
} |
94 |
|
95 |
return $this; |
96 |
} |
97 |
|
98 |
private static function isVariadic(\reflectionParameter $parameter)100% |
99 |
{ |
100 |
return (method_exists($parameter, 'isVariadic') && $parameter->isVariadic()); |
101 |
} |
102 |
} |