Skip to content

Commit

Permalink
Merge pull request #54 from driehle/maintenance-work
Browse files Browse the repository at this point in the history
Improved type hints for collections
  • Loading branch information
driehle authored Dec 23, 2021
2 parents d557597 + 32cee1c commit cc7be8b
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 28 deletions.
8 changes: 0 additions & 8 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,5 @@
<!-- Not used for consistency with the Laminas project -->
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix"/>
<!-- This causes BC breaks, as method signatures (return type) change -->
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint"/>
<!-- This causes BC breaks, as method signatures (parameter type) change -->
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint"/>
<!-- This is simply too noisy for now -->
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
</rule>
</ruleset>
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ parameters:
paths:
- src
- tests
ignoreErrors:
-
message: '#expects .*Collection<\(int\|string\), object>, .*ArrayCollection<int, Doctrine.*Entity> given#'
path: tests/DoctrineObjectTest.php
includes:
- vendor/jangregor/phpstan-prophecy/extension.neon
23 changes: 11 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="doctrine-laminas-hydrator">
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="doctrine-laminas-hydrator">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 2 additions & 0 deletions src/DoctrineObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public function getFieldNames(): iterable

/**
* Extract values from an object
*
* @return array<array-key,mixed>
*/
public function extract(object $object): array
{
Expand Down
4 changes: 4 additions & 0 deletions src/Strategy/AbstractCollectionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ protected function getInflector(): Inflector
/**
* Return the collection by value (using the public API)
*
* @return Collection<array-key,object>
*
* @throws InvalidArgumentException
*/
protected function getCollectionFromObjectByValue(): Collection
Expand Down Expand Up @@ -130,6 +132,8 @@ protected function getCollectionFromObjectByValue(): Collection
/**
* Return the collection by reference (not using the public API)
*
* @return Collection<array-key,object>
*
* @throws InvalidArgumentException|ReflectionException
*/
protected function getCollectionFromObjectByReference(): Collection
Expand Down
8 changes: 4 additions & 4 deletions tests/Assets/DifferentAllowRemoveByReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
class DifferentAllowRemoveByReference extends AbstractCollectionStrategy
{
/**
* @param mixed $value
* @param array<array-key, mixed>|null $data
* @param mixed $value
* @param array<array-key,mixed>|null $data
*
* @return Collection|mixed
* @return Collection<array-key,mixed>
*
* @throws ReflectionException
*/
public function hydrate($value, ?array $data)
public function hydrate($value, ?array $data): Collection
{
$collection = $this->getCollectionFromObjectByReference();
$collectionArray = $collection->toArray();
Expand Down
7 changes: 7 additions & 0 deletions tests/Assets/OneToManyArrayEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class OneToManyArrayEntity
{
protected int $id;

/** @var Collection<array-key,object> */
protected Collection $entities;

public function __construct()
Expand All @@ -28,6 +29,9 @@ public function getId(): int
return $this->id;
}

/**
* @psalm-param Collection<array-key,object> $entities
*/
public function addEntities(Collection $entities, bool $modifyValue = true): void
{
foreach ($entities as $entity) {
Expand All @@ -40,6 +44,9 @@ public function addEntities(Collection $entities, bool $modifyValue = true): voi
}
}

/**
* @param Collection<array-key,object> $entities
*/
public function removeEntities(Collection $entities): void
{
foreach ($entities as $entity) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Assets/OneToManyEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class OneToManyEntity
{
protected int $id;

/** @var Collection<array-key,object> */
protected Collection $entities;

public function __construct()
Expand All @@ -28,6 +29,9 @@ public function getId(): int
return $this->id;
}

/**
* @param Collection<array-key,object> $entities
*/
public function addEntities(Collection $entities, bool $modifyValue = true): void
{
foreach ($entities as $entity) {
Expand All @@ -40,13 +44,19 @@ public function addEntities(Collection $entities, bool $modifyValue = true): voi
}
}

/**
* @param Collection<array-key,object> $entities
*/
public function removeEntities(Collection $entities): void
{
foreach ($entities as $entity) {
$this->entities->removeElement($entity);
}
}

/**
* @return Collection<array-key,object>
*/
public function getEntities(bool $modifyValue = true): Collection
{
// Modify the value to illustrate the difference between by value and by reference
Expand Down
3 changes: 3 additions & 0 deletions tests/Assets/OneToManyEntityWithEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public function __construct(?ArrayCollection $entities = null)
$this->entities = $entities;
}

/**
* @return Collection<array-key,object>
*/
public function getEntities(bool $modifyValue = true): Collection
{
return $this->entities;
Expand Down
5 changes: 1 addition & 4 deletions tests/DoctrineObjectTypeConversionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ protected function setUp(): void
->will($this->returnValue($this->metadata));
}

/**
* @param string|null $genericFieldType
*/
public function configureObjectManagerForSimpleEntityWithGenericField($genericFieldType): void
public function configureObjectManagerForSimpleEntityWithGenericField(?string $genericFieldType): void
{
$refl = new ReflectionClass(Assets\SimpleEntityWithGenericField::class);

Expand Down

0 comments on commit cc7be8b

Please sign in to comment.