Skip to content

Conversation

@krzysztof-ciszewski
Copy link

Q A
Type improvement
BC Break yes
Fixed issues #2850

Summary

Added parameter and return types to the PersistentCollectionTrait in preparation for doctrine/collection update

@krzysztof-ciszewski krzysztof-ciszewski changed the title added types [3.x] Added parameter and return types to the PersistentCollectionTrait Nov 29, 2025
Copy link
Member

@IonBazan IonBazan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also check remaining methods in that file and add the types there

* @return true
*/
private function doAdd($value, $arrayAccess)
private function doAdd($value, bool $arrayAccess): true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use native mixed type

}

public function add($element)
public function add($element): bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function add($element): bool
public function add(mixed $element): bool

}

public function set($key, $value)
public function set($key, $value): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see if we can add types to the key and value here too

/** @return void */
#[ReturnTypeWillChange]
public function offsetUnset($offset)
public function offsetUnset(mixed $offset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function offsetUnset(mixed $offset)
public function offsetUnset(mixed $offset): void

This also allows to remove the PHP doc and #[ReturnTypeWillChange]

}

/**
* @phpstan-return (TMaybeContained is T ? TKey|false : false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think should stay

@IonBazan IonBazan requested review from GromNaN and Copilot November 30, 2025 04:11
Copilot finished reviewing on behalf of IonBazan November 30, 2025 04:13
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds parameter and return types to the PersistentCollectionTrait in preparation for a doctrine/collections update. This is marked as a breaking change, as it introduces strict type declarations throughout the trait and corresponding interface.

Key changes:

  • Added return types to all public methods in PersistentCollectionTrait and PersistentCollectionInterface
  • Added parameter types where missing (e.g., converting $dirty to bool $dirty)
  • Updated PHPDoc annotations to align with new type signatures
  • Updated test mocks to match new return type expectations

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
tests/Tests/PersistentCollectionTest.php Updated test mocks to return correct types matching new method signatures (empty array for slice(), null for remove())
src/PersistentCollection/PersistentCollectionTrait.php Added comprehensive return types and parameter types to all trait methods, with supporting PHPDoc annotations
src/PersistentCollection/PersistentCollectionInterface.php Added return types to interface methods and cleaned up redundant PHPDoc annotations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

public function getMapping()
/** @return array<string, mixed> */
public function getMapping(): array
{
Copy link

Copilot AI Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getMapping() method declares a return type of array, but the $mapping property is declared as ?array (nullable) on line 57 and initialized to null. This method can return null, which violates the type declaration. The return type should be array|null or the method should throw an exception when $mapping is null.

Suggested change
{
{
if ($this->mapping === null) {
throw new MongoDBException('No mapping is associated with this PersistentCollection, please set one using setOwner method.');
}

Copilot uses AI. Check for mistakes.
}

public function set($key, $value)
public function set($key, $value): void
Copy link

Copilot AI Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The set() method is missing parameter types. Based on the PHPDoc annotations for similar methods and the template parameters, $key should be typed as TKey (string|int) and $value should be typed as T (mixed or object). Consider adding type declarations for consistency with other methods in this trait.

Suggested change
public function set($key, $value): void
public function set(string|int $key, mixed $value): void

Copilot uses AI. Check for mistakes.
}

public function add($element)
public function add($element): bool
Copy link

Copilot AI Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add() method is missing a parameter type for $element. Based on the template parameter T of object and consistency with other methods, this should be typed. Consider adding a type declaration for the parameter.

Suggested change
public function add($element): bool
public function add(object $element): bool

Copilot uses AI. Check for mistakes.
}

public function removeElement($element)
public function removeElement($element): bool
Copy link

Copilot AI Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removeElement() method is missing a parameter type for $element. Based on the template parameter T of object and consistency with other methods in this trait, this parameter should have a type declaration.

Suggested change
public function removeElement($element): bool
public function removeElement(object $element): bool

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants