forked from nelmio/alice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectionWithoutConstructorInstantiator.php
52 lines (44 loc) · 1.41 KB
/
ReflectionWithoutConstructorInstantiator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/*
* This file is part of the Alice package.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\Alice\Instances\Instantiator\Chainable;
use Nelmio\Alice\Fixtures\Fixture;
use Nelmio\Alice\Instances\Instantiator\ChainableInstantiatorInterface;
final class ReflectionWithoutConstructorInstantiator implements ChainableInstantiatorInterface
{
/**
* @inheritdoc
*/
public function instantiate(Fixture $fixture)
{
$reflectionClass = new \ReflectionClass($fixture->getClass());
return $reflectionClass->newInstanceWithoutConstructor();
}
/**
* @inheritdoc
*/
public function canInstantiate(Fixture $fixture): bool
{
if (false === $fixture->shouldUseConstructor()) {
return true;
}
try {
$reflectionMethod = new \ReflectionMethod($fixture->getClass(), '__construct');
return (
false === $reflectionMethod->isPublic()
&& '__construct' === $fixture->getConstructorMethod()
);
} catch (\ReflectionException $exception) {
// thrown when __construct does not exist, i.e. is default constructor
return false;
} catch (\Exception $exception) {
return false;
}
}
}