public function createPartialMock(ReflectionClass $class, $arguments = array())
{
$constructor = $class->getConstructor();
$isDone = false;
$isConstructorCalled = false;
if ($constructor && $constructor->isFinal()) {
$mock = false;
if ($this->isConstructorBypassSupportedForExtendedInternals) {
$mock = $class->newInstanceWithoutConstructor();
$isDone = true;
// @codeCoverageIgnoreStart
} elseif ($this->isConstructorBypassSupported) {
$isInternal = false;
$ancestor = $class->getParentClass();
while (!$isInternal && $ancestor) {
if ($isInternal = $ancestor->isInternal()) {
break;
}
$ancestor = $ancestor->getParentClass();
}
if (!$isInternal) {
$mock = $class->newInstanceWithoutConstructor();
$isDone = true;
}
}
if (!$isDone) {
$className = $class->getName();
$serialized = sprintf('O:%d:"%s":0:{}', strlen($className), $className);
try {
$mock = @unserialize($serialized);
$isDone = $mock instanceof $className;
} catch (Throwable $error) {
// re-thrown after cleanup
} catch (Exception $error) {
// re-thrown after cleanup
}
}
if (!$isDone) {
if (null === $arguments) {
throw new RuntimeException(sprintf('Unable to bypass final constructor for %s.', $class->getName()));
}
$mock = $class->newInstanceArgs($arguments);
$isDone = true;
$isConstructorCalled = true;
}
}
// @codeCoverageIgnoreEnd
if (!$isDone) {
$mock = $class->newInstance();
}
$handle = $this->handleFactory->instanceHandle($mock, strval($this->labelSequencer->next()));
$handle->partial();
if (!$isConstructorCalled && null !== $arguments) {
$handle->constructWith($arguments);
}
return $mock;
}