Fix too strict type definitions

This commit is contained in:
Dennis Eichhorn 2018-07-14 18:23:05 +02:00
parent e76606d0a3
commit c8d938591a
2 changed files with 74 additions and 45 deletions

View File

@ -724,22 +724,26 @@ class DataMapperAbstract implements DataMapperInterface
* The reference is stored in the main model * The reference is stored in the main model
* *
* @param string $propertyName Property name to initialize * @param string $propertyName Property name to initialize
* @param object $obj Object to create * @param mixed $obj Object to create
* *
* @return mixed * @return mixed
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private static function createOwnsOne(string $propertyName, object $obj) private static function createOwnsOne(string $propertyName, $obj)
{ {
$mapper = static::$ownsOne[$propertyName]['mapper']; if (is_object($obj)) {
$primaryKey = $mapper::getObjectId($obj); $mapper = static::$ownsOne[$propertyName]['mapper'];
$primaryKey = $mapper::getObjectId($obj);
if (empty($primaryKey)) { if (empty($primaryKey)) {
return $mapper::create($obj); return $mapper::create($obj);
}
return $primaryKey;
} }
return $primaryKey; return $obj;
} }
/** /**
@ -776,23 +780,27 @@ class DataMapperAbstract implements DataMapperInterface
* The reference is stored in the main model * The reference is stored in the main model
* *
* @param string $propertyName Property name to initialize * @param string $propertyName Property name to initialize
* @param object $obj Object to create * @param mixed $obj Object to create
* *
* @return mixed * @return mixed
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private static function createBelongsTo(string $propertyName, object $obj) private static function createBelongsTo(string $propertyName, $obj)
{ {
/** @var string $mapper */ if (is_object($obj)) {
$mapper = static::$belongsTo[$propertyName]['mapper']; /** @var string $mapper */
$primaryKey = $mapper::getObjectId($obj); $mapper = static::$belongsTo[$propertyName]['mapper'];
$primaryKey = $mapper::getObjectId($obj);
if (empty($primaryKey)) { if (empty($primaryKey)) {
return $mapper::create($obj); return $mapper::create($obj);
}
return $primaryKey;
} }
return $primaryKey; return $obj;
} }
/** /**
@ -1069,18 +1077,22 @@ class DataMapperAbstract implements DataMapperInterface
* The reference is stored in the main model * The reference is stored in the main model
* *
* @param string $propertyName Property name to initialize * @param string $propertyName Property name to initialize
* @param object $obj Object to update * @param mixed $obj Object to update
* *
* @return mixed * @return mixed
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private static function updateBelongsTo(string $propertyName, object $obj) private static function updateBelongsTo(string $propertyName, $obj)
{ {
/** @var string $mapper */ if (is_object($obj)) {
$mapper = static::$belongsTo[$propertyName]['mapper']; /** @var string $mapper */
$mapper = static::$belongsTo[$propertyName]['mapper'];
return $mapper::update($obj); return $mapper::update($obj);
}
return $obj;
} }
/** /**
@ -1269,19 +1281,23 @@ class DataMapperAbstract implements DataMapperInterface
* The reference is stored in the main model * The reference is stored in the main model
* *
* @param string $propertyName Property name to initialize * @param string $propertyName Property name to initialize
* @param object $obj Object to delete * @param mixed $obj Object to delete
* *
* @return mixed * @return mixed
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private static function deleteOwnsOne(string $propertyName, object $obj) private static function deleteOwnsOne(string $propertyName, $obj)
{ {
/** @var string $mapper */ if (is_object($obj)) {
$mapper = static::$ownsOne[$propertyName]['mapper']; /** @var string $mapper */
$mapper = static::$ownsOne[$propertyName]['mapper'];
// todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one? // todo: delete owned one object is not recommended since it can be owned by by something else? or does owns one mean that nothing else can have a relation to this one?
return $mapper::delete($obj); return $mapper::delete($obj);
}
return $obj;
} }
/** /**
@ -1290,18 +1306,22 @@ class DataMapperAbstract implements DataMapperInterface
* The reference is stored in the main model * The reference is stored in the main model
* *
* @param string $propertyName Property name to initialize * @param string $propertyName Property name to initialize
* @param object $obj Object to delete * @param mixed $obj Object to delete
* *
* @return mixed * @return mixed
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private static function deleteBelongsTo(string $propertyName, object $obj) private static function deleteBelongsTo(string $propertyName, $obj)
{ {
/** @var string $mapper */ if (is_object($obj)) {
$mapper = static::$belongsTo[$propertyName]['mapper']; /** @var string $mapper */
$mapper = static::$belongsTo[$propertyName]['mapper'];
return $mapper::delete($obj); return $mapper::delete($obj);
}
return $obj;
} }
/** /**
@ -2694,14 +2714,14 @@ class DataMapperAbstract implements DataMapperInterface
/** /**
* Test if object is null object * Test if object is null object
* *
* @param object $obj Object to check * @param mixed $obj Object to check
* *
* @return bool * @return bool
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private static function isNullObject(object $obj) : bool private static function isNullObject($obj) : bool
{ {
return strpos(get_class($obj), '\Null') !== false; return \is_object($obj) && \strpos(\get_class($obj), '\Null') !== false;
} }
} }

View File

@ -40,17 +40,17 @@ final class TestUtils
/** /**
* Set private object member * Set private object member
* *
* @param object $obj Object to modify * @param object|string $obj Object to modify
* @param string $name Member name to modify * @param string $name Member name to modify
* @param mixed $value Value to set * @param mixed $value Value to set
* *
* @return bool The function returns true after setting the member * @return bool The function returns true after setting the member
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function setMember(object $obj, string $name, $value) : bool public static function setMember($obj, string $name, $value) : bool
{ {
$reflectionClass = new \ReflectionClass(\get_class($obj)); $reflectionClass = new \ReflectionClass(\is_string($obj) ? $obj : \get_class($obj));
if (!$reflectionClass->hasProperty($name)) { if (!$reflectionClass->hasProperty($name)) {
return false; return false;
@ -62,7 +62,11 @@ final class TestUtils
$reflectionProperty->setAccessible(true); $reflectionProperty->setAccessible(true);
} }
$reflectionProperty->setValue($obj, $value); if (\is_string($obj)) {
$reflectionProperty->setValue($value);
} elseif (\is_object($obj)) {
$reflectionProperty->setValue($obj, $value);
}
if (!$accessible) { if (!$accessible) {
$reflectionProperty->setAccessible(false); $reflectionProperty->setAccessible(false);
@ -74,16 +78,16 @@ final class TestUtils
/** /**
* Get private object member * Get private object member
* *
* @param object $obj Object to read * @param object|string $obj Object to read
* @param string $name Member name to read * @param string $name Member name to read
* *
* @return mixed Returns the member variable value * @return mixed Returns the member variable value
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function getMember(object $obj, string $name) public static function getMember($obj, string $name)
{ {
$reflectionClass = new \ReflectionClass(\get_class($obj)); $reflectionClass = new \ReflectionClass(\is_string($obj) ? $obj : \get_class($obj));
if (!$reflectionClass->hasProperty($name)) { if (!$reflectionClass->hasProperty($name)) {
return null; return null;
@ -95,7 +99,12 @@ final class TestUtils
$reflectionProperty->setAccessible(true); $reflectionProperty->setAccessible(true);
} }
$value = $reflectionProperty->getValue($obj); $value = null;
if (\is_string($obj)) {
$value = $reflectionProperty->getValue();
} elseif (\is_object($obj)) {
$value = $reflectionProperty->getValue($obj);
}
if (!$accessible) { if (!$accessible) {
$reflectionProperty->setAccessible(false); $reflectionProperty->setAccessible(false);