Skip to content

Commit

Permalink
type declarations and fix some todos
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Apr 21, 2023
1 parent 822f290 commit a9a53ad
Show file tree
Hide file tree
Showing 30 changed files with 692 additions and 1,618 deletions.
71 changes: 29 additions & 42 deletions lib/Doctrine/ODM/PHPCR/ChildrenCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,31 @@
*/
class ChildrenCollection extends PersistentCollection
{
private $document;
private object $document;

/**
* @var array|string|null
*/
private $filter;

private $fetchDepth;
private int $fetchDepth;

private $originalNodeNames;
/**
* @var string[]
*/
private array $originalNodeNames;

private $node;
private NodeInterface $node;

/**
* Creates a new persistent collection.
*
* @param DocumentManagerInterface $dm the DocumentManager the collection will be associated with
* @param object $document The parent document instance
* @param string|array $filter Filter string or array of filter string
* @param int $fetchDepth optional fetch depth, -1 to not override
* @param string $locale The locale to use during the loading of this collection
* @param object $document The parent document instance
* @param string|array $filter Filter string or array of filter string
* @param int $fetchDepth Optional fetch depth, -1 to not override
* @param string|null $locale The locale to use during the loading of this collection
*/
public function __construct(DocumentManagerInterface $dm, $document, $filter = null, $fetchDepth = -1, $locale = null)
public function __construct(DocumentManagerInterface $dm, object $document, $filter = null, int $fetchDepth = -1, ?string $locale = null)
{
$this->dm = $dm;
$this->document = $document;
Expand All @@ -44,31 +49,20 @@ public function __construct(DocumentManagerInterface $dm, $document, $filter = n
}

/**
* @param DocumentManagerInterface $dm the DocumentManager the collection will be associated with
* @param object $document The parent document instance
* @param array|Collection $collection The collection to initialize with
* @param string|array $filter Filter string or array of filter string
* @param int $fetchDepth optional fetch depth, -1 to not override
* @param bool $forceOverwrite If to force overwrite the state in the database to the state of the collection
*
* @return ChildrenCollection
*/
public static function createFromCollection(DocumentManagerInterface $dm, $document, $collection, $filter = null, $fetchDepth = -1, $forceOverwrite = false)
public static function createFromCollection(DocumentManagerInterface $dm, object $document, $collection, $filter = null, int $fetchDepth = -1, bool $forceOverwrite = false): self
{
$childrenCollection = new self($dm, $document, $filter, $fetchDepth);
$childrenCollection->initializeFromCollection($collection, $forceOverwrite);

return $childrenCollection;
}

/**
* @param $fetchDepth
*
* @return NodeInterface
*/
private function getNode($fetchDepth)
private function getNode(int $fetchDepth): NodeInterface
{
if (null === $this->node) {
if (!isset($this->node)) {
$path = $this->dm->getUnitOfWork()->getDocumentId($this->document);
$this->node = $this->dm->getPhpcrSession()->getNode($path, is_int($fetchDepth) ? $fetchDepth : -1);
}
Expand All @@ -77,11 +71,9 @@ private function getNode($fetchDepth)
}

/**
* @param $childNodes
*
* @return NodeInterface[]
*/
private function getChildren($childNodes)
private function getChildren($childNodes): array
{
$uow = $this->dm->getUnitOfWork();
$locale = $this->locale ?: $uow->getCurrentLocale($this->document);
Expand All @@ -99,7 +91,7 @@ private function getChildren($childNodes)
* Initializes the collection by loading its contents from the database
* if the collection is not yet initialized.
*/
public function initialize()
public function initialize(): void
{
if (!$this->isInitialized()) {
$this->getOriginalNodeNames();
Expand All @@ -110,8 +102,7 @@ public function initialize()
}
}

/** {@inheritdoc} */
public function contains($element)
public function contains($element): bool
{
if (!$this->isInitialized()) {
$uow = $this->dm->getUnitOfWork();
Expand Down Expand Up @@ -141,8 +132,7 @@ public function contains($element)
return parent::contains($element);
}

/** {@inheritdoc} */
public function containsKey($key)
public function containsKey($key): bool
{
if (!$this->isInitialized()) {
return in_array($key, $this->getOriginalNodeNames(), true);
Expand All @@ -151,8 +141,7 @@ public function containsKey($key)
return parent::containsKey($key);
}

/** {@inheritdoc} */
public function count()
public function count(): int
{
if (!$this->isInitialized()) {
return count($this->getOriginalNodeNames());
Expand All @@ -161,8 +150,7 @@ public function count()
return parent::count();
}

/** {@inheritdoc} */
public function isEmpty()
public function isEmpty(): bool
{
if (!$this->isInitialized()) {
return !$this->count();
Expand All @@ -171,7 +159,6 @@ public function isEmpty()
return parent::isEmpty();
}

/** {@inheritdoc} */
public function slice($offset, $length = null)
{
if (!$this->isInitialized()) {
Expand Down Expand Up @@ -210,9 +197,9 @@ public function slice($offset, $length = null)
*
* @return string[]
*/
public function getOriginalNodeNames()
public function getOriginalNodeNames(): array
{
if (null === $this->originalNodeNames) {
if (!isset($this->originalNodeNames)) {
if (self::INITIALIZED_FROM_COLLECTION === $this->initialized) {
$this->originalNodeNames = $this->collection->getKeys();
} else {
Expand All @@ -226,13 +213,13 @@ public function getOriginalNodeNames()
/**
* Reset originalNodeNames and mark the collection as non dirty.
*/
public function takeSnapshot()
public function takeSnapshot(): void
{
if (is_array($this->originalNodeNames)) {
if (isset($this->originalNodeNames)) {
if ($this->isInitialized()) {
$this->originalNodeNames = $this->collection->getKeys();
} else {
$this->originalNodeNames = null;
unset($this->originalNodeNames);
}
}

Expand Down
3 changes: 0 additions & 3 deletions lib/Doctrine/ODM/PHPCR/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ public function setDocumentNamespaces(array $documentNamespaces): void

/**
* Sets the driver implementation that is used to retrieve mapping metadata.
*
* @todo Force parameter to be a Closure to ensure lazy evaluation
* (as soon as a metadata cache is in effect, the driver never needs to initialize).
*/
public function setMetadataDriverImpl(MappingDriver $driverImpl, bool $useBuiltInDocumentsDriver = true): void
{
Expand Down
Loading

0 comments on commit a9a53ad

Please sign in to comment.