From eb5574fb63127d205db7a4a21794274aa5ac1e4b Mon Sep 17 00:00:00 2001 From: dimitri-bouteille Date: Fri, 19 Jan 2024 22:05:03 +0100 Subject: [PATCH] Finish clean Meta builder, CS, PHPStan level 5, clean Api models --- phpstan.neon | 2 +- src/Api/CommentInterface.php | 199 ------------------ src/Api/OptionInterface.php | 18 -- src/Api/PostInterface.php | 50 ----- src/Api/TermInterface.php | 9 - src/Api/TermRelationshipInterface.php | 9 - src/Api/TermTaxonomyInterface.php | 13 -- src/Api/UserInterface.php | 38 ---- src/Builders/AbstractBuilder.php | 11 - src/Builders/AbstractWithMetaBuilder.php | 174 +++++++++++++++ src/Builders/PostBuilder.php | 5 +- src/Builders/TermBuilder.php | 4 +- src/Builders/Traits/WithMeta.php | 139 ------------ src/Builders/UserBuilder.php | 5 +- .../WithMeta.php => Concerns/HasMeta.php} | 19 +- src/Models/Comment.php | 152 +++++++++---- src/Models/Option.php | 15 +- src/Models/Post.php | 52 ++++- src/Models/Term.php | 12 +- src/Models/TermRelationship.php | 8 + src/Models/TermTaxonomy.php | 10 + src/Models/User.php | 39 +++- src/Orm/AbstractModel.php | 7 +- src/Orm/Database.php | 69 ++---- src/Taps/Comment/IsCommentTypeTap.php | 35 +++ 25 files changed, 473 insertions(+), 621 deletions(-) create mode 100644 src/Builders/AbstractWithMetaBuilder.php delete mode 100644 src/Builders/Traits/WithMeta.php rename src/{Models/Meta/WithMeta.php => Concerns/HasMeta.php} (90%) create mode 100644 src/Taps/Comment/IsCommentTypeTap.php diff --git a/phpstan.neon b/phpstan.neon index 1580cc60..3dfb5991 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 2 + level: 5 paths: - src excludePaths: diff --git a/src/Api/CommentInterface.php b/src/Api/CommentInterface.php index 1d3ae28c..57b306b0 100644 --- a/src/Api/CommentInterface.php +++ b/src/Api/CommentInterface.php @@ -8,41 +8,7 @@ namespace Dbout\WpOrm\Api; -use Carbon\Carbon; -use Dbout\WpOrm\Models\Comment; -use Dbout\WpOrm\Models\Post; -use Dbout\WpOrm\Models\User; - /** - * @method Comment setCommentAuthor(?string $author) - * @method string|null getCommentAuthor() - * @method Comment setCommentAuthorEmail(?string $email) - * @method string|null getCommentAuthorEmail() - * @method Comment setCommentAuthorUrl(?string $url) - * @method string|null getCommentAuthorUrl() - * @method Comment setCommentAuthorIP(?string $ip) - * @method string|null getCommentAuthorIP() - * @method Comment setCommentContent(?string $content) - * @method string|null getCommentContent() - * @method Comment setCommentKarma(?int $karma) - * @method int|null getCommentKarma() - * @method Comment setCommentApproved(string $approved) - * @method string getCommentApproved() - * @method Comment setCommentAgent(?string $agent) - * @method string|null getCommentAgent() - * @method Comment setCommentType(?string $type) - * @method string|null getCommentType() - * @method Comment setUserId(?int $userId) - * @method int|null getUserId() - * @method Comment setCommentDate(mixed $date) - * @method Carbon|null getCommentDate() - * @method Comment setCommentDateGmt(mixed $date) - * @method Carbon|null getCommentDateGmt() - * - * @property-read User|null $user - * @property-read Post|null $post - * @property-read Comment|null $parent - * * @since 3.0.0 */ interface CommentInterface @@ -62,169 +28,4 @@ interface CommentInterface public const TYPE = 'comment_type'; public const PARENT = 'comment_parent'; public const USER_ID = 'user_id'; - - /** - * @param string $author - * @return self - * @deprecated Remove in next version - * @see setCommentAuthor() - */ - public function setAuthor(string $author): self; - - /** - * @return string|null - * @deprecated Remove in next version - * @see getCommentAuthor() - */ - public function getAuthor(): ?string; - - /** - * @param string|null $email - * @return self - * @deprecated Remove in next version - * @see setCommentAuthorEmail() - */ - public function setAuthorEmail(?string $email): self; - - /** - * @return string|null - * @deprecated Remove in next version - * @see getCommentAuthorEmail() - */ - public function getAuthorEmail(): ?string; - - /** - * @param string|null $url - * @return self - * @deprecated Remove in next version - * @see setCommentAuthorUrl() - */ - public function setAuthorUrl(?string $url): self; - - /** - * @return string|null - * @deprecated Remove in next version - * @see getCommentAuthorUrl() - */ - public function getAuthorUrl(): ?string; - - /** - * @param string|null $ip - * @return self - * @deprecated Remove in next version - * @see setCommentAuthorIP() - */ - public function setAuthorIp(?string $ip): self; - - /** - * @return string|null - * @deprecated Remove in next version - * @see getCommentAuthorIP() - */ - public function getAuthorIp(): ?string; - - /** - * @param string|null $content - * @return self - * @deprecated Remove in next version - * @see setCommentContent() - */ - public function setContent(?string $content): self; - - /** - * @return string|null - * @deprecated Remove in next version - * @see gsetCommentContent() - */ - public function getContent(): ?string; - - /** - * @param int|null $karma - * @return self - * @deprecated Remove in next version - * @see setCommentKarma() - */ - public function setKarma(?int $karma): self; - - /** - * @return int|null - * @deprecated Remove in next version - * @see getCommentKarma() - */ - public function getKarma(): ?int; - - /** - * @param string|null $agent - * @return self - * @deprecated Remove in next version - * @see setCommentAgent() - */ - public function setAgent(?string $agent): self; - - /** - * @return string|null - * @deprecated Remove in next version - * @see getCommentAgent() - */ - public function getAgent(): ?string; - - /** - * @param string|null $type - * @return self - * @deprecated Remove in next version - * @see setCommentType() - */ - public function setType(?string $type): self; - - /** - * @return string|null - * @deprecated Remove in next version - * @see getCommentType() - */ - public function getType(): ?string; - - /** - * @param string|null $approved - * @return self - * @deprecated Remove in next version - * @see setCommentApproved() - */ - public function setApproved(?string $approved): self; - - /** - * @return string|null - * @deprecated Remove in next version - * @see getCommentApproved() - */ - public function getApproved(): ?string; - - /** - * @param mixed $date - * @return self - * @deprecated Remove in next version - * @see setCommentDate() - */ - public function setDate(mixed $date): self; - - /** - * @return Carbon|null - * @deprecated Remove in next version - * @see getCommentDate() - */ - public function getDate(): ?Carbon; - - /** - * @param mixed $date - * @return self - * @deprecated Remove in next version - * @see setCommentDateGmt() - */ - public function setDateGMT(mixed $date): self; - - /** - * @return Carbon|null - * @deprecated Remove in next version - * @see getCommentDateGmt() - */ - public function getDateGMT(): ?Carbon; } diff --git a/src/Api/OptionInterface.php b/src/Api/OptionInterface.php index f08bfad0..20593db0 100644 --- a/src/Api/OptionInterface.php +++ b/src/Api/OptionInterface.php @@ -8,17 +8,7 @@ namespace Dbout\WpOrm\Api; -use Dbout\WpOrm\Enums\YesNo; -use Dbout\WpOrm\Models\Option; - /** - * @method Option setOptionName(string $name) - * @method string getOptionName() - * @method Option setOptionValue($value) - * @method mixed getOptionValue() - * @method Option setAutoload(string|YesNo $autoload) - * @method string getAutoload() - * * @since 3.0.0 */ interface OptionInterface @@ -27,12 +17,4 @@ interface OptionInterface public const NAME = 'option_name'; public const VALUE = 'option_value'; public const AUTOLOAD = 'autoload'; - - /** - * Find one option by name - * - * @param string $optionName - * @return self|null - */ - public static function findOneByName(string $optionName): ?self; } diff --git a/src/Api/PostInterface.php b/src/Api/PostInterface.php index 15dcd18e..4dce8466 100644 --- a/src/Api/PostInterface.php +++ b/src/Api/PostInterface.php @@ -8,49 +8,7 @@ namespace Dbout\WpOrm\Api; -use Carbon\Carbon; -use Dbout\WpOrm\Models\Post; - /** - * @method Post setPostDate($date) - * @method Carbon|null getPostDate() - * @method Post setPostDateGMT($date) - * @method Carbon|null getPostDateGMT() - * @method Post setPostContent(?string $content) - * @method string|null getPostContent() - * @method Post setPostType(string $type) - * @method string|null getPostType() - * @method Post setGuid(?string $guid) - * @method string|null getGuid() - * @method Post setPostTitle(?string $title) - * @method string|null getPostTitle() - * @method Post setPostExcerpt(?string $excerpt) - * @method string|null getPostExcerpt() - * @method Post setPostStatus(?string $status) - * @method string|null getPostStatus() - * @method Post setCommentStatus(string $status) - * @method string|null getCommentStatus() - * @method Post setPingStatus(string $status) - * @method string|null getPingStatus() - * @method Post setPostPassword(?string $password) - * @method string|null getPostPassword() - * @method Post setPostName(?string $name) - * @method string|null getPostName() - * @method Post setToPing(?string $toPing) - * @method string|null getToPing() - * @method Post setPinged(?string $pinged) - * @method string|null getPinged() - * @method Post setPostModified($modified) - * @method Carbon|null getPostModified() - * @method Post setPostModifiedGMT($modified) - * @method Carbon|null getPostModifiedGMT() - * @method setPostMimeType(?string $mimeType) - * @method string|null getPostMimeType() - * @method Post setMenuOrder(?int $order) - * @method int|null getMenuOrder() - * @method Post setPostContentFiltered($content) - * @method string|null getPostContentFiltered() - * * @since 3.0.0 */ interface PostInterface @@ -78,12 +36,4 @@ interface PostInterface public const TYPE = 'post_type'; public const MIME_TYPE = 'post_mime_type'; public const COMMENT_COUNT = 'comment_count'; - - /** - * Find post by name - * - * @param string|null $name - * @return Post|null - */ - public function findOneByName(?string $name): ?Post; } diff --git a/src/Api/TermInterface.php b/src/Api/TermInterface.php index f8374a4b..a3b31dd6 100644 --- a/src/Api/TermInterface.php +++ b/src/Api/TermInterface.php @@ -8,16 +8,7 @@ namespace Dbout\WpOrm\Api; -use Dbout\WpOrm\Models\Term; - /** - * @method string|null getName() - * @method Term setName(?string $name); - * @method string|null getSlug() - * @method Term setSlug(?string $slug) - * @method int|null getTermGroup() - * @method Term setTermGroup(?int $group) - * * @since 3.0.0 */ interface TermInterface diff --git a/src/Api/TermRelationshipInterface.php b/src/Api/TermRelationshipInterface.php index 8162d3e8..451610c3 100644 --- a/src/Api/TermRelationshipInterface.php +++ b/src/Api/TermRelationshipInterface.php @@ -8,16 +8,7 @@ namespace Dbout\WpOrm\Api; -use Dbout\WpOrm\Models\TermRelationship; - /** - * @method int|null getTermOrder() - * @method TermRelationship setTermOrder(?int $order) - * @method int|null getTermTaxonomyId() - * @method TermRelationship setTermTaxonomyId(?int $id) - * @method int|null getObjectId() - * @method TermRelationship setObjectId(?int $id) - * * @since 3.0.0 */ interface TermRelationshipInterface diff --git a/src/Api/TermTaxonomyInterface.php b/src/Api/TermTaxonomyInterface.php index 731bae84..d66446b0 100644 --- a/src/Api/TermTaxonomyInterface.php +++ b/src/Api/TermTaxonomyInterface.php @@ -8,20 +8,7 @@ namespace Dbout\WpOrm\Api; -use Dbout\WpOrm\Models\TermTaxonomy; - /** - * @method int|null getTermId() - * @method TermTaxonomy setTermId(int $id) - * @method string getTaxonomy() - * @method TermTaxonomy setTaxonomy(string $taxonomy) - * @method string|null getDescription() - * @method TermTaxonomy setDescription(?string $description) - * @method int|null getParent() - * @method TermTaxonomy setParent($parent) - * @method int|null getCount() - * @method TermTaxonomy setCount(int $count) - * * @since 3.0.0 */ interface TermTaxonomyInterface diff --git a/src/Api/UserInterface.php b/src/Api/UserInterface.php index 9c005415..1029673d 100644 --- a/src/Api/UserInterface.php +++ b/src/Api/UserInterface.php @@ -8,29 +8,7 @@ namespace Dbout\WpOrm\Api; -use Carbon\Carbon; -use Dbout\WpOrm\Models\User; - /** - * @method string|null getUserLogin() - * @method User setUserLogin(string $login) - * @method string|null getUserPass() - * @method User setUserPass(string $password) - * @method string|null getUserNicename() - * @method User setUserNicename(string $nicename) - * @method string|null getUserEmail() - * @method User setUserEmail(string $email) - * @method string|null getUserUrl() - * @method User setUserUrl(?string $url) - * @method Carbon|null getUserRegistered() - * @method User setUserRegistered($date) - * @method string|null getUserActivationKey() - * @method User setUserActivationKey(?string $key) - * @method int getUserStatus() - * @method User setUserStatus(int $status) - * @method string|null getDisplayName() - * @method User setDisplayName(?string $name) - * * @since 3.0.0 */ interface UserInterface @@ -45,20 +23,4 @@ interface UserInterface public const ACTIVATION_KEY = 'user_activation_key'; public const DISPLAY_NAME = 'display_name'; public const STATUS = 'user_status'; - - /** - * Find user by email - * - * @param string $email - * @return self|null - */ - public static function findOneByEmail(string $email): ?self; - - /** - * Find user by login - * - * @param string $login - * @return self|null - */ - public static function findOneByLogin(string $login): ?self; } diff --git a/src/Builders/AbstractBuilder.php b/src/Builders/AbstractBuilder.php index acb01456..5ce4c223 100644 --- a/src/Builders/AbstractBuilder.php +++ b/src/Builders/AbstractBuilder.php @@ -8,7 +8,6 @@ namespace Dbout\WpOrm\Builders; -use Dbout\WpOrm\Api\PostInterface; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; @@ -64,14 +63,4 @@ protected function joined($query, $table): bool return false; } - - /** - * @param string $label - * @param string $key - * @return array - */ - public function toArrayOptions(string $label = PostInterface::TITLE, string $key = PostInterface::POST_ID): array - { - return $this->pluck($label, $key)->toArray(); - } } diff --git a/src/Builders/AbstractWithMetaBuilder.php b/src/Builders/AbstractWithMetaBuilder.php new file mode 100644 index 00000000..6c354909 --- /dev/null +++ b/src/Builders/AbstractWithMetaBuilder.php @@ -0,0 +1,174 @@ + + */ + +namespace Dbout\WpOrm\Builders; + +use Dbout\WpOrm\Attributes\MetaConfigAttribute; +use Dbout\WpOrm\Exceptions\MetaNotSupportedException; +use Dbout\WpOrm\Exceptions\WpOrmException; +use Dbout\WpOrm\Orm\AbstractModel; +use Illuminate\Database\Eloquent\Model; + +/** + * @since 3.0.0 + */ +abstract class AbstractWithMetaBuilder extends AbstractBuilder +{ + /** + * @var array + */ + protected array $joinCallback = [ + 'inner' => 'join', + 'left' => 'leftJoin', + 'right' => 'rightJoin', + ]; + + /** + * @var MetaConfigAttribute|null + */ + protected ?MetaConfigAttribute $metaConfig = null; + + /** + * @var string|null + */ + protected ?string $metaTable = null; + + /** + * @inheritDoc + * @throws \Exception + */ + public function setModel(Model $model): self + { + parent::setModel($model); + $this->initMeta(); + return $this; + } + + /** + * @param string $metaKey + * @param string|null $alias + * @throws WpOrmException + * @return $this + */ + public function addMetaToSelect(string $metaKey, ?string $alias = null): self + { + $this->joinToMeta($metaKey); + if ($alias === null || $alias === '') { + $alias = $metaKey; + } + + $column = sprintf('%s.%s AS %s', $metaKey, $this->metaConfig?->columnValue, $alias); + $this->addSelect($column); + return $this; + } + + /** + * @param array|array $metas + * @throws WpOrmException + * @return $this + */ + public function addMetasToSelect(array $metas): self + { + foreach ($metas as $key => $metaName) { + $alias = null; + if (is_string($key)) { + $alias = $key; + } + + $this->addMetaToSelect($metaName, $alias); + } + + return $this; + } + + /** + * @param string $metaKey + * @param mixed $value + * @param string $operator + * @throws WpOrmException + * @return $this + */ + public function addMetaToFilter(string $metaKey, mixed $value, string $operator = '='): self + { + $this + ->joinToMeta($metaKey) + ->where(sprintf('%s.%s', $metaKey, $this->metaConfig->columnValue), $operator, $value); + + return $this; + } + + /** + * @param string $metaKey + * @param string $joinType + * @throws WpOrmException + * @return $this + */ + public function joinToMeta(string $metaKey, string $joinType = 'inner'): self + { + $model = $this->model; + $joinTable = sprintf('%s AS %s', $this->metaTable, $metaKey); + + if ($this->joined($this, $joinTable)) { + return $this; + } + + $join = $this->joinCallback[$joinType] ?? null; + if ($join === null) { + throw new WpOrmException('Invalid join type.'); + } + + $this->$join($joinTable, function ($join) use ($metaKey, $model) { + /** @var \Illuminate\Database\Query\JoinClause $join */ + $join->on( + sprintf('%s.%s', $metaKey, $this->metaConfig?->columnKey), + '=', + $join->raw(sprintf("'%s'", $metaKey)) + )->on( + sprintf('%s.%s', $metaKey, $this->metaConfig?->foreignKey), + '=', + sprintf('%s.%s', $model->getTable(), $model->getKeyName()), + ); + }); + + return $this; + } + + /** + * @throws \ReflectionException + * @throws MetaNotSupportedException + * @throws WpOrmException + */ + protected function initMeta(): void + { + $traits = class_uses_recursive(get_class($this->model)); + if (!in_array(\Dbout\WpOrm\Concerns\HasMeta::class, $traits, true)) { + throw new MetaNotSupportedException(sprintf( + 'Model %s must be use trait %s', + get_class($this->model), + \Dbout\WpOrm\Concerns\HasMeta::class + )); + } + + // @phpstan-ignore-next-line + $config = $this->model->getMetaConfig(); + $reflection = new \ReflectionClass($config->metaClass); + if (!$reflection->isSubclassOf(AbstractModel::class)) { + throw new WpOrmException(sprintf( + 'Class %s must extend from %s.', + $config->metaClass, + AbstractModel::class + )); + } + + /** @var AbstractModel $metaModel */ + $metaModel = $reflection->newInstanceWithoutConstructor(); + + $this->metaTable = $metaModel->getTable(); + $this->metaConfig = $config; + } +} diff --git a/src/Builders/PostBuilder.php b/src/Builders/PostBuilder.php index 9b28fb06..44359958 100644 --- a/src/Builders/PostBuilder.php +++ b/src/Builders/PostBuilder.php @@ -9,14 +9,11 @@ namespace Dbout\WpOrm\Builders; use Dbout\WpOrm\Api\PostInterface; -use Dbout\WpOrm\Builders\Traits\WithMeta; use Dbout\WpOrm\Models\Post; use Illuminate\Database\Eloquent\Collection; -class PostBuilder extends AbstractBuilder +class PostBuilder extends AbstractWithMetaBuilder { - use WithMeta; - /** * @param string|null $name * @return Post|null diff --git a/src/Builders/TermBuilder.php b/src/Builders/TermBuilder.php index 2925ebd9..6dbc9881 100644 --- a/src/Builders/TermBuilder.php +++ b/src/Builders/TermBuilder.php @@ -8,7 +8,7 @@ namespace Dbout\WpOrm\Builders; -use Dbout\WpOrm\Models\TermTaxonomy; +use Dbout\WpOrm\Api\TermTaxonomyInterface; use Illuminate\Database\Eloquent\Collection; class TermBuilder extends AbstractBuilder @@ -20,7 +20,7 @@ class TermBuilder extends AbstractBuilder public function findAllByTaxonomy(string $taxonomy): Collection { return $this->whereHas('termTaxonomy', function ($query) use ($taxonomy) { - return $query->where(TermTaxonomy::TAXONOMY, $taxonomy); + return $query->where(TermTaxonomyInterface::TAXONOMY, $taxonomy); })->get(); } } diff --git a/src/Builders/Traits/WithMeta.php b/src/Builders/Traits/WithMeta.php deleted file mode 100644 index bc2b9dde..00000000 --- a/src/Builders/Traits/WithMeta.php +++ /dev/null @@ -1,139 +0,0 @@ - - */ - -namespace Dbout\WpOrm\Builders\Traits; - -use Dbout\WpOrm\Exceptions\MetaNotSupportedException; -use Dbout\WpOrm\Models\Meta\AbstractMeta; -use Dbout\WpOrm\Orm\AbstractModel; -use Illuminate\Database\Eloquent\Model; - -trait WithMeta -{ - /** - * @var array|string[] - */ - protected array $joinCallback = [ - 'inner' => 'join', - 'left' => 'leftJoin', - 'right' => 'rightJoin', - ]; - - /** - * @var AbstractMeta|null - */ - protected ?AbstractMeta $metaModel = null; - - /** - * @param string $metaKey - * @param string|null $alias - * @return $this - */ - public function addMetaToSelect(string $metaKey, ?string $alias = null): self - { - $this - ->joinToMeta($metaKey); - - if (!$alias) { - $alias = $metaKey; - } - - $column = sprintf('%s.%s AS %s', $metaKey, $this->metaModel->getValueColumn(), $alias); - $this->addSelect($column); - return $this; - } - - /** - * @param array $metas - * @return $this - */ - public function addMetasToSelect(array $metas): self - { - foreach ($metas as $key => $metaName) { - $alias = null; - if (is_string($key)) { - $alias = $key; - } - - $this->addMetaToSelect($metaName, $alias); - } - - return $this; - } - - /** - * @param string $metaKey - * @param $value - * @param string $operator - * @return $this - */ - public function addMetaToFilter(string $metaKey, $value, string $operator = '='): self - { - $this - ->joinToMeta($metaKey) - ->where(sprintf('%s.%s', $metaKey, $this->metaModel->getValueColumn()), $operator, $value); - - return $this; - } - - /** - * @inheritDoc - * @throws MetaNotSupportedException - * @throws \ReflectionException - */ - public function setModel(Model $model) - { - $traits = class_uses_recursive(get_class($model)); - if (!in_array(\Dbout\WpOrm\Models\Meta\WithMeta::class, $traits, true)) { - throw new MetaNotSupportedException(sprintf( - 'Model %s must be use trait %s', - get_class($model), - \Dbout\WpOrm\Models\Meta\WithMeta::class - )); - } - - /** @var \Dbout\WpOrm\Models\Meta\WithMeta $model */ - $metaClass = $model->getMetaClass(); - $object = (new \ReflectionClass($metaClass)); - $this->metaModel = $object->newInstanceWithoutConstructor(); - - return parent::setModel($model); - } - - /** - * @param string $metaKey - * @param string $joinType - * @return $this - */ - public function joinToMeta(string $metaKey, string $joinType = 'inner'): self - { - /** @var AbstractModel $model */ - $model = $this->model; - $joinTable = sprintf('%s AS %s', $this->metaModel->getTable(), $metaKey); - - if ($this->joined($this, $joinTable)) { - return $this; - } - - $join = $this->joinCallback[$joinType]; - $this->$join($joinTable, function ($join) use ($metaKey, $model) { - /** @var \Illuminate\Database\Query\JoinClause $join */ - $join->on( - sprintf('%s.%s', $metaKey, $this->metaModel->getKeyColumn()), - '=', - $join->raw(sprintf("'%s'", $metaKey)) - )->on( - $join->raw(sprintf('%s.%s', $metaKey, $this->metaModel->getFkColumn())), - '=', - sprintf('%s.%s', $model->getTable(), $model->getKeyName()), - ); - }); - - return $this; - } -} diff --git a/src/Builders/UserBuilder.php b/src/Builders/UserBuilder.php index 42ca0b27..8a650adb 100644 --- a/src/Builders/UserBuilder.php +++ b/src/Builders/UserBuilder.php @@ -9,13 +9,10 @@ namespace Dbout\WpOrm\Builders; use Dbout\WpOrm\Api\UserInterface; -use Dbout\WpOrm\Builders\Traits\WithMeta; use Dbout\WpOrm\Models\User; -class UserBuilder extends AbstractBuilder +class UserBuilder extends AbstractWithMetaBuilder { - use WithMeta; - /** * @param string $email * @return User|null diff --git a/src/Models/Meta/WithMeta.php b/src/Concerns/HasMeta.php similarity index 90% rename from src/Models/Meta/WithMeta.php rename to src/Concerns/HasMeta.php index 02e92a6a..50ea2dde 100644 --- a/src/Models/Meta/WithMeta.php +++ b/src/Concerns/HasMeta.php @@ -6,13 +6,14 @@ * Author: Dimitri BOUTEILLE */ -namespace Dbout\WpOrm\Models\Meta; +namespace Dbout\WpOrm\Concerns; use Dbout\WpOrm\Attributes\MetaConfigAttribute; use Dbout\WpOrm\Exceptions\WpOrmException; +use Dbout\WpOrm\Models\Meta\AbstractMeta; use Illuminate\Database\Eloquent\Relations\HasMany; -trait WithMeta +trait HasMeta { /** * @var MetaConfigAttribute @@ -27,7 +28,7 @@ trait WithMeta /** * @return void */ - protected static function bootWithMeta(): void + protected static function bootHasMeta(): void { static::saved(function ($model) { $model->saveTmpMetas(); @@ -38,7 +39,7 @@ protected static function bootWithMeta(): void * @throws WpOrmException * @return void */ - public function initializeWithMeta(): void + public function initializeHasMeta(): void { $reflection = new \ReflectionClass(static::class); $configs = $reflection->getAttributes(MetaConfigAttribute::class); @@ -47,7 +48,7 @@ public function initializeWithMeta(): void } /** @var MetaConfigAttribute $config */ - $config = $configs[0]; + $config = $configs[0]; $this->metaConfig = $config; } @@ -151,4 +152,12 @@ protected function saveTmpMetas(): void $this->_tmpMetas = []; } + + /** + * @return MetaConfigAttribute|null + */ + public function getMetaConfig(): ?MetaConfigAttribute + { + return $this->metaConfig; + } } diff --git a/src/Models/Comment.php b/src/Models/Comment.php index 995dd5b7..5e0707f9 100644 --- a/src/Models/Comment.php +++ b/src/Models/Comment.php @@ -17,8 +17,36 @@ use Illuminate\Database\Eloquent\Relations\HasOne; /** + * @method Comment setCommentAuthor(?string $author) + * @method string|null getCommentAuthor() + * @method Comment setCommentAuthorEmail(?string $email) + * @method string|null getCommentAuthorEmail() + * @method Comment setCommentAuthorUrl(?string $url) + * @method string|null getCommentAuthorUrl() + * @method Comment setCommentAuthorIP(?string $ip) + * @method string|null getCommentAuthorIP() + * @method Comment setCommentContent(?string $content) + * @method string|null getCommentContent() + * @method Comment setCommentKarma(?int $karma) + * @method int|null getCommentKarma() + * @method Comment setCommentApproved(string $approved) + * @method string getCommentApproved() + * @method Comment setCommentAgent(?string $agent) + * @method string|null getCommentAgent() + * @method Comment setCommentType(?string $type) + * @method string|null getCommentType() + * @method Comment setUserId(?int $userId) + * @method int|null getUserId() + * @method Comment setCommentDate(mixed $date) + * @method Carbon|null getCommentDate() + * @method Comment setCommentDateGmt(mixed $date) + * @method Carbon|null getCommentDateGmt() * @method static static|null find(int $commentId) * @method static CommentBuilder query() + * + * @property-read User|null $user + * @property-read Post|null $post + * @property-read Comment|null $parent */ class Comment extends AbstractModel implements CommentInterface { @@ -35,11 +63,6 @@ class Comment extends AbstractModel implements CommentInterface */ protected $table = 'comments'; - /** - * @inheritDoc - */ - protected $guarded = []; - /** * @inheritDoc */ @@ -82,15 +105,20 @@ public function newEloquentBuilder($query): CommentBuilder } /** - * @inheritDoc + * @param string $author + * @return self + * @deprecated Remove in next version + * @see setCommentAuthor() */ - public function setAuthor(string $author): CommentInterface + public function setAuthor(string $author): self { return $this->setCommentAuthor($author); } /** - * @inheritDoc + * @return string|null + * @deprecated Remove in next version + * @see getCommentAuthor() */ public function getAuthor(): ?string { @@ -98,15 +126,20 @@ public function getAuthor(): ?string } /** - * @inheritDoc + * @param string|null $email + * @return self + * @deprecated Remove in next version + * @see setCommentAuthorEmail() */ - public function setAuthorEmail(?string $email): CommentInterface + public function setAuthorEmail(?string $email): self { return $this->setCommentAuthorEmail($email); } /** - * @inheritDoc + * @return string|null + * @deprecated Remove in next version + * @see getCommentAuthorEmail() */ public function getAuthorEmail(): ?string { @@ -114,15 +147,20 @@ public function getAuthorEmail(): ?string } /** - * @inheritDoc + * @param string|null $url + * @return self + * @deprecated Remove in next version + * @see setCommentAuthorUrl() */ - public function setAuthorUrl(?string $url): CommentInterface + public function setAuthorUrl(?string $url): self { return $this->setCommentAuthorUrl($url); } /** - * @inheritDoc + * @return string|null + * @deprecated Remove in next version + * @see getCommentAuthorUrl() */ public function getAuthorUrl(): ?string { @@ -130,15 +168,20 @@ public function getAuthorUrl(): ?string } /** - * @inheritDoc + * @param string|null $ip + * @return self + * @deprecated Remove in next version + * @see setCommentAuthorIP() */ - public function setAuthorIp(?string $ip): CommentInterface + public function setAuthorIp(?string $ip): self { return $this->setCommentAuthorIP($ip); } /** - * @inheritDoc + * @return string|null + * @deprecated Remove in next version + * @see getCommentAuthorIP() */ public function getAuthorIp(): ?string { @@ -146,15 +189,20 @@ public function getAuthorIp(): ?string } /** - * @inheritDoc + * @param string|null $content + * @return self + * @deprecated Remove in next version + * @see setCommentContent() */ - public function setContent(?string $content): CommentInterface + public function setContent(?string $content): self { return $this->setCommentContent($content); } /** - * @inheritDoc + * @return string|null + * @deprecated Remove in next version + * @see gsetCommentContent() */ public function getContent(): ?string { @@ -162,15 +210,20 @@ public function getContent(): ?string } /** - * @inheritDoc + * @param int|null $karma + * @return self + * @deprecated Remove in next version + * @see setCommentKarma() */ - public function setKarma(?int $karma): CommentInterface + public function setKarma(?int $karma): self { return $this->setCommentKarma($karma); } /** * @inheritDoc + * @deprecated Remove in next version + * @see getCommentKarma() */ public function getKarma(): ?int { @@ -178,15 +231,20 @@ public function getKarma(): ?int } /** - * @inheritDoc + * @param string|null $agent + * @return self + * @deprecated Remove in next version + * @see setCommentAgent() */ - public function setAgent(?string $agent): CommentInterface + public function setAgent(?string $agent): self { return $this->setCommentAgent($agent); } /** - * @inheritDoc + * @return string|null + * @deprecated Remove in next version + * @see getCommentAgent() */ public function getAgent(): ?string { @@ -194,15 +252,20 @@ public function getAgent(): ?string } /** - * @inheritDoc + * @param string|null $type + * @return self + * @deprecated Remove in next version + * @see setCommentType() */ - public function setType(?string $type): CommentInterface + public function setType(?string $type): self { return $this->setCommentType($type); } /** - * @inheritDoc + * @return string|null + * @deprecated Remove in next version + * @see getCommentType() */ public function getType(): ?string { @@ -210,15 +273,20 @@ public function getType(): ?string } /** - * @inheritDoc + * @param string|null $approved + * @return self + * @deprecated Remove in next version + * @see setCommentApproved() */ - public function setApproved(?string $approved): CommentInterface + public function setApproved(?string $approved): self { return $this->setCommentApproved($approved); } /** - * @inheritDoc + * @return string|null + * @deprecated Remove in next version + * @see getCommentApproved() */ public function getApproved(): ?string { @@ -226,15 +294,20 @@ public function getApproved(): ?string } /** - * @inheritDoc + * @param mixed $date + * @return self + * @deprecated Remove in next version + * @see setCommentDate() */ - public function setDate(mixed $date): CommentInterface + public function setDate(mixed $date): self { return $this->setCommentDate($date); } /** - * @inheritDoc + * @return Carbon|null + * @deprecated Remove in next version + * @see getCommentDate() */ public function getDate(): ?Carbon { @@ -242,15 +315,20 @@ public function getDate(): ?Carbon } /** - * @inheritDoc + * @param mixed $date + * @return self + * @deprecated Remove in next version + * @see setCommentDateGmt() */ - public function setDateGMT(mixed $date): CommentInterface + public function setDateGMT(mixed $date): self { return $this->setCommentDateGmt($date); } /** - * @inheritDoc + * @return Carbon|null + * @deprecated Remove in next version + * @see getCommentDateGmt() */ public function getDateGMT(): ?Carbon { diff --git a/src/Models/Option.php b/src/Models/Option.php index be2ba8a1..efaf4bbf 100644 --- a/src/Models/Option.php +++ b/src/Models/Option.php @@ -10,9 +10,16 @@ use Dbout\WpOrm\Api\OptionInterface; use Dbout\WpOrm\Builders\OptionBuilder; +use Dbout\WpOrm\Enums\YesNo; use Dbout\WpOrm\Orm\AbstractModel; /** + * @method Option setOptionName(string $name) + * @method string getOptionName() + * @method Option setOptionValue($value) + * @method mixed getOptionValue() + * @method Option setAutoload(string|YesNo $autoload) + * @method string getAutoload() * @method static static|null find($optionId) * @method static OptionBuilder query() */ @@ -33,11 +40,6 @@ class Option extends AbstractModel implements OptionInterface */ public $timestamps = false; - /** - * @inheritDoc - */ - protected $guarded = []; - /** * @inheritDoc */ @@ -47,7 +49,8 @@ public function newEloquentBuilder($query): OptionBuilder } /** - * @inheritDoc + * @param string $optionName + * @return self|null */ public static function findOneByName(string $optionName): ?self { diff --git a/src/Models/Post.php b/src/Models/Post.php index d343465b..7e88a667 100644 --- a/src/Models/Post.php +++ b/src/Models/Post.php @@ -8,19 +8,59 @@ namespace Dbout\WpOrm\Models; +use Carbon\Carbon; use Dbout\WpOrm\Api\CommentInterface; use Dbout\WpOrm\Api\PostInterface; use Dbout\WpOrm\Api\UserInterface; use Dbout\WpOrm\Builders\PostBuilder; +use Dbout\WpOrm\Concerns\HasMeta; use Dbout\WpOrm\Models\Meta\PostMeta; -use Dbout\WpOrm\Models\Meta\WithMeta; use Dbout\WpOrm\Orm\AbstractModel; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; /** + * @method Post setPostDate($date) + * @method Carbon|null getPostDate() + * @method Post setPostDateGMT($date) + * @method Carbon|null getPostDateGMT() + * @method Post setPostContent(?string $content) + * @method string|null getPostContent() + * @method Post setPostType(string $type) + * @method string|null getPostType() + * @method Post setGuid(?string $guid) + * @method string|null getGuid() + * @method Post setPostTitle(?string $title) + * @method string|null getPostTitle() + * @method Post setPostExcerpt(?string $excerpt) + * @method string|null getPostExcerpt() + * @method Post setPostStatus(?string $status) + * @method string|null getPostStatus() + * @method Post setCommentStatus(string $status) + * @method string|null getCommentStatus() + * @method Post setPingStatus(string $status) + * @method string|null getPingStatus() + * @method Post setPostPassword(?string $password) + * @method string|null getPostPassword() + * @method Post setPostName(?string $name) + * @method string|null getPostName() + * @method Post setToPing(?string $toPing) + * @method string|null getToPing() + * @method Post setPinged(?string $pinged) + * @method string|null getPinged() + * @method Post setPostModified($modified) + * @method Carbon|null getPostModified() + * @method Post setPostModifiedGMT($modified) + * @method Carbon|null getPostModifiedGMT() + * @method setPostMimeType(?string $mimeType) + * @method string|null getPostMimeType() + * @method Post setMenuOrder(?int $order) + * @method int|null getMenuOrder() + * @method Post setPostContentFiltered($content) + * @method string|null getPostContentFiltered() * @method static static find(int $postId) * @method static PostBuilder query() + * * @property-read User|null $author * @property-read PostMeta[] $metas * @property-read static|null $parent @@ -29,7 +69,7 @@ #[\Dbout\WpOrm\Attributes\MetaConfigAttribute(PostMeta::class, PostMeta::POST_ID)] class Post extends AbstractModel implements PostInterface { - use WithMeta; + use HasMeta; public const UPDATED_AT = self::MODIFIED; public const CREATED_AT = self::DATE; @@ -39,11 +79,6 @@ class Post extends AbstractModel implements PostInterface */ protected $primaryKey = self::POST_ID; - /** - * @inheritDoc - */ - protected $guarded = []; - /** * @inheritDoc */ @@ -94,7 +129,8 @@ public function newEloquentBuilder($query): PostBuilder } /** - * @inheritDoc + * @param string|null $name + * @return Post|null */ public function findOneByName(?string $name): ?Post { diff --git a/src/Models/Term.php b/src/Models/Term.php index a9a2f011..7801ecb7 100644 --- a/src/Models/Term.php +++ b/src/Models/Term.php @@ -15,8 +15,15 @@ use Illuminate\Database\Eloquent\Relations\HasOne; /** + * @method string|null getName() + * @method Term setName(?string $name); + * @method string|null getSlug() + * @method Term setSlug(?string $slug) + * @method int|null getTermGroup() + * @method Term setTermGroup(?int $group) * @method static static|null find(int $termId) * @method static TermBuilder query() + * * @property-read TermTaxonomy|null $termTaxonomy */ class Term extends AbstractModel implements TermInterface @@ -43,11 +50,6 @@ class Term extends AbstractModel implements TermInterface self::TERM_GROUP => 'integer', ]; - /** - * @inheritDoc - */ - protected $guarded = []; - /** * @return HasOne */ diff --git a/src/Models/TermRelationship.php b/src/Models/TermRelationship.php index d38af123..63f8b845 100644 --- a/src/Models/TermRelationship.php +++ b/src/Models/TermRelationship.php @@ -11,6 +11,14 @@ use Dbout\WpOrm\Api\TermRelationshipInterface; use Dbout\WpOrm\Orm\AbstractModel; +/** + * @method int|null getTermOrder() + * @method TermRelationship setTermOrder(?int $order) + * @method int|null getTermTaxonomyId() + * @method TermRelationship setTermTaxonomyId(?int $id) + * @method int|null getObjectId() + * @method TermRelationship setObjectId(?int $id) + */ class TermRelationship extends AbstractModel implements TermRelationshipInterface { /** diff --git a/src/Models/TermTaxonomy.php b/src/Models/TermTaxonomy.php index 365d9d03..6bb76680 100644 --- a/src/Models/TermTaxonomy.php +++ b/src/Models/TermTaxonomy.php @@ -12,6 +12,16 @@ use Dbout\WpOrm\Orm\AbstractModel; /** + * @method int|null getTermId() + * @method TermTaxonomy setTermId(int $id) + * @method string getTaxonomy() + * @method TermTaxonomy setTaxonomy(string $taxonomy) + * @method string|null getDescription() + * @method TermTaxonomy setDescription(?string $description) + * @method int|null getParent() + * @method TermTaxonomy setParent($parent) + * @method int|null getCount() + * @method TermTaxonomy setCount(int $count) * @method static static|null find(int $id) */ class TermTaxonomy extends AbstractModel implements TermTaxonomyInterface diff --git a/src/Models/User.php b/src/Models/User.php index 1c71360d..5871da3c 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -8,18 +8,38 @@ namespace Dbout\WpOrm\Models; +use Carbon\Carbon; use Dbout\WpOrm\Api\CommentInterface; use Dbout\WpOrm\Api\PostInterface; use Dbout\WpOrm\Api\UserInterface; use Dbout\WpOrm\Builders\UserBuilder; +use Dbout\WpOrm\Concerns\HasMeta; use Dbout\WpOrm\Models\Meta\UserMeta; -use Dbout\WpOrm\Models\Meta\WithMeta; use Dbout\WpOrm\Orm\AbstractModel; use Illuminate\Database\Eloquent\Relations\HasMany; /** + * @method string|null getUserLogin() + * @method User setUserLogin(string $login) + * @method string|null getUserPass() + * @method User setUserPass(string $password) + * @method string|null getUserNicename() + * @method User setUserNicename(string $nicename) + * @method string|null getUserEmail() + * @method User setUserEmail(string $email) + * @method string|null getUserUrl() + * @method User setUserUrl(?string $url) + * @method Carbon|null getUserRegistered() + * @method User setUserRegistered($date) + * @method string|null getUserActivationKey() + * @method User setUserActivationKey(?string $key) + * @method int getUserStatus() + * @method User setUserStatus(int $status) + * @method string|null getDisplayName() + * @method User setDisplayName(?string $name) * @method static static|null find($userId) * @method static UserBuilder query() + * * @property-read UserMeta[] $metas * @property-read Comment[] $comments * @property-read Post[] $posts @@ -27,10 +47,10 @@ #[\Dbout\WpOrm\Attributes\MetaConfigAttribute(UserMeta::class, UserMeta::USER_ID)] class User extends AbstractModel implements UserInterface { - use WithMeta; + use HasMeta; - public const CREATED_AT = self::REGISTERED; - public const UPDATED_AT = null; + final public const CREATED_AT = self::REGISTERED; + final public const UPDATED_AT = null; /** * @inheritDoc @@ -50,11 +70,6 @@ class User extends AbstractModel implements UserInterface */ protected $primaryKey = self::USER_ID; - /** - * @inheritDoc - */ - protected $guarded = []; - /** * @return HasMany */ @@ -80,7 +95,8 @@ public function newEloquentBuilder($query): UserBuilder } /** - * @inheritDoc + * @param string $email + * @return self|null */ public static function findOneByEmail(string $email): ?self { @@ -90,7 +106,8 @@ public static function findOneByEmail(string $email): ?self } /** - * @inheritDoc + * @param string $login + * @return self|null */ public static function findOneByLogin(string $login): ?self { diff --git a/src/Orm/AbstractModel.php b/src/Orm/AbstractModel.php index eaa47ec2..62bcf1d3 100644 --- a/src/Orm/AbstractModel.php +++ b/src/Orm/AbstractModel.php @@ -14,7 +14,11 @@ abstract class AbstractModel extends Model { /** - * AbstractModel constructor. + * @inheritDoc + */ + protected $guarded = []; + + /** * @param array $attributes */ public function __construct(array $attributes = []) @@ -41,6 +45,7 @@ protected function newBaseQueryBuilder() */ public function getConnection() { + // @phpstan-ignore-next-line return Database::getInstance(); } diff --git a/src/Orm/Database.php b/src/Orm/Database.php index f5dec80c..6b685f43 100644 --- a/src/Orm/Database.php +++ b/src/Orm/Database.php @@ -82,7 +82,7 @@ public function __construct() } /** - * @return mixed|string + * @inheritDoc */ public function getDatabaseName() { @@ -106,9 +106,7 @@ public function getTablePrefix(): ?string } /** - * @param \Closure|\Illuminate\Database\Query\Builder|string $table - * @param null $as - * @return Builder|\Illuminate\Database\Query\Builder + * @inheritDoc */ public function table($table, $as = null) { @@ -120,11 +118,7 @@ public function table($table, $as = null) } /** - * Get a new raw query expression. - * - * @param mixed $value - * - * @return \Illuminate\Database\Query\Expression + * @inheritDoc */ public function raw($value) { @@ -146,38 +140,28 @@ public function query() } /** - * Run a select statement and return a single result - * - * @param string $query - * @param array $bindings - * @param bool $useReadPdo - * @return array|mixed|object|void|null + * @inheritDoc */ public function selectOne($query, $bindings = [], $useReadPdo = true) { $query = $this->bind_params($query, $bindings); $result = $this->db->get_row($query); if ($result === false || $this->db->last_error) { - throw new QueryException($query, $bindings, new \Exception($this->db->last_error)); + throw new QueryException($this->getName(), $query, $bindings, new \Exception($this->db->last_error)); } return $result; } /** - * Run a select statement - * - * @param string $query - * @param array $bindings - * @param bool $useReadPdo - * @return array|object|null + * @inheritDoc */ public function select($query, $bindings = [], $useReadPdo = true) { $query = $this->bind_params($query, $bindings); $result = $this->db->get_results($query); if ($result === false || $this->db->last_error) { - throw new QueryException($query, $bindings, new \Exception($this->db->last_error)); + throw new QueryException($this->getName(), $query, $bindings, new \Exception($this->db->last_error)); } return $result; @@ -297,19 +281,18 @@ public function statement($query, $bindings = []) */ public function affectingStatement($query, $bindings = []) { - $new_query = $this->bind_params($query, $bindings, true); - $result = $this->db->query($new_query); + $newQuery = $this->bind_params($query, $bindings, true); + $result = $this->db->query($newQuery); if ($result === false || $this->db->last_error) { - throw new QueryException($new_query, $bindings, new \Exception($this->db->last_error)); + throw new QueryException($this->getName(), $newQuery, $bindings, new \Exception($this->db->last_error)); } return (int) $result; } /** - * @param string $query - * @return bool + * @inheritDoc */ public function unprepared($query) { @@ -318,8 +301,7 @@ public function unprepared($query) } /** - * @param array $bindings - * @return array + * @inheritDoc */ public function prepareBindings(array $bindings) { @@ -343,10 +325,7 @@ public function prepareBindings(array $bindings) } /** - * @param \Closure $callback - * @param int $attempts - * @throws \Exception - * @return mixed + * @inheritDoc */ public function transaction(\Closure $callback, $attempts = 1) { @@ -362,9 +341,7 @@ public function transaction(\Closure $callback, $attempts = 1) } /** - * Start a new database transaction. - * - * @return void + * @inheritDoc */ public function beginTransaction() { @@ -375,9 +352,7 @@ public function beginTransaction() } /** - * Commit the active database transaction. - * - * @return void + * @inheritDoc */ public function commit() { @@ -391,9 +366,7 @@ public function commit() } /** - * Rollback the active database transaction. - * - * @return void + * @inheritDoc */ public function rollBack() { @@ -407,9 +380,7 @@ public function rollBack() } /** - * Get the number of active transactions. - * - * @return int + * @inheritDoc */ public function transactionLevel() { @@ -417,11 +388,7 @@ public function transactionLevel() } /** - * Execute the given callback in "dry run" mode. - * - * @param \Closure $callback - * - * @return array + * @inheritDoc */ public function pretend(\Closure $callback) { diff --git a/src/Taps/Comment/IsCommentTypeTap.php b/src/Taps/Comment/IsCommentTypeTap.php new file mode 100644 index 00000000..d55af95c --- /dev/null +++ b/src/Taps/Comment/IsCommentTypeTap.php @@ -0,0 +1,35 @@ + + */ + +namespace Dbout\WpOrm\Taps\Comment; + +use Dbout\WpOrm\Api\CommentInterface; +use Dbout\WpOrm\Builders\CommentBuilder; + +/** + * @since 3.0.0 + */ +class IsCommentTypeTap +{ + /** + * @param string $commentType + */ + public function __construct( + protected readonly string $commentType + ) { + } + + /** + * @param CommentBuilder $builder + * @return void + */ + public function __invoke(CommentBuilder $builder): void + { + $builder->where(CommentInterface::TYPE, $this->commentType); + } +}