New PHP 7.1 type hints for entities and repositories.

This commit is contained in:
Alexey Skobkin 2017-01-13 01:58:52 +03:00
parent 069aa27bb4
commit 89998a6418
17 changed files with 107 additions and 493 deletions

View File

@ -107,182 +107,101 @@ class Comment
$this->children = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
public function getId(): int
{
return $this->id;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return Comment
*/
public function setCreatedAt($createdAt)
public function setCreatedAt(\DateTime $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
/**
* Set text
*
* @param string $text
* @return Comment
*/
public function setText($text)
public function setText(string $text): self
{
$this->text = $text;
return $this;
}
/**
* Get text
*
* @return string
*/
public function getText()
public function getText(): string
{
return $this->text;
}
/**
* Set isRec
*
* @param boolean $rec
* @return Comment
*/
public function setRec($rec)
public function setRec(bool $rec): self
{
$this->rec = $rec;
return $this;
}
/**
* Get isRec
*
* @return boolean
*/
public function isRec()
public function isRec(): bool
{
return $this->rec;
}
/**
* Get isRec
*
* @return boolean
*/
public function getRec()
public function getRec(): bool
{
return $this->rec;
}
/**
* @return Post
*/
public function getPost()
public function getPost(): Post
{
return $this->post;
}
/**
* @param Post $post
* @return Comment
*/
public function setPost($post)
public function setPost(Post $post): self
{
$this->post = $post;
return $this;
}
/**
* Set number
*
* @param int $number
* @return Comment
*/
public function setNumber($number)
public function setNumber(int $number): self
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* @return int
*/
public function getNumber()
public function getNumber(): int
{
return $this->number;
}
/**
* @return User
*/
public function getAuthor()
public function getAuthor(): User
{
return $this->author;
}
/**
* @param User $author
* @return Comment
*/
public function setAuthor($author)
public function setAuthor(User $author): self
{
$this->author = $author;
return $this;
}
/**
* Add files
*
* @param File $files
* @return Comment
*/
public function addFile(File $files)
public function addFile(File $files): self
{
$this->files[] = $files;
return $this;
}
/**
* Remove files
*
* @param File $files
*/
public function removeFile(File $files)
public function removeFile(File $files): void
{
$this->files->removeElement($files);
}
/**
* Get files
*
* @return File[]|ArrayCollection
*/
public function getFiles()
@ -293,81 +212,48 @@ class Comment
/**
* @return Comment
*/
public function getParent()
public function getParent(): ?Comment
{
return $this->parent;
}
/**
* @param Comment $parent
* @return Comment
*/
public function setParent($parent)
public function setParent(Comment $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* Set deleted
*
* @param boolean $deleted
* @return Comment
*/
public function setDeleted($deleted)
public function setDeleted(bool $deleted): self
{
$this->deleted = $deleted;
return $this;
}
/**
* Get deleted
*
* @return boolean
*/
public function getDeleted()
public function getDeleted(): bool
{
return $this->deleted;
}
/**
* Get deleted
*
* @return boolean
*/
public function isDeleted()
public function isDeleted(): bool
{
return $this->deleted;
}
/**
* Add children
*
* @param Comment $children
* @return Comment
*/
public function addChild(Comment $children)
public function addChild(Comment $children): self
{
$this->children[] = $children;
return $this;
}
/**
* Remove children
*
* @param Comment $children
*/
public function removeChild(Comment $children)
public function removeChild(Comment $children): void
{
$this->children->removeElement($children);
}
/**
* Get children
*
* @return Comment[]|ArrayCollection
*/
public function getChildren()

View File

@ -5,10 +5,8 @@ namespace Skobkin\Bundle\PointToolsBundle\Entity\Blogs;
use Doctrine\ORM\Mapping as ORM;
/**
* File
*
* @ORM\Table(name="files", schema="posts")
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\FileRepository")
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\FileRepository", readOnly=true)
*/
class File
{
@ -29,40 +27,17 @@ class File
private $remoteUrl;
public function __construct($remoteUrl = null)
public function __construct($remoteUrl)
{
$this->remoteUrl = $remoteUrl;
}
/**
* Get id
*
* @return integer
*/
public function getId()
public function getId(): int
{
return $this->id;
}
/**
* Set remoteUrl
*
* @param string $remoteUrl
* @return File
*/
public function setRemoteUrl($remoteUrl)
{
$this->remoteUrl = $remoteUrl;
return $this;
}
/**
* Get remoteUrl
*
* @return string
*/
public function getRemoteUrl()
public function getRemoteUrl(): string
{
return $this->remoteUrl;
}

View File

@ -7,8 +7,6 @@ use Doctrine\ORM\Mapping as ORM;
use Skobkin\Bundle\PointToolsBundle\Entity\User;
/**
* Post
*
* @ORM\Table(name="posts", schema="posts", indexes={
* @ORM\Index(name="idx_post_created_at", columns={"created_at"}),
* @ORM\Index(name="idx_post_private", columns={"private"}),
@ -18,8 +16,8 @@ use Skobkin\Bundle\PointToolsBundle\Entity\User;
*/
class Post
{
const TYPE_POST = 'post';
const TYPE_FEED = 'feed';
public const TYPE_POST = 'post';
public const TYPE_FEED = 'feed';
/**
* @var string
@ -121,135 +119,77 @@ class Post
/**
* @ORM\PreUpdate
*/
public function preUpdate()
public function preUpdate(): void
{
$this->updatedAt = new \DateTime();
}
/**
* Get id
*
* @return string
*/
public function getId()
public function getId(): string
{
return $this->id;
}
/**
* Set text
*
* @param string $text
* @return Post
*/
public function setText($text)
public function setText(string $text): self
{
$this->text = $text;
return $this;
}
/**
* Get text
*
* @return string
*/
public function getText()
public function getText(): string
{
return $this->text;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return Post
*/
public function setCreatedAt($createdAt)
public function setCreatedAt(\DateTime $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
public function getCreatedAt(): \DateTime
{
return $this->createdAt;
}
/**
* @return \DateTime
*/
public function getUpdatedAt()
public function getUpdatedAt(): ?\DateTime
{
return $this->updatedAt;
}
/**
* Set type
*
* @param string $type
* @return Post
*/
public function setType($type)
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
public function getType(): string
{
return $this->type;
}
/**
* @return User
*/
public function getAuthor()
public function getAuthor(): User
{
return $this->author;
}
/**
* @param User $author
* @return Post
*/
public function setAuthor($author)
public function setAuthor(User $author): self
{
$this->author = $author;
return $this;
}
/**
* Add files
*
* @param File $files
* @return Post
*/
public function addFile(File $files)
public function addFile(File $files): self
{
$this->files[] = $files;
return $this;
}
/**
* Remove files
*
* @param File $files
*/
public function removeFile(File $files)
public function removeFile(File $files): void
{
$this->files->removeElement($files);
}
@ -264,13 +204,7 @@ class Post
return $this->files;
}
/**
* Add post tags
*
* @param PostTag $tag
* @return Post
*/
public function addPostTag(PostTag $tag)
public function addPostTag(PostTag $tag): self
{
$tag->setPost($this);
$this->postTags[] = $tag;
@ -278,12 +212,7 @@ class Post
return $this;
}
/**
* Remove tags
*
* @param PostTag $tag
*/
public function removePostTag(PostTag $tag)
public function removePostTag(PostTag $tag): void
{
$this->postTags->removeElement($tag);
}
@ -298,69 +227,41 @@ class Post
return $this->postTags;
}
/**
* Set deleted
*
* @param boolean $deleted
* @return Post
*/
public function setDeleted($deleted)
public function setDeleted(bool $deleted): self
{
$this->deleted = $deleted;
return $this;
}
/**
* Get deleted
*
* @return boolean
*/
public function getDeleted()
public function getDeleted(): bool
{
return $this->deleted;
}
/**
* Get deleted
*
* @return boolean
*/
public function isDeleted()
public function isDeleted(): bool
{
return $this->deleted;
}
/**
* Set private
*
* @param boolean $private
* @return Post
*/
public function setPrivate($private)
public function setPrivate(bool $private): self
{
$this->private = $private;
return $this;
}
/**
* Get private
*
* @return boolean
*/
public function getPrivate()
public function isPrivate(): bool
{
return $this->private;
}
/**
* Add comments
*
* @param Comment $comment
* @return Post
*/
public function addComment(Comment $comment)
public function getPrivate(): bool
{
return $this->private;
}
public function addComment(Comment $comment): self
{
$this->comments[] = $comment;
$comment->setPost($this);
@ -368,12 +269,7 @@ class Post
return $this;
}
/**
* Remove comments
*
* @param Comment $comment
*/
public function removeComment(Comment $comment)
public function removeComment(Comment $comment): void
{
$this->comments->removeElement($comment);
}

View File

@ -5,15 +5,13 @@ namespace Skobkin\Bundle\PointToolsBundle\Entity\Blogs;
use Doctrine\ORM\Mapping as ORM;
/**
* PostTag
*
* @ORM\Table(name="posts_tags", schema="posts")
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\PostTagRepository")
*/
class PostTag
{
/**
* @var integer
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
@ -32,6 +30,8 @@ class PostTag
/**
* @var Tag
*
* @todo fix SET NULL
*
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag", fetch="EAGER")
* @ORM\JoinColumn(name="tag_id", onDelete="SET NULL")
*/
@ -45,53 +45,29 @@ class PostTag
private $text;
/**
* PostTag constructor.
*
* @param Tag $tag
*/
public function __construct(Tag $tag)
{
$this->tag = $tag;
}
/**
* Get id
*
* @return integer
*/
public function getId()
public function getId(): int
{
return $this->id;
}
/**
* Set text
*
* @param string $text
* @return PostTag
*/
public function setText($text)
public function setText(string $text): self
{
$this->text = $text;
return $this;
}
/**
* Get text
*
* @return string
*/
public function getText()
public function getText(): string
{
return $this->text;
}
/**
* @return string
*/
public function getOriginalTagText()
public function getOriginalTagText(): string
{
return $this->tag ? $this->tag->getText() : '';
}
@ -99,45 +75,24 @@ class PostTag
/**
* Set post
*
* @todo move to constructor
*
* @param Post $post
* @return PostTag
*/
public function setPost(Post $post = null)
public function setPost(Post $post = null): self
{
$this->post = $post;
return $this;
}
/**
* Get post
*
* @return Post
*/
public function getPost()
public function getPost(): Post
{
return $this->post;
}
/**
* Set tag
*
* @param Tag $tag
* @return PostTag
*/
public function setTag(Tag $tag = null)
{
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* @return Tag
*/
public function getTag()
public function getTag(): Tag
{
return $this->tag;
}

View File

@ -5,17 +5,15 @@ namespace Skobkin\Bundle\PointToolsBundle\Entity\Blogs;
use Doctrine\ORM\Mapping as ORM;
/**
* Tag
*
* @ORM\Table(name="tags", schema="posts", indexes={
* @ORM\Index(name="idx_tag_text", columns={"text"})
* })
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\TagRepository")
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\TagRepository", readOnly=true)
*/
class Tag
{
/**
* @var integer
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
@ -31,43 +29,17 @@ class Tag
private $text;
/**
* @param string $text
*/
public function __construct($text)
public function __construct(string $text)
{
$this->text = $text;
}
/**
* Get id
*
* @return integer
*/
public function getId()
public function getId(): int
{
return $this->id;
}
/**
* Set text
*
* @param string $text
* @return Tag
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* Get text
*
* @return string
*/
public function getText()
public function getText(): string
{
return $this->text;
}

View File

@ -14,8 +14,8 @@ use Doctrine\ORM\Mapping as ORM;
*/
class SubscriptionEvent
{
const ACTION_SUBSCRIBE = 'subscribe';
const ACTION_UNSUBSCRIBE = 'unsubscribe';
public const ACTION_SUBSCRIBE = 'subscribe';
public const ACTION_UNSUBSCRIBE = 'unsubscribe';
/**
* @var int

View File

@ -6,8 +6,6 @@ use Doctrine\ORM\Mapping as ORM;
use Skobkin\Bundle\PointToolsBundle\Entity\User;
/**
* Account
*
* @ORM\Table(name="telegram_accounts", schema="users", indexes={
* @ORM\Index(name="subscriber_notification_idx", columns={"subscriber_notification"}, options={"where": "subscriber_notification = TRUE"}),
* @ORM\Index(name="rename_notification_idx", columns={"rename_notification"}, options={"where": "rename_notification = TRUE"}),
@ -113,7 +111,7 @@ class Account
/**
* @ORM\PrePersist()
*/
public function prePersist()
public function prePersist(): void
{
$this->createdAt = new \DateTime();
}
@ -121,7 +119,7 @@ class Account
/**
* @ORM\PreUpdate()
*/
public function preUpdate()
public function preUpdate(): void
{
$this->updatedAt = new \DateTime();
}
@ -167,12 +165,12 @@ class Account
return $this;
}
public function getLastName(): string
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName = null): Account
public function setLastName(?string $lastName = null): Account
{
$this->lastName = $lastName;
@ -203,10 +201,7 @@ class Account
return $this->chatId;
}
/**
* @return User|null
*/
public function getUser()
public function getUser(): ?User
{
return $this->user;
}

View File

@ -73,36 +73,22 @@ class User
private $newSubscriberEvents;
/**
* @param int $id
* @param string $login
* @param string $name
*/
public function __construct(int $id, string $login = null, string $name = null)
{
$this->id = $id;
$this->login = $login;
$this->name = $name;
$this->createdAt = new \DateTime();
$this->subscribers = new ArrayCollection();
$this->subscriptions = new ArrayCollection();
$this->newSubscriberEvents = new ArrayCollection();
}
/**
* @ORM\PrePersist
*/
public function onCreate()
{
if (!$this->createdAt) {
$this->createdAt = new \DateTime();
}
}
/**
* @ORM\PreUpdate
*/
public function preUpdate()
public function preUpdate(): void
{
$this->updatedAt = new \DateTime();
}
@ -112,10 +98,6 @@ class User
return $this->id;
}
/**
* @param string $login
* @return User
*/
public function setLogin(string $login): self
{
$this->login = $login;
@ -128,17 +110,14 @@ class User
return $this->login;
}
public function setName(string $name = null): self
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return string|null
*/
public function getName()
public function getName(): ?string
{
return $this->name;
}
@ -198,7 +177,7 @@ class User
return $this;
}
public function getUpdatedAt(): \DateTime
public function getUpdatedAt(): ?\DateTime
{
return $this->updatedAt;
}

View File

@ -7,7 +7,7 @@ use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment;
class CommentRepository extends EntityRepository
{
public function add(Comment $entity)
public function add(Comment $entity): void
{
$this->getEntityManager()->persist($entity);
}

View File

@ -7,7 +7,7 @@ use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\File;
class FileRepository extends EntityRepository
{
public function add(File $entity)
public function add(File $entity): void
{
$this->getEntityManager()->persist($entity);
}

View File

@ -8,18 +8,19 @@ use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
class PostRepository extends EntityRepository
{
public function add(Post $entity)
public function add(Post $entity): void
{
$this->getEntityManager()->persist($entity);
}
public function getPostWithComments($postId)
public function getPostWithComments(string $postId): ?Post
{
/** @var QueryBuilder $qb */
$qb = $this->createQueryBuilder('p');
return $qb
->select(['p', 'c', 'a'])
->leftJoin('p.comments', 'c')
// @todo Optimize https://ocramius.github.io/blog/doctrine-orm-optimization-hydration/
->leftJoin('c.author', 'a')
->where($qb->expr()->eq('p.id', ':post_id'))
->orderBy('c.number', 'asc')

View File

@ -3,7 +3,12 @@
namespace Skobkin\Bundle\PointToolsBundle\Repository\Blogs;
use Doctrine\ORM\EntityRepository;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\PostTag;
class PostTagRepository extends EntityRepository
{
public function add(PostTag $entity): void
{
$this->getEntityManager()->persist($entity);
}
}

View File

@ -7,17 +7,12 @@ use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Tag;
class TagRepository extends EntityRepository
{
public function add(Tag $entity)
public function add(Tag $entity): void
{
$this->getEntityManager()->persist($entity);
}
/**
* @param $text
* @return Tag|null
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneByLowerText($text)
public function findOneByLowerText(string $text): ?Tag
{
$qb = $this->createQueryBuilder('t');
return $qb

View File

@ -9,7 +9,7 @@ use Skobkin\Bundle\PointToolsBundle\Entity\User;
class SubscriptionEventRepository extends EntityRepository
{
public function add(SubscriptionEvent $entity)
public function add(SubscriptionEvent $entity): void
{
$this->getEntityManager()->persist($entity);
}
@ -21,12 +21,12 @@ class SubscriptionEventRepository extends EntityRepository
{
$qb = $this->createQueryBuilder('se');
$now = new \DateTime();
$from = (new \DateTime())->sub(new \DateInterval('PT24H'));
return $qb
->select('COUNT(se)')
->where('se.date > :time')
->setParameter('time', $now->sub(new \DateInterval('PT24H')))
->where('se.date > :from_time')
->setParameter('from_time', $from)
->getQuery()->getSingleScalarResult()
;
}

View File

@ -8,7 +8,7 @@ use Skobkin\Bundle\PointToolsBundle\Entity\User;
class SubscriptionRepository extends EntityRepository
{
public function add(Subscription $entity)
public function add(Subscription $entity): void
{
$this->getEntityManager()->persist($entity);
}
@ -18,12 +18,8 @@ class SubscriptionRepository extends EntityRepository
*
* @return int
*/
public function getUserSubscribersCountById($id): int
public function getUserSubscribersCountById(int $id): int
{
if (!is_int($id)) {
throw new \InvalidArgumentException('$id must be an integer');
}
$qb = $this->createQueryBuilder('s');
return $qb
->select('COUNT(s.subscriber)')
@ -38,7 +34,7 @@ class SubscriptionRepository extends EntityRepository
* @param User $user
* @param User[] $subscribers
*/
public function removeSubscribers(User $user, array $subscribers)
public function removeSubscribers(User $user, array $subscribers): void
{
$qb = $this->createQueryBuilder('s');
$qb

View File

@ -7,50 +7,11 @@ use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account;
class AccountRepository extends EntityRepository
{
public function add(Account $entity)
public function add(Account $entity): void
{
$this->getEntityManager()->persist($entity);
}
/**
* @todo remove if not used
*
* @param array $criteria
* @param array|null $orderBy
* @param int|null $limit
* @param int|null $offset
*
* @return Account[]
*/
public function findLinkedAccountsBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): array
{
$qb = $this->createQueryBuilder('a');
$i = 0;
foreach ($criteria as $property => $value) {
$qb
->andWhere('a.'.$property.' = :criteria_'.$i)
->setParameter('criteria_'.$i, $value)
;
}
if (null !== $orderBy) {
foreach ($orderBy as $property => $order) {
$qb->addOrderBy($property, $order);
}
}
if (null !== $limit) {
$qb->setMaxResults($limit);
}
if (null !== $offset) {
$qb->setFirstResult($offset);
}
return $qb->getQuery()->getResult();
}
/**
* Returns total number of accounts
*

View File

@ -15,10 +15,8 @@ class UserRepository extends EntityRepository
/**
* Case-insensitive user search
*
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findUserByLogin(string $login): User
public function findUserByLogin(string $login): ?User
{
$qb = $this->createQueryBuilder('u');