/** * @param $type * @param $value * @return mixed */ private function mapField($type, $value) { $cacheKey = $this->getCacheKey($type, $value); if (!isset($this->mappingFieldsCache[$cacheKey])) { if (Scalar::isScalar($type)) { $this->mappingFieldsCache[$cacheKey] = Scalar::map($value, $type); } else { $class = new \ReflectionClass($type); $result = $class->getMethod('getMapped')->invoke(null, $value); if (is_object($result)) { $object = new \ReflectionObject($result); if ($object->isCloneable()) { $result = clone $result; } } $this->mappingFieldsCache[$cacheKey] = $result; } } return $this->mappingFieldsCache[$cacheKey]; }
private function __clone() { } } $bar = new bar(); print "User class - private __clone\n"; $obj = new ReflectionClass($bar); var_dump($obj->isCloneable()); $obj = new ReflectionObject($bar); var_dump($obj->isCloneable()); $h = clone $foo; print "Closure\n"; $closure = function () { }; $obj = new ReflectionClass($closure); var_dump($obj->isCloneable()); $obj = new ReflectionObject($closure); var_dump($obj->isCloneable()); $h = clone $closure; print "Internal class - SimpleXMLElement\n"; $obj = new ReflectionClass('simplexmlelement'); var_dump($obj->isCloneable()); $obj = new ReflectionObject(new simplexmlelement('<test></test>')); var_dump($obj->isCloneable()); $h = clone new simplexmlelement('<test></test>'); print "Internal class - XMLWriter\n"; $obj = new ReflectionClass('xmlwriter'); var_dump($obj->isCloneable()); $obj = new ReflectionObject(new XMLWriter()); var_dump($obj->isCloneable()); $h = clone new xmlwriter();
/** * @param $param * @param \ReflectionObject $reflection */ private function buildFromObject($param, $reflection = null) { foreach ($param as $key => $value) { $this->object['Object default'][$key] = $value; } // Get info on the object $this->object['Reflection']['In namespace'] = $reflection->inNamespace() ? 'Yes' : 'No'; if ($reflection->inNamespace()) { $this->object['Class namespace'] = $reflection->getNamespaceName(); } $this->object['Reflection']['Class name'] = $reflection->getName(); $this->object['Reflection']['Is internal'] = $reflection->isInternal() ? 'Yes' : 'No'; $this->object['Reflection']['Is iterable'] = $reflection->isIterateable() ? 'Yes' : 'No'; $this->object['Reflection']['Is abstract'] = $reflection->isAbstract() ? 'Yes' : 'No'; $this->object['Reflection']['Is final'] = $reflection->isFinal() ? 'Yes' : 'No'; $this->object['Reflection']['Is user defined'] = $reflection->isUserDefined() ? 'Yes' : 'No'; $this->object['Reflection']['Is instantiable'] = $reflection->isInstantiable() ? 'Yes' : 'No'; $this->object['Reflection']['Is clonable'] = $reflection->isCloneable() ? 'Yes' : 'No'; $this->object['Reflection']['Is interface'] = $reflection->isInterface() ? 'Yes' : 'No'; $this->object['Reflection']['Class constants'] = !empty($reflection->getConstants()) ? $reflection->getConstants() : 'Class has no constants'; $this->object['Reflection']['Class static properties'] = !empty($reflection->getStaticProperties()) ? $reflection->getStaticProperties() : 'Class has no static properties'; $this->object['Reflection']['Class default properties'] = !empty($reflection->getDefaultProperties()) ? $reflection->getDefaultProperties() : 'Class has no default properties'; if (null === $reflection->getConstructor()) { $this->object['Reflection']['Class construct'] = 'Class has no construct.'; } else { $this->object['Reflection']['Class construct'] = $reflection->getConstructor(); } $this->object['Reflection']['Class interfaces'] = !empty($reflection->getInterfaces()) ? $reflection->getInterfaces() : 'Class implements no interfaces'; $this->object['Reflection']['Class traits'] = !empty($reflection->getTraits()) ? $reflection->getTraits() : 'Class has no traits'; $this->object['Reflection']['Class parent'] = $reflection->getParentClass() !== false ? $reflection->getParentClass() : 'Class has no parent'; if (false === $reflection->getFileName()) { $this->object['Reflection']['Defined in'] = 'Class is internal, no definition to provide.'; } else { $this->object['Reflection']['Defined in'] = $reflection->getFileName(); } if (false === $reflection->getFileName()) { $this->object['Reflection']['Start line'] = 'Class is internal, no start line to provide.'; } else { $this->object['Reflection']['Start line'] = $reflection->getFileName(); } if (false === $reflection->getEndLine()) { $this->object['Reflection']['End line'] = 'Class is internal, no end line to provide.'; } else { $this->object['Reflection']['End line'] = $reflection->getEndLine(); } if (false === $reflection->getDocComment()) { $this->object['Reflection']['Doc comments'] = 'No documents to provide.'; } else { $this->object['Reflection']['Doc comments'] = $reflection->getDocComment(); } // End get info $this->html .= "<span class=\"js-parent-object\">"; if (!empty($this->object['Object default'])) { $this->html .= "<div class=\"js-object-default-tab \"><button class=\"button-reflection button\">Show reflection</button></div>"; $this->html .= "<div class=\"js-object-default \">"; $this->buildFromObjectIterationInformationRecursive($this->object['Object default']); $this->html .= "</div>"; } if ($param instanceof \Closure) { $this->html .= "<div class=\"js-object-default-tab \"><button class=\"button-reflection button\">Show reflection</button></div>"; $this->html .= "<div class=\"js-object-default \">"; $this->html .= "<span class=\"css-type-string\">Nothing here...</span>"; $this->html .= "</div>"; } $this->html .= "<div class=\"js-object-reflection-tab hide\"><button class=\"button-class-default button\">Show default</button></div>"; $this->html .= "<div class=\"js-object-reflection hide\">"; $this->buildFromObjectReflectionInformationRecursive($this->object['Reflection']); $this->html .= "</div>"; $this->html .= "</span>"; $this->object = []; }
/** * Copy an object * @param object $object * @return object */ private function copyObject($object) { $objectHash = spl_object_hash($object); if (isset($this->hashMap[$objectHash])) { return $this->hashMap[$objectHash]; } $reflectedObject = new \ReflectionObject($object); if (!$reflectedObject->isCloneable() and $this->skipUncloneable) { $this->hashMap[$objectHash] = $object; return $object; } $newObject = clone $object; $this->hashMap[$objectHash] = $newObject; foreach ($reflectedObject->getProperties() as $property) { $this->copyObjectProperty($newObject, $property); } return $newObject; }
function objectToPromise($obj) { $r = new ReflectionObject($obj); if ($r->isCloneable()) { $result = clone $obj; $values = array(); foreach ($result as $key => $value) { $values[] = toPromise($value)->then(function ($v) use($result, $key) { $result->{$key} = $v; }); } return all($values)->then(function () use($result) { return $result; }); } return $obj; }
/** * @param PHPUnit_Framework_MockObject_MockObject $testArgument * * @return bool */ private function isCloneable(PHPUnit_Framework_MockObject_MockObject $testArgument) { $reflector = new ReflectionObject($testArgument); if (!$reflector->isCloneable()) { return false; } if ($reflector->hasMethod('__clone') && $reflector->getMethod('__clone')->isPublic()) { return true; } return false; }
/** * Copy an object * @param object $object * @return object */ private function copyObject($object) { $objectHash = spl_object_hash($object); if (isset($this->hashMap[$objectHash])) { return $this->hashMap[$objectHash]; } $reflectedObject = new \ReflectionObject($object); if (false === ($isCloneable = $reflectedObject->isCloneable()) and $this->skipUncloneable) { $this->hashMap[$objectHash] = $object; return $object; } if (false === $isCloneable) { throw new CloneException(sprintf('Class "%s" is not cloneable.', $reflectedObject->getName())); } $newObject = clone $object; $this->hashMap[$objectHash] = $newObject; foreach (ReflectionHelper::getProperties($reflectedObject) as $property) { $this->copyObjectProperty($newObject, $property); } return $newObject; }