Skip to content

Commit

Permalink
Add more phpdoc type info (#2330)
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored Jun 29, 2021
1 parent e00e9e9 commit 0c0f04b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 14 deletions.
8 changes: 5 additions & 3 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/AtomicSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ public function testReplacementOfEmbedManyElements()
$firstChapter->name = 'First chapter A';

// Developers commonly attempt to replace the contents of an EmbedMany with a new ArrayCollection like this:
/** @var ArrayCollection<int, Chapter> $replacementChapters */
$replacementChapters = new ArrayCollection();
$replacementChapters->add($firstChapter);
$replacementChapters->add(new Chapter('Second chapter B'));
Expand All @@ -478,9 +479,10 @@ public function testReplacementOfIdentifiedEmbedManyElements()
$this->dm->flush();
$this->dm->clear();

$book = $this->dm->getRepository(Book::CLASSNAME)->findOneBy(['_id' => $book->id]);
$firstChapter = $book->identifiedChapters->first();
$firstChapter->name = 'First chapter A';
$book = $this->dm->getRepository(Book::CLASSNAME)->findOneBy(['_id' => $book->id]);
$firstChapter = $book->identifiedChapters->first();
$firstChapter->name = 'First chapter A';
/** @var ArrayCollection<int, IdentifiedChapter> $replacementChapters */
$replacementChapters = new ArrayCollection();
$replacementChapters->add($firstChapter);
$replacementChapters->add(new IdentifiedChapter('Second chapter B'));
Expand Down
15 changes: 13 additions & 2 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/EmbeddedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Tests\Functional;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\PersistentCollection;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
Expand Down Expand Up @@ -560,6 +561,8 @@ public function testWhenCopyingManyEmbedSubDocumentsFromOneDocumentToAnotherWill
$this->dm->persist($test1);
$this->dm->flush();

assert($test1->embedMany instanceof Collection);

$test2 = new ChangeEmbeddedIdTest();
$test2->embedMany = $test1->embedMany; //using clone will work
$this->dm->persist($test2);
Expand Down Expand Up @@ -642,10 +645,18 @@ class ChangeEmbeddedIdTest
/** @ODM\Id */
public $id;

/** @ODM\EmbedOne(targetDocument=EmbeddedDocumentWithId::class) */
/**
* @ODM\EmbedOne(targetDocument=EmbeddedDocumentWithId::class)
*
* @var EmbeddedDocumentWithId|null
*/
public $embed;

/** @ODM\EmbedMany(targetDocument=EmbeddedDocumentWithId::class) */
/**
* @ODM\EmbedMany(targetDocument=EmbeddedDocumentWithId::class)
*
* @var Collection<int, EmbeddedDocumentWithId>|array<EmbeddedDocumentWithId>
*/
public $embedMany;

public function __construct()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testSavesEmbeddedDocumentsInReferencedDocument()

$project = $this->dm->find(Project::class, $project->getId());

/** @var ArrayCollection<int, SubProject> $subProjects */
$subProjects = new ArrayCollection();
$subProject1 = new SubProject('Sub Project #1');
$subProject2 = new SubProject('Sub Project #2');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
Expand All @@ -20,6 +21,7 @@ public function testClearCollection()
$doc->embeds->clear();
$doc->embeds->add(new GH1011Embedded('test2'));
$this->uow->computeChangeSets();
$this->assertInstanceOf(PersistentCollectionInterface::class, $doc->embeds);
$this->assertTrue($this->uow->isCollectionScheduledForUpdate($doc->embeds));
$this->assertFalse($this->uow->isCollectionScheduledForDeletion($doc->embeds));
}
Expand Down Expand Up @@ -47,7 +49,11 @@ class GH1011Document
/** @ODM\Id */
public $id;

/** @ODM\EmbedMany(targetDocument=GH1011Embedded::class, strategy="set") */
/**
* @ODM\EmbedMany(targetDocument=GH1011Embedded::class, strategy="set")
*
* @var Collection<int, GH1011Embedded>
*/
public $embeds;

public function __construct()
Expand Down
13 changes: 11 additions & 2 deletions tests/Documents/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Documents;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/** @ODM\Document */
Expand All @@ -18,10 +19,18 @@ class Book
/** @ODM\Field(type="int") @ODM\Version */
public $version = 1;

/** @ODM\EmbedMany(targetDocument=Chapter::class, strategy="atomicSet") */
/**
* @ODM\EmbedMany(targetDocument=Chapter::class, strategy="atomicSet")
*
* @var Collection<int, Chapter>
*/
public $chapters;

/** @ODM\EmbedMany(targetDocument=IdentifiedChapter::class, strategy="atomicSet") */
/**
* @ODM\EmbedMany(targetDocument=IdentifiedChapter::class, strategy="atomicSet")
*
* @var Collection<int, IdentifiedChapter>
*/
public $identifiedChapters;

public function __construct()
Expand Down
7 changes: 6 additions & 1 deletion tests/Documents/IdentifiedChapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Documents;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/** @ODM\EmbeddedDocument */
Expand All @@ -16,7 +17,11 @@ class IdentifiedChapter
/** @ODM\Field(type="string") */
public $name;

/** @ODM\EmbedMany(targetDocument=Page::class) */
/**
* @ODM\EmbedMany(targetDocument=Page::class)
*
* @var Collection<int, Page>
*/
public $pages;

public function __construct($name = null)
Expand Down
25 changes: 21 additions & 4 deletions tests/Documents/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,27 @@ class Project
/** @ODM\Field(type="string") */
private $name;

/** @ODM\EmbedOne(targetDocument=Address::class) */
/**
* @ODM\EmbedOne(targetDocument=Address::class)
*
* @var Address|null
*/
private $address;

/** @ODM\ReferenceMany(targetDocument=SubProject::class, cascade="all") */
/**
* @ODM\ReferenceMany(targetDocument=SubProject::class, cascade="all")
*
* @var Collection<int, SubProject>
*/
private $subProjects;

public function __construct($name, ?Collection $subProjects = null)
/**
* @param Collection<int, SubProject>|null $subProjects
*/
public function __construct(string $name, ?Collection $subProjects = null)
{
$this->name = $name;
$this->subProjects = $subProjects ? $subProjects : new ArrayCollection();
$this->subProjects = $subProjects ?? new ArrayCollection();
}

public function getId()
Expand Down Expand Up @@ -59,11 +70,17 @@ public function getAddress()
return $this->address;
}

/**
* @param Collection<int, SubProject> $subProjects
*/
public function setSubProjects(Collection $subProjects)
{
$this->subProjects = $subProjects;
}

/**
* @return Collection<int, SubProject>
*/
public function getSubProjects()
{
return $this->subProjects;
Expand Down
12 changes: 11 additions & 1 deletion tests/Documents/SubProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@
/** @ODM\Document */
class SubProject extends Project
{
/** @ODM\EmbedMany(targetDocument=Issue::class) */
/**
* @ODM\EmbedMany(targetDocument=Issue::class)
*
* @var Collection<int, Issue>
*/
private $issues;

/**
* @return Collection<int, Issue>
*/
public function getIssues()
{
return $this->issues;
}

/**
* @param Collection<int, Issue> $issues
*/
public function setIssues(Collection $issues)
{
$this->issues = $issues;
Expand Down

0 comments on commit 0c0f04b

Please sign in to comment.