Skip to content

Commit

Permalink
Merge pull request #67 from driehle/feat/dropPhp74
Browse files Browse the repository at this point in the history
Drop PHP 7.4
  • Loading branch information
driehle authored Jan 29, 2023
2 parents 80435af + 68f1b48 commit c8b6c2a
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
name: "PHPUnit"
uses: "doctrine/.github/.github/workflows/continuous-integration.yml@3.0.0"
with:
php-versions: '["7.4", "8.0", "8.1", "8.2"]'
php-versions: '["8.0", "8.1", "8.2"]'
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"rss": "https://github.com/doctrine/doctrine-laminas-hydrator/releases.atom"
},
"require": {
"php": "^7.4 || ~8.0.0 || ~8.1.0 || ~8.2.0",
"php": "~8.0.0 || ~8.1.0 || ~8.2.0",
"ext-ctype": "*",
"doctrine/collections": "^1.6.8",
"doctrine/inflector": "^2.0.4",
Expand Down
55 changes: 19 additions & 36 deletions src/DoctrineObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,30 @@
use function array_intersect_key;
use function array_key_exists;
use function array_keys;
use function array_merge;
use function assert;
use function ctype_upper;
use function current;
use function get_class;
use function get_class_methods;
use function gettype;
use function in_array;
use function is_array;
use function is_callable;
use function is_int;
use function is_iterable;
use function is_object;
use function is_string;
use function method_exists;
use function property_exists;
use function sprintf;
use function str_ends_with;
use function str_starts_with;
use function strpos;
use function substr;

class DoctrineObject extends AbstractHydrator
{
protected ObjectManager $objectManager;

protected ?ClassMetadata $metadata = null;

protected bool $byValue = true;

/** @var class-string<Strategy\CollectionStrategyInterface> */
protected string $defaultByValueStrategy = AllowRemoveByValue::class;

Expand All @@ -66,11 +63,9 @@ class DoctrineObject extends AbstractHydrator
* @param ObjectManager $objectManager The ObjectManager to use
* @param bool $byValue If set to true, hydrator will always use entity's public API
*/
public function __construct(ObjectManager $objectManager, bool $byValue = true, ?Inflector $inflector = null)
public function __construct(protected ObjectManager $objectManager, protected bool $byValue = true, ?Inflector $inflector = null)
{
$this->objectManager = $objectManager;
$this->byValue = $byValue;
$this->inflector = $inflector ?? InflectorFactory::create()->build();
$this->inflector = $inflector ?? InflectorFactory::create()->build();
}

protected function getClassMetadata(): ClassMetadata
Expand Down Expand Up @@ -122,10 +117,7 @@ public function setDefaultByReferenceStrategy(string $defaultByReferenceStrategy
*/
public function getFieldNames(): iterable
{
$fields = array_merge(
$this->getClassMetadata()->getFieldNames(),
$this->getClassMetadata()->getAssociationNames()
);
$fields = [...$this->getClassMetadata()->getFieldNames(), ...$this->getClassMetadata()->getAssociationNames()];

foreach ($fields as $fieldName) {
$pos = strpos($fieldName, '.');
Expand Down Expand Up @@ -174,7 +166,7 @@ public function hydrate(array $data, object $object): object
*/
protected function prepare(object $object): void
{
$this->metadata = $this->objectManager->getClassMetadata(get_class($object));
$this->metadata = $this->objectManager->getClassMetadata($object::class);
$this->prepareStrategies();
}

Expand Down Expand Up @@ -210,7 +202,7 @@ protected function prepareStrategies(): void
sprintf(
'Strategies used for collections valued associations must inherit from %s, %s given',
Strategy\CollectionStrategyInterface::class,
get_class($strategy)
$strategy::class
)
);
}
Expand Down Expand Up @@ -250,7 +242,7 @@ protected function extractByValue(object $object): array
} elseif (in_array($isser, $methods)) {
$data[$dataFieldName] = $this->extractValue($fieldName, $object->$isser(), $object);
} elseif (
substr($fieldName, 0, 2) === 'is'
str_starts_with($fieldName, 'is')
&& ctype_upper(substr($fieldName, 2, 1))
&& in_array($fieldName, $methods)
) {
Expand Down Expand Up @@ -283,7 +275,7 @@ protected function extractByReference(object $object): array
throw new LogicException(
sprintf(
'this class "%s" is readonly, data can\'t be extracted',
get_class($object)
$object::class
)
);
}
Expand Down Expand Up @@ -441,7 +433,7 @@ protected function hydrateByReference(array $data, ?object $object): object
throw new LogicException(
sprintf(
'Cannot hydrate class "%s" by reference. Property "%s" is readonly. To fix this error, remove readonly.',
get_class($object),
$object::class,
$field
)
);
Expand Down Expand Up @@ -513,9 +505,8 @@ protected function tryConvertArrayToObject(array $data, object $object): ?object
* target will be returned.
*
* @param class-string $target
* @param mixed $value
*/
protected function toOne(string $target, $value): ?object
protected function toOne(string $target, mixed $value): ?object
{
$metadata = $this->objectManager->getClassMetadata($target);

Expand All @@ -540,11 +531,10 @@ protected function toOne(string $target, $value): ?object
* changing the collection of the object
*
* @param class-string $target
* @param mixed $values
*
* @throws InvalidArgumentException
*/
protected function toMany(object $object, string $collectionName, string $target, $values): void
protected function toMany(object $object, string $collectionName, string $target, mixed $values): void
{
$metadata = $this->objectManager->getClassMetadata($target);
$identifier = $metadata->getIdentifier();
Expand Down Expand Up @@ -609,9 +599,7 @@ protected function toMany(object $object, string $collectionName, string $target

$collection = array_filter(
$collection,
static function ($item) {
return $item !== null;
}
static fn ($item) => $item !== null
);

// Set the object so that the strategy can extract the Collection from it
Expand All @@ -630,11 +618,9 @@ static function ($item) {
*
* @link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#doctrine-mapping-types
*
* @param mixed $value
*
* @return mixed|null
*/
protected function handleTypeConversions($value, ?string $typeOfField)
protected function handleTypeConversions(mixed $value, ?string $typeOfField)
{
if ($value === null) {
return null;
Expand Down Expand Up @@ -667,7 +653,7 @@ protected function handleTypeConversions($value, ?string $typeOfField)
return null;
}

$isImmutable = substr($typeOfField, -9) === 'immutable';
$isImmutable = str_ends_with($typeOfField, 'immutable');

// Psalm has troubles with nested conditions, therefore break this into two return statements.
// See https://github.com/vimeo/psalm/issues/6683.
Expand Down Expand Up @@ -712,14 +698,13 @@ protected function handleTypeConversions($value, ?string $typeOfField)
/**
* Find an object by a given target class and identifier
*
* @param mixed $identifiers
* @psalm-param class-string<T> $targetClass
*
* @psalm-return T|null
*
* @template T of object
*/
protected function find($identifiers, string $targetClass): ?object
protected function find(mixed $identifiers, string $targetClass): ?object
{
if ($identifiers instanceof $targetClass) {
return $identifiers;
Expand All @@ -743,12 +728,10 @@ private function isNullIdentifier($identifier): bool
return true;
}

if ($identifier instanceof Traversable || is_array($identifier)) {
if (is_iterable($identifier)) {
$nonNullIdentifiers = array_filter(
ArrayUtils::iteratorToArray($identifier),
static function ($value) {
return $value !== null;
}
static fn ($value) => $value !== null
);

return empty($nonNullIdentifiers);
Expand Down
5 changes: 1 addition & 4 deletions src/Filter/PropertyName.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ final class PropertyName implements FilterInterface
/** @var string[] */
private array $properties = [];

private bool $exclude;

/**
* @param string|string[] $properties The properties to exclude or include.
* @param bool $exclude If the method should be excluded
*/
public function __construct($properties, bool $exclude = true)
public function __construct($properties, private bool $exclude = true)
{
$this->exclude = $exclude;
$this->properties = is_array($properties)
? $properties
: [$properties];
Expand Down
3 changes: 1 addition & 2 deletions src/Strategy/AbstractCollectionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use LogicException;
use ReflectionException;

use function get_class;
use function is_array;
use function method_exists;
use function spl_object_hash;
Expand Down Expand Up @@ -115,7 +114,7 @@ protected function getCollectionFromObjectByValue(): Collection
'The getter %s to access collection %s in object %s does not exist',
$getter,
$this->getCollectionName(),
get_class($object)
$object::class
)
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Strategy/AllowRemoveByValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use LogicException;

use function array_udiff;
use function get_class;
use function method_exists;
use function sprintf;

Expand Down Expand Up @@ -43,7 +42,7 @@ public function hydrate($value, ?array $data)
entity domain code, but one or both seem to be missing',
$adder,
$remover,
get_class($object)
$object::class
)
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Strategy/DisallowRemoveByValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use LogicException;

use function array_udiff;
use function get_class;
use function method_exists;
use function sprintf;

Expand Down Expand Up @@ -41,7 +40,7 @@ public function hydrate($value, ?array $data)
'DisallowRemove strategy for DoctrineModule hydrator requires %s to
be defined in %s entity domain code, but it seems to be missing',
$adder,
get_class($object)
$object::class
)
);
}
Expand Down
10 changes: 2 additions & 8 deletions tests/Assets/ByValueDifferentiatorEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@ class ByValueDifferentiatorEntity

protected string $field;

/**
* @param string|int $id
*/
public function setId($id): void
public function setId(string|int $id): void
{
$this->id = $id;
}

/**
* @return string|int
*/
public function getId()
public function getId(): string|int
{
return $this->id;
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Assets/DifferentAllowRemoveByValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use LogicException;

use function array_udiff;
use function get_class;
use function method_exists;
use function sprintf;

Expand All @@ -35,7 +34,7 @@ public function hydrate($value, ?array $data)
entity domain code, but one or both seem to be missing',
$adder,
$remover,
get_class($object)
$object::class
)
);
}
Expand Down
5 changes: 1 addition & 4 deletions tests/Assets/NamingStrategyEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

class NamingStrategyEntity
{
protected ?string $camelCase = null;

public function __construct(?string $camelCase = null)
public function __construct(protected ?string $camelCase = null)
{
$this->camelCase = $camelCase;
}

public function setCamelCase(?string $camelCase): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Assets/OneToOneEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OneToOneEntity
{
protected int $id;

protected ?ByValueDifferentiatorEntity $toOne;
protected ?ByValueDifferentiatorEntity $toOne = null;

protected DateTime $createdAt;

Expand Down
10 changes: 2 additions & 8 deletions tests/Assets/SimpleEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ class SimpleEntity

protected string $field;

/**
* @param string|int $id
*/
public function setId($id): void
public function setId(string|int $id): void
{
$this->id = $id;
}

/**
* @return string|int
*/
public function getId()
public function getId(): string|int
{
return $this->id;
}
Expand Down
8 changes: 1 addition & 7 deletions tests/Assets/SimpleEntityReadonlyPhp82.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@

readonly class SimpleEntityReadonlyPhp82
{
protected ?int $id;

protected ?string $field;

public function __construct(?int $id, ?string $field)
public function __construct(protected ?int $id, protected ?string $field)
{
$this->id = $id;
$this->field = $field;
}
}
2 changes: 1 addition & 1 deletion tests/Assets/SimpleEntityWithDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SimpleEntityWithDateTime
{
protected int $id;

protected ?DateTime $date;
protected ?DateTime $date = null;

public function setId(int $id): void
{
Expand Down
5 changes: 1 addition & 4 deletions tests/Assets/SimpleEntityWithGenericField.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public function getId(): ?int
return $this->id;
}

/**
* @param mixed $value
*/
public function setGenericField($value): void
public function setGenericField(mixed $value): void
{
$this->genericField = $value;
}
Expand Down
Loading

0 comments on commit c8b6c2a

Please sign in to comment.