[PropertyAccessor] A little refactor
- Remove obsolete code - Remove excessive actions - Fix phpdoc & comments
This commit is contained in:
parent
ab188a727d
commit
457b3f0047
@ -62,6 +62,9 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
/**
|
/**
|
||||||
* Should not be used by application code. Use
|
* Should not be used by application code. Use
|
||||||
* {@link PropertyAccess::createPropertyAccessor()} instead.
|
* {@link PropertyAccess::createPropertyAccessor()} instead.
|
||||||
|
*
|
||||||
|
* @param bool $magicCall
|
||||||
|
* @param bool $throwExceptionOnInvalidIndex
|
||||||
*/
|
*/
|
||||||
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false)
|
public function __construct($magicCall = false, $throwExceptionOnInvalidIndex = false)
|
||||||
{
|
{
|
||||||
@ -365,7 +368,7 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
}
|
}
|
||||||
} elseif (!$access[self::ACCESS_HAS_PROPERTY] && property_exists($object, $property)) {
|
} elseif (!$access[self::ACCESS_HAS_PROPERTY] && property_exists($object, $property)) {
|
||||||
// Needed to support \stdClass instances. We need to explicitly
|
// Needed to support \stdClass instances. We need to explicitly
|
||||||
// exclude $classHasProperty, otherwise if in the previous clause
|
// exclude $access[self::ACCESS_HAS_PROPERTY], otherwise if
|
||||||
// a *protected* property was found on the class, property_exists()
|
// a *protected* property was found on the class, property_exists()
|
||||||
// returns true, consequently the following line will result in a
|
// returns true, consequently the following line will result in a
|
||||||
// fatal error.
|
// fatal error.
|
||||||
@ -411,7 +414,6 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
$getsetter = lcfirst($camelProp); // jQuery style, e.g. read: last(), write: last($item)
|
$getsetter = lcfirst($camelProp); // jQuery style, e.g. read: last(), write: last($item)
|
||||||
$isser = 'is'.$camelProp;
|
$isser = 'is'.$camelProp;
|
||||||
$hasser = 'has'.$camelProp;
|
$hasser = 'has'.$camelProp;
|
||||||
$classHasProperty = $reflClass->hasProperty($property);
|
|
||||||
|
|
||||||
if ($reflClass->hasMethod($getter) && $reflClass->getMethod($getter)->isPublic()) {
|
if ($reflClass->hasMethod($getter) && $reflClass->getMethod($getter)->isPublic()) {
|
||||||
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
@ -429,13 +431,10 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
$access[self::ACCESS_NAME] = $property;
|
$access[self::ACCESS_NAME] = $property;
|
||||||
$access[self::ACCESS_REF] = false;
|
$access[self::ACCESS_REF] = false;
|
||||||
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
|
} elseif ($access[self::ACCESS_HAS_PROPERTY] && $reflClass->getProperty($property)->isPublic()) {
|
||||||
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
$access[self::ACCESS_NAME] = $property;
|
$access[self::ACCESS_NAME] = $property;
|
||||||
$access[self::ACCESS_REF] = true;
|
$access[self::ACCESS_REF] = true;
|
||||||
|
|
||||||
$result[self::VALUE] = &$object->$property;
|
|
||||||
$result[self::IS_REF] = true;
|
|
||||||
} elseif ($this->magicCall && $reflClass->hasMethod('__call') && $reflClass->getMethod('__call')->isPublic()) {
|
} elseif ($this->magicCall && $reflClass->hasMethod('__call') && $reflClass->getMethod('__call')->isPublic()) {
|
||||||
// we call the getter and hope the __call do the job
|
// we call the getter and hope the __call do the job
|
||||||
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_MAGIC;
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_MAGIC;
|
||||||
@ -506,7 +505,7 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
$this->writeCollection($object, $property, $value, $access[self::ACCESS_ADDER], $access[self::ACCESS_REMOVER]);
|
$this->writeCollection($object, $property, $value, $access[self::ACCESS_ADDER], $access[self::ACCESS_REMOVER]);
|
||||||
} elseif (!$access[self::ACCESS_HAS_PROPERTY] && property_exists($object, $property)) {
|
} elseif (!$access[self::ACCESS_HAS_PROPERTY] && property_exists($object, $property)) {
|
||||||
// Needed to support \stdClass instances. We need to explicitly
|
// Needed to support \stdClass instances. We need to explicitly
|
||||||
// exclude $classHasProperty, otherwise if in the previous clause
|
// exclude $access[self::ACCESS_HAS_PROPERTY], otherwise if
|
||||||
// a *protected* property was found on the class, property_exists()
|
// a *protected* property was found on the class, property_exists()
|
||||||
// returns true, consequently the following line will result in a
|
// returns true, consequently the following line will result in a
|
||||||
// fatal error.
|
// fatal error.
|
||||||
@ -579,7 +578,6 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
private function getWriteAccessInfo($object, $property, $value)
|
private function getWriteAccessInfo($object, $property, $value)
|
||||||
{
|
{
|
||||||
$key = get_class($object).'::'.$property;
|
$key = get_class($object).'::'.$property;
|
||||||
$guessedAdders = '';
|
|
||||||
|
|
||||||
if (isset($this->writePropertyCache[$key])) {
|
if (isset($this->writePropertyCache[$key])) {
|
||||||
$access = $this->writePropertyCache[$key];
|
$access = $this->writePropertyCache[$key];
|
||||||
@ -594,13 +592,7 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
if (is_array($value) || $value instanceof \Traversable) {
|
if (is_array($value) || $value instanceof \Traversable) {
|
||||||
$methods = $this->findAdderAndRemover($reflClass, $singulars);
|
$methods = $this->findAdderAndRemover($reflClass, $singulars);
|
||||||
|
|
||||||
if (null === $methods) {
|
if (null !== $methods) {
|
||||||
// It is sufficient to include only the adders in the error
|
|
||||||
// message. If the user implements the adder but not the remover,
|
|
||||||
// an exception will be thrown in findAdderAndRemover() that
|
|
||||||
// the remover has to be implemented as well.
|
|
||||||
$guessedAdders = '"add'.implode('()", "add', $singulars).'()", ';
|
|
||||||
} else {
|
|
||||||
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_ADDER_AND_REMOVER;
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_ADDER_AND_REMOVER;
|
||||||
$access[self::ACCESS_ADDER] = $methods[0];
|
$access[self::ACCESS_ADDER] = $methods[0];
|
||||||
$access[self::ACCESS_REMOVER] = $methods[1];
|
$access[self::ACCESS_REMOVER] = $methods[1];
|
||||||
@ -608,11 +600,9 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($access[self::ACCESS_TYPE])) {
|
if (!isset($access[self::ACCESS_TYPE])) {
|
||||||
$setter = 'set'.$this->camelize($property);
|
$setter = 'set'.$camelized;
|
||||||
$getsetter = lcfirst($camelized); // jQuery style, e.g. read: last(), write: last($item)
|
$getsetter = lcfirst($camelized); // jQuery style, e.g. read: last(), write: last($item)
|
||||||
|
|
||||||
$classHasProperty = $reflClass->hasProperty($property);
|
|
||||||
|
|
||||||
if ($this->isMethodAccessible($reflClass, $setter, 1)) {
|
if ($this->isMethodAccessible($reflClass, $setter, 1)) {
|
||||||
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_METHOD;
|
||||||
$access[self::ACCESS_NAME] = $setter;
|
$access[self::ACCESS_NAME] = $setter;
|
||||||
@ -622,7 +612,7 @@ class PropertyAccessor implements PropertyAccessorInterface
|
|||||||
} elseif ($this->isMethodAccessible($reflClass, '__set', 2)) {
|
} elseif ($this->isMethodAccessible($reflClass, '__set', 2)) {
|
||||||
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
$access[self::ACCESS_NAME] = $property;
|
$access[self::ACCESS_NAME] = $property;
|
||||||
} elseif ($classHasProperty && $reflClass->getProperty($property)->isPublic()) {
|
} elseif ($access[self::ACCESS_HAS_PROPERTY] && $reflClass->getProperty($property)->isPublic()) {
|
||||||
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
$access[self::ACCESS_TYPE] = self::ACCESS_TYPE_PROPERTY;
|
||||||
$access[self::ACCESS_NAME] = $property;
|
$access[self::ACCESS_NAME] = $property;
|
||||||
} elseif ($this->magicCall && $this->isMethodAccessible($reflClass, '__call', 2)) {
|
} elseif ($this->magicCall && $this->isMethodAccessible($reflClass, '__call', 2)) {
|
||||||
|
Reference in New Issue
Block a user