/usr/share/php/Prophecy/Doubler/Doubler.php is in php-phpspec-prophecy 1.7.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | <?php
/*
* This file is part of the Prophecy.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
* Marcello Duarte <marcello.duarte@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Prophecy\Doubler;
use Doctrine\Instantiator\Instantiator;
use Prophecy\Doubler\ClassPatch\ClassPatchInterface;
use Prophecy\Doubler\Generator\ClassMirror;
use Prophecy\Doubler\Generator\ClassCreator;
use Prophecy\Exception\InvalidArgumentException;
use ReflectionClass;
/**
* Cached class doubler.
* Prevents mirroring/creation of the same structure twice.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class Doubler
{
private $mirror;
private $creator;
private $namer;
/**
* @var ClassPatchInterface[]
*/
private $patches = array();
/**
* @var \Doctrine\Instantiator\Instantiator
*/
private $instantiator;
/**
* Initializes doubler.
*
* @param ClassMirror $mirror
* @param ClassCreator $creator
* @param NameGenerator $namer
*/
public function __construct(ClassMirror $mirror = null, ClassCreator $creator = null,
NameGenerator $namer = null)
{
$this->mirror = $mirror ?: new ClassMirror;
$this->creator = $creator ?: new ClassCreator;
$this->namer = $namer ?: new NameGenerator;
}
/**
* Returns list of registered class patches.
*
* @return ClassPatchInterface[]
*/
public function getClassPatches()
{
return $this->patches;
}
/**
* Registers new class patch.
*
* @param ClassPatchInterface $patch
*/
public function registerClassPatch(ClassPatchInterface $patch)
{
$this->patches[] = $patch;
@usort($this->patches, function (ClassPatchInterface $patch1, ClassPatchInterface $patch2) {
return $patch2->getPriority() - $patch1->getPriority();
});
}
/**
* Creates double from specific class or/and list of interfaces.
*
* @param ReflectionClass $class
* @param ReflectionClass[] $interfaces Array of ReflectionClass instances
* @param array $args Constructor arguments
*
* @return DoubleInterface
*
* @throws \Prophecy\Exception\InvalidArgumentException
*/
public function double(ReflectionClass $class = null, array $interfaces, array $args = null)
{
foreach ($interfaces as $interface) {
if (!$interface instanceof ReflectionClass) {
throw new InvalidArgumentException(sprintf(
"[ReflectionClass \$interface1 [, ReflectionClass \$interface2]] array expected as\n".
"a second argument to `Doubler::double(...)`, but got %s.",
is_object($interface) ? get_class($interface).' class' : gettype($interface)
));
}
}
$classname = $this->createDoubleClass($class, $interfaces);
$reflection = new ReflectionClass($classname);
if (null !== $args) {
return $reflection->newInstanceArgs($args);
}
if ((null === $constructor = $reflection->getConstructor())
|| ($constructor->isPublic() && !$constructor->isFinal())) {
return $reflection->newInstance();
}
if (!$this->instantiator) {
$this->instantiator = new Instantiator();
}
return $this->instantiator->instantiate($classname);
}
/**
* Creates double class and returns its FQN.
*
* @param ReflectionClass $class
* @param ReflectionClass[] $interfaces
*
* @return string
*/
protected function createDoubleClass(ReflectionClass $class = null, array $interfaces)
{
$name = $this->namer->name($class, $interfaces);
$node = $this->mirror->reflect($class, $interfaces);
foreach ($this->patches as $patch) {
if ($patch->supports($node)) {
$patch->apply($node);
}
}
$this->creator->create($name, $node);
return $name;
}
}
|