/** * Retrieve all public properties and their values * Although this duplicates get_object_vars(), it * is mostly useful for internal calls when we need * to filter out the non-public properties. * * @param bool $publicOnly * * @return array */ public function getProperties($publicOnly = true) { $reflection = new \ReflectionObject($this); $properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC); if (!$publicOnly) { $properties = array_merge($properties, $reflection->getProperties(\ReflectionProperty::IS_PROTECTED)); } $data = array(); foreach ($properties as $property) { $name = $property->name; $data[$name] = $this->{$name}; } return $data; }
public function checkForTravelerIdEdit(TravelerId $liveTravelerId, TravelerId $travelerId) { $edit = null; $oldAttributes = []; $newAttributes = []; $reflectedObject = new \ReflectionObject($travelerId); $reflectedProperties = $reflectedObject->getProperties(); foreach ($reflectedProperties as $reflectedProperty) { $propertyName = $reflectedProperty->getName(); if ($propertyName !== 'bin') { $reflectedProperty->setAccessible(true); $liveValue = $reflectedProperty->getValue($liveTravelerId); $value = $reflectedProperty->getValue($travelerId); if ($liveValue !== $value) { $oldAttributes[$propertyName] = (is_object($liveValue) and method_exists($liveValue, 'getId')) ? $liveValue->getId() : $liveValue; $newAttributes[$propertyName] = (is_object($value) and method_exists($value, 'getId')) ? $value->getId() : $value; } } } if (count($oldAttributes) > 0) { $edit = new InventoryTravelerIdEdit(); $edit->setTravelerId($liveTravelerId); $edit->setByUser($this->getUser()); $edit->setEditedAt(new \DateTime()); $edit->setOldAttributes($oldAttributes); $edit->setNewAttributes($newAttributes); $this->getDoctrine()->getManager()->persist($edit); } return $edit; }
public function isEmpty() { //testing attributes, the easier place to find something foreach ($this->attributes as $attr) { if (!empty($attr)) { return false; } } //if didn't returned, let's try custom properties $obj = new ReflectionObject($this); foreach ($obj->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { if ($property->getDeclaringClass()->name == $obj->name) { $value = $property->getValue($this); if (!empty($value)) { return false; } } } //nothing? so, trying getters foreach ($obj->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { if ($method->getDeclaringClass()->name == $obj->name && strpos($method->name, 'get') === 0 && $method->getNumberOfRequiredParameters() == 0) { $value = $method->invoke($this); if (!empty($value)) { return false; } } } return true; }
public function checkForSalesItemEdit(SalesItem $salesItem, SalesItemDataTransferObject $dto) { $edit = null; $oldAttributes = []; $newAttributes = []; $reflectedObject = new \ReflectionObject($salesItem); $reflectedProperties = $reflectedObject->getProperties(); foreach ($reflectedProperties as $reflectedProperty) { $propertyName = $reflectedProperty->getName(); if ($propertyName !== 'bin' and property_exists($dto, $propertyName)) { $reflectedProperty->setAccessible(true); $liveValue = $reflectedProperty->getValue($salesItem); $value = $dto->{$propertyName}; if ($liveValue !== $value) { $oldAttributes[$propertyName] = (is_object($liveValue) and method_exists($liveValue, 'getId')) ? $liveValue->getId() : $liveValue; $newAttributes[$propertyName] = (is_object($value) and method_exists($value, 'getId')) ? $value->getId() : $value; } } } if (count($oldAttributes) > 0) { $edit = new InventorySalesItemEdit(); $edit->setSalesItem($salesItem); $edit->setByUser($this->getUser()); $edit->setEditedAt(new \DateTime()); $edit->setOldAttributes($oldAttributes); $edit->setNewAttributes($newAttributes); $this->getDoctrine()->getManager()->persist($edit); } return $edit; }
private function pickle($o, $maxLevel = FIRELOGGER_MAX_PICKLE_DEPTH) { if ($maxLevel == 0) { // see http://us3.php.net/manual/en/language.types.string.php#73524 if (!is_object($o) || method_exists($o, '__toString')) { return (string) $o; } return get_class($o); } if (is_object($o)) { $data = array(); $r = new ReflectionObject($o); $props = $r->getProperties(); foreach ($props as $prop) { $name = $prop->getName(); $prop->setAccessible(true); // http://schlitt.info/opensource/blog/0581_reflecting_private_properties.html $val = $prop->getValue($o); $data[$name] = $this->pickle($val, $maxLevel - 1); } return $data; } if (is_array($o)) { $data = array(); foreach ($o as $k => $v) { $data[$k] = $this->pickle($v, $maxLevel - 1); } return $data; } // TODO: investigate other complex cases return $o; }
protected function tearDown() { //Close & unsets if (is_object($this->em)) { $this->em->getConnection()->close(); $this->em->close(); } unset($this->em); unset($this->container); unset($this->kern); unset($this->client); //Nettoyage des mocks //http://kriswallsmith.net/post/18029585104/faster-phpunit $refl = new \ReflectionObject($this); foreach ($refl->getProperties() as $prop) { if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) { $prop->setAccessible(true); $prop->setValue($this, null); } } //Nettoyage du garbage if (!gc_enabled()) { gc_enable(); } gc_collect_cycles(); //Parent parent::tearDown(); }
/** * {@inheritdoc} */ public function normalize($object, $format = null, array $context = array()) { $reflectionObject = new \ReflectionObject($object); $attributes = array(); foreach ($reflectionObject->getProperties() as $property) { if (in_array($property->name, $this->ignoredAttributes)) { continue; } // Override visibility if (!$property->isPublic()) { $property->setAccessible(true); } $attributeValue = $property->getValue($object); if (array_key_exists($property->name, $this->callbacks)) { $attributeValue = call_user_func($this->callbacks[$property->name], $attributeValue); } if (null !== $attributeValue && !is_scalar($attributeValue)) { if (!$this->serializer instanceof NormalizerInterface) { throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $property->name)); } $attributeValue = $this->serializer->normalize($attributeValue, $format); } $attributes[$property->name] = $attributeValue; } return $attributes; }
/** * {@inheritdoc} * * @throws CircularReferenceException */ public function normalize($object, $format = null, array $context = array()) { if ($this->isCircularReference($object, $context)) { return $this->handleCircularReference($object); } $reflectionObject = new \ReflectionObject($object); $attributes = array(); $allowedAttributes = $this->getAllowedAttributes($object, $context, true); foreach ($reflectionObject->getProperties() as $property) { if (in_array($property->name, $this->ignoredAttributes)) { continue; } if (false !== $allowedAttributes && !in_array($property->name, $allowedAttributes)) { continue; } // Override visibility if (!$property->isPublic()) { $property->setAccessible(true); } $attributeValue = $property->getValue($object); if (isset($this->callbacks[$property->name])) { $attributeValue = call_user_func($this->callbacks[$property->name], $attributeValue); } if (null !== $attributeValue && !is_scalar($attributeValue)) { $attributeValue = $this->serializer->normalize($attributeValue, $format, $context); } $propertyName = $property->name; if ($this->nameConverter) { $propertyName = $this->nameConverter->normalize($propertyName); } $attributes[$propertyName] = $attributeValue; } return $attributes; }
public function reflect($object = null) { if ($object == null) { $object = $this; } $reflection = new \ReflectionObject($object); $properties = $reflection->getProperties(); $fields = []; foreach ($properties as $property) { $name = $property->getName(); if ($name == 'bot_name') { continue; } if (!$property->isPrivate()) { $array_of_obj = false; $array_of_array_obj = false; if (is_array($object->{$name})) { $array_of_obj = true; $array_of_array_obj = true; foreach ($object->{$name} as $elm) { if (!is_object($elm)) { //echo $name . " not array of object \n"; $array_of_obj = false; //break; } if (is_array($elm)) { foreach ($elm as $more_net) { if (!is_object($more_net)) { $array_of_array_obj = false; } } } } } if (is_object($object->{$name})) { $fields[$name] = $this->reflect($object->{$name}); } elseif ($array_of_obj) { foreach ($object->{$name} as $elm) { $fields[$name][] = $this->reflect($elm); } } elseif ($array_of_array_obj) { foreach ($object->{$name} as $elm) { $temp = null; foreach ($elm as $obj) { $temp[] = $this->reflect($obj); } $fields[$name][] = $temp; } } else { $property->setAccessible(true); $value = $property->getValue($object); if (is_null($value)) { continue; } $fields[$name] = $value; } } } return $fields; }
/** * Recursively traverses and wraps all Closure objects within the value. * * NOTE: THIS MAY NOT WORK IN ALL USE CASES, SO USE AT YOUR OWN RISK. * * @param mixed $data Any variable that contains closures. * @param SerializerInterface $serializer The serializer to use. */ public static function wrapClosures(&$data, SerializerInterface $serializer) { if ($data instanceof \Closure) { // Handle and wrap closure objects. $reflection = new \ReflectionFunction($data); if ($binding = $reflection->getClosureThis()) { self::wrapClosures($binding, $serializer); $scope = $reflection->getClosureScopeClass(); $scope = $scope ? $scope->getName() : 'static'; $data = $data->bindTo($binding, $scope); } $data = new SerializableClosure($data, $serializer); } elseif (is_array($data) || $data instanceof \stdClass || $data instanceof \Traversable) { // Handle members of traversable values. foreach ($data as &$value) { self::wrapClosures($value, $serializer); } } elseif (is_object($data) && !$data instanceof \Serializable) { // Handle objects that are not already explicitly serializable. $reflection = new \ReflectionObject($data); if (!$reflection->hasMethod('__sleep')) { foreach ($reflection->getProperties() as $property) { if ($property->isPrivate() || $property->isProtected()) { $property->setAccessible(true); } $value = $property->getValue($data); self::wrapClosures($value, $serializer); $property->setValue($data, $value); } } } }
/** * * Hydrates object * * @param object|string $object * @param array $data * @return object * @throws RuntimeException */ public static function hydrate($object, array $data = []) { if (false === is_object($object)) { throw new RuntimeException('First parameter of hydrate must be object.'); } if (false === is_object($object)) { return null; } /** @var \ReflectionObject $reflection reflection object of given model */ $reflection = new \ReflectionObject($object); /** @var \ReflectionProperty[] $properties */ $properties = $reflection->getProperties(); /** * iterate through all fields * and fill array for checking */ foreach ($data as $field => $value) { $fieldName = preg_replace('/[^A-Za-z]/', '', $field); $data[strtolower($fieldName)] = $value; } /** @var ReflectionProperty $property */ foreach ($properties as $property) { if (false === $property->isPublic()) { $property->setAccessible(true); } $name = preg_replace('/[^A-Za-z]/', '', strtolower($property->getName())); if (isset($data[$name])) { $property->setValue($object, $data[$name]); } } return $object; }
/** * @param $object * @param string $format * @return array|string */ public static function serialize($object, $format = "array") { $attributes = array(); if (is_array($object)) { foreach ($object as $obj) { $attributes[] = self::serialize($obj, $format); } } else { $reflectionObject = new \ReflectionObject($object); foreach ($reflectionObject->getProperties() as $property) { if ($property->isStatic()) { continue; } if (!$property->isPublic()) { $property->setAccessible(true); } $attributeValue = $property->getValue($object); $propertyName = $property->name; if (is_object($attributeValue)) { $attributeValue = self::serialize($attributeValue); } $attributes[$propertyName] = $attributeValue; } if ($format == "json") { return json_encode($attributes); } } return $attributes; }
* Marked only as protected to allow extension of the class. To extend, * simply override {@link getInstance()}. * * @var Zend_Controller_Front */ protected static $_instance = null; /** * Array of invocation parameters to use when instantiating action * controllers * @var array */ protected $_invokeParams = array(); /** * Subdirectory within a module containing controllers; defaults to 'controllers' * @var string */ protected $_moduleControllerDirectoryName = 'controllers'; /** * Instance of Zend_Controller_Plugin_Broker * @var Zend_Controller_Plugin_Broker */ protected $_plugins = null; /** * Instance of Zend_Controller_Request_Abstract * @var Zend_Controller_Request_Abstract */ protected $_request = null; /** * Instance of Zend_Controller_Response_Abstract * @var Zend_Controller_Response_Abstract
/** * Resolve the dependencies of the object * * @param mixed $object Object in which to resolve dependencies * @throws Annotations\AnnotationException */ public function resolveDependencies($object) { if (is_null($object)) { return; } // Fetch the object's properties $reflectionClass = new \ReflectionObject($object); $properties = $reflectionClass->getProperties(); // For each property foreach ($properties as $property) { // Look for DI annotations $injectAnnotation = null; $valueAnnotation = null; $propertyAnnotations = $this->getAnnotationReader()->getPropertyAnnotations($property); foreach ($propertyAnnotations as $annotation) { if ($annotation instanceof Inject) { $injectAnnotation = $annotation; } if ($annotation instanceof Value) { $valueAnnotation = $annotation; } } // If both @Inject and @Value annotation, exception if ($injectAnnotation && $valueAnnotation) { throw new AnnotationException(get_class($object) . "::" . $property->getName() . " can't have both @Inject and @Value annotations"); } elseif ($injectAnnotation) { $this->resolveInject($property, $object, $injectAnnotation->lazy); } elseif ($valueAnnotation) { $this->resolveValue($property, $valueAnnotation->key, $object); } } }
public function reflect($object) { $reflection = new \ReflectionObject($object); $properties = $reflection->getProperties(); $fields = []; foreach ($properties as $property) { $name = $property->getName(); if ($name == 'bot_name') { continue; } if (!$property->isPrivate()) { if (is_object($object->{$name})) { $fields[$name] = $this->reflect($object->{$name}); } else { $property->setAccessible(true); $value = $property->getValue($object); if (is_null($value)) { continue; } $fields[$name] = $value; } } } return $fields; }
public static function produceHumanReadableOutputForObject($object, $depth, $isTruncatingRecursion, $level, array $previousSplObjectHashes) { if (false == is_object($object)) { throw new \UnexpectedValueException(sprintf("Expected parameter '%s' to be an Object. Found: %s", '$object', gettype($object))); } $reflection = new \ReflectionObject($object); $properties = $reflection->getProperties(); $objectValuesString = ""; foreach ($properties as $property) { $property->setAccessible(true); $propertyValue = $property->getValue($object); $replacementValue = self::prepareRecursively($propertyValue, $depth - 1, $isTruncatingRecursion, $level + 1, $previousSplObjectHashes); $exposure = ""; if ($property->isPrivate()) { $exposure = "private"; } elseif ($property->isProtected()) { $exposure = "protected"; } else { $exposure = "public"; } if ($property->isStatic()) { $exposure .= " static"; } $objectValuesString .= str_repeat(self::INDENTATION_CHARACTERS, $level + 1) . str_replace(["%PROPERTY_EXPOSURE%", "%PROPERTY_NAME%", "%PROPERTY_VALUE%"], [$exposure, $property->getName(), trim($replacementValue)], "%PROPERTY_EXPOSURE% \$%PROPERTY_NAME% = %PROPERTY_VALUE%"); if (is_scalar($propertyValue) || is_null($propertyValue)) { $objectValuesString .= ";"; } $objectValuesString .= PHP_EOL; } $hash = spl_object_hash($object); return str_repeat(self::INDENTATION_CHARACTERS, $level) . get_class($object) . " Object &{$hash}" . PHP_EOL . str_repeat(self::INDENTATION_CHARACTERS, $level) . "{" . PHP_EOL . $objectValuesString . str_repeat(self::INDENTATION_CHARACTERS, $level) . "}" . PHP_EOL; }
/** * Unset all additional properties of test classes to help PHP * garbage collection. This reduces memory footprint with lots * of tests. * * If owerwriting tearDown() in test classes, please call * parent::tearDown() at the end. Unsetting of own properties * is not needed this way. * * @throws \RuntimeException * @return void */ protected function tearDown() { // Unset properties of test classes to safe memory $reflection = new \ReflectionObject($this); foreach ($reflection->getProperties() as $property) { $declaringClass = $property->getDeclaringClass()->getName(); if (!$property->isStatic() && $declaringClass !== \TYPO3\CMS\Core\Tests\UnitTestCase::class && $declaringClass !== \TYPO3\CMS\Core\Tests\BaseTestCase::class && strpos($property->getDeclaringClass()->getName(), 'PHPUnit_') !== 0) { $propertyName = $property->getName(); unset($this->{$propertyName}); } } unset($reflection); // Delete registered test files and directories foreach ($this->testFilesToDelete as $absoluteFileName) { $absoluteFileName = GeneralUtility::fixWindowsFilePath(PathUtility::getCanonicalPath($absoluteFileName)); if (!GeneralUtility::validPathStr($absoluteFileName)) { throw new \RuntimeException('tearDown() cleanup: Filename contains illegal characters', 1410633087); } if (!StringUtility::beginsWith($absoluteFileName, PATH_site . 'typo3temp/')) { throw new \RuntimeException('tearDown() cleanup: Files to delete must be within typo3temp/', 1410633412); } // file_exists returns false for links pointing to not existing targets, so handle links before next check. if (@is_link($absoluteFileName) || @is_file($absoluteFileName)) { unlink($absoluteFileName); } elseif (@is_dir($absoluteFileName)) { GeneralUtility::rmdir($absoluteFileName, true); } else { throw new \RuntimeException('tearDown() cleanup: File, link or directory does not exist', 1410633510); } } $this->testFilesToDelete = array(); }
public function preFlush(PreFlushEventArgs $eventArgs) { $uow = $eventArgs->getEntityManager()->getUnitOfWork(); $insertions = $uow->getScheduledEntityInsertions(); $updates = $uow->getScheduledEntityUpdates(); $entities = array_merge($insertions, $updates); foreach ($entities as $entity) { $refl = new \ReflectionObject($entity); foreach ($refl->getProperties() as $prop) { /** @var TsVector $annot */ $annot = $this->reader->getPropertyAnnotation($prop, TsVector::class); if (is_null($annot)) { continue; } $fields = $annot->fields; $tsVectorVal = []; foreach ($fields as $field) { $field = $refl->getProperty($field); $field->setAccessible(true); $fieldValue = $field->getValue($entity); if (is_array($fieldValue)) { $fieldValue = implode(' ', $fieldValue); } $tsVectorVal[] = $fieldValue; } $prop->setAccessible(true); $value = ['data' => join(' ', $tsVectorVal), 'language' => $annot->language, 'weight' => $annot->weight]; $prop->setValue($entity, $value); } } }
public function load($filename) { if (!is_readable($filename)) { throw new \yii\base\InvalidValueException('cannot read config file at this location: ' . $filename); } $this->config = Json::decode(file_get_contents($filename), false); \Yii::beginProfile('reflection', 'config'); // // silently injects newly introduced option into current config from default config // $def_config = $this->getDefaultCfg(); $rf1 = new \ReflectionObject($def_config); /* @var $p_base \ReflectionProperty */ foreach ($rf1->getProperties() as $p_base) { // lvl-1: system, book ... $lvl1 = $p_base->name; if (empty($this->config->{$lvl1})) { $this->config->{$lvl1} = $def_config->{$lvl1}; continue; } $rf2 = new \ReflectionObject($def_config->{$p_base->name}); foreach ($rf2->getProperties() as $p_option) { //lvl-2: system->theme .. $lvl2 = $p_option->name; if (empty($this->config->{$lvl1}->{$lvl2})) { $this->config->{$lvl1}->{$lvl2} = $def_config->{$lvl1}->{$lvl2}; continue; //reserved. required for lvl-3 if introduced } } } \Yii::endProfile('reflection', 'config'); }
/** * Export all public, private and protected properties of $var. * * @param mixed $var Variable to export. * @param int $depth Maximum depth to export, or -1 for infinite. **danger, will robison** (default: -1) * * @return mixed */ public static function export($var, $depth = 5) { if ($depth == 0) { if (is_object($var)) { return sprintf('<%s #%s>', get_class($var), spl_object_hash($var)); } elseif (is_array($var)) { return sprintf('Array(%d)', count($var)); } else { return $var; } } elseif (is_array($var)) { $return = array(); foreach ($var as $k => $v) { $return[$k] = self::export($v, $depth - 1); } return $return; } elseif (is_object($var)) { $return = new \StdClass(); $class = new \ReflectionObject($var); $return->__CLASS__ = get_class($var); foreach ($class->getProperties() as $prop) { $name = $prop->getName(); $prop->setAccessible(true); $return->{$name} = self::export($prop->getValue($var), $depth - 1); } return $return; } else { return $var; } }
/** * map public properties of value object to db columns * @return array dbFields */ public function map() { $rf = new \ReflectionObject($this->valObj); foreach ($rf->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) { $this->dbFields[$prop->name] = $this->valObj->{$prop->name}; } return $this->dbFields; }
public function foo() { $reflection = new ReflectionObject($this); foreach ($reflection->getProperties() as $property) { $property->getName(); $property->getName(); } }
/** * recursive print variables and limit by level. * * @param mixed $data The variable you want to dump. * @param int $level The level number to limit recursive loop. * * @since 2.0 * * @return string Dumped data. */ function printRLevel($data, $level = 5) { static $innerLevel = 1; static $tabLevel = 1; $self = __FUNCTION__; $type = gettype($data); $tabs = str_repeat(' ', $tabLevel); $quoteTabes = str_repeat(' ', $tabLevel - 1); $output = ''; $elements = array(); $recursiveType = array('object', 'array'); // Recursive if (in_array($type, $recursiveType)) { // If type is object, try to get properties by Reflection. if ($type == 'object') { $output = get_class($data) . ' ' . ucfirst($type); $ref = new \ReflectionObject($data); $properties = $ref->getProperties(); foreach ($properties as $property) { $property->setAccessible(true); $pType = $property->getName(); if ($property->isProtected()) { $pType .= ":protected"; } elseif ($property->isPrivate()) { $pType .= ":" . $property->class . ":private"; } if ($property->isStatic()) { $pType .= ":static"; } $elements[$pType] = $property->getValue($data); } } elseif ($type == 'array') { $output = ucfirst($type); $elements = $data; } // Start dumping data if ($level == 0 || $innerLevel < $level) { // Start recursive print $output .= "\n{$quoteTabes}("; foreach ($elements as $key => $element) { $output .= "\n{$tabs}[{$key}] => "; // Increment level $tabLevel = $tabLevel + 2; $innerLevel++; $output .= in_array(gettype($element), $recursiveType) ? $self($element, $level) : $element; // Decrement level $tabLevel = $tabLevel - 2; $innerLevel--; } $output .= "\n{$quoteTabes})\n"; } else { $output .= "\n{$quoteTabes}*MAX LEVEL*\n"; } } else { $output = $data; } return $output; }
/** * Implements the __toArray() pseudo-magic method by reflecting the object * to extract the list of public properties and return their values as an * associative array. * * @history * 2013.09.30: * (AT) Initial release * * @version 2013.09.30 * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> * * @return array Associative array with public properties and their values */ public function __toArray() { $array = array(); $reflection = new \ReflectionObject($this); foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { $array[$property->name] = $this->{$property->name}; } return $array; }
/** * Moves all public properties to the protected __property array to force * all set and get operations to pass through __get() and __set(). * * @history * 2013.09.30: * (AT) Initial release * * @version 2013.09.30 * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**> */ public function __construct() { $reflection = new \ReflectionObject($this); foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { $this->__values[$property->name] = $this->{$property->name}; $this->__properties[] = $property->name; unset($this->{$property->name}); } }
static function copyPropertiesObj($obj, $target) { $rc = new ReflectionObject($obj); foreach ($rc->getProperties() as $prop) { if (property_exists($target, $prop->name)) { $target->{$prop->name} = $prop->getValue($obj); } } }
/** * Flushes in-memory value object content. * * @since 5.1 * @return void */ public function flush() { $obj = new \ReflectionObject($this); $properties = $obj->getProperties(); foreach ($properties as $property) { $name = $property->getName(); $this->{$name} = false; } }
public function __call($method, $args) { $reflection = new ReflectionObject($this); foreach ($reflection->getProperties() as $property) { $transformedPropertyName = str_replace('_', '', $property->getName()); $property->getName(); current($args); } }
/** * fixing memory usage */ protected function fixMemoryUsage() { $refl = new \ReflectionObject($this); foreach ($refl->getProperties() as $prop) { if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) { $prop->setAccessible(true); $prop->setValue($this, null); } } }
/** * @param string $name Name of method. * @param array $arguments Method arguments. * * @return mixed * * @throws MultipleCallersException */ public function __call($name, array $arguments) { $underscored = S::create($name)->underscored(); $callers = []; $callersNames = []; $reflection = new \ReflectionObject($this); foreach ($reflection->getProperties(\ReflectionProperty::IS_PROTECTED) as $property) { if (is_a($this->{$property->name}, CallerInterface::class) && $underscored->indexOf($this->{$property->name}->callPrefix() . '_') === 0) { if (!in_array($this->{$property->name}, $callers, true)) { $callers[] = $this->{$property->name}; $callersNames[] = $property->name; } } } $methodName = get_class($this) . '::' . $name . '()'; $errorMessage = 'Call to undefined method ' . $methodName . '.'; if (empty($callers)) { trigger_error($errorMessage, E_USER_ERROR); } if (($count = count($callers)) !== 1) { throw new MultipleCallersException("Found {$count} callers (" . implode(', ', $callersNames) . ") for method {$methodName}."); } try { $exception = null; $callers[0]->call($this, $name, $arguments); } catch (CallerException $e) { $exception = $e; } if (is_null($exception)) { trigger_error($errorMessage, E_USER_ERROR); } return $exception->getReturn(); }