Skip to content

Commit

Permalink
PHpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
ilestis committed Jul 7, 2024
1 parent 9400530 commit 6c1eb35
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 14 deletions.
1 change: 0 additions & 1 deletion app/Http/Controllers/CampaignBoostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ public function confirm(CampaignBoost $campaignBoost)
}

/**
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function destroy(CampaignBoost $campaignBoost)
Expand Down
1 change: 1 addition & 0 deletions app/Models/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public function mappedPreview(): string
if (!$this->child->hasEntry()) {
return '';
}
// @phpstan-ignore-next-line
return Str::limit(strip_tags($this->child->parsedEntry()), 500);
}

Expand Down
1 change: 1 addition & 0 deletions app/Models/MiscModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @property int $id
* Still keep campaign_id for phpstan
* @property int $campaign_id
* @property string $entry
* @property string $name
* @property string $type
* @property string $slug
Expand Down
3 changes: 3 additions & 0 deletions app/Observers/EntryObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ public function saving(Model $model)
// When creating modules through the API, there might be no entry, which is why we need to
// check if they are in the attributes of the model before interacting with it;
$attributes = $model->getAttributes();
// @phpstan-ignore-next-line
if (!array_key_exists($model->entryFieldName(), $attributes)) {
return;
}
// @phpstan-ignore-next-line
$model->{$model->entryFieldName()} = $this->purify(Mentions::codify($model->{$model->entryFieldName()}));
}

public function saved(Model $model)
{
// @phpstan-ignore-next-line
if ($model->isDirty($model->entryFieldName())) {
if ($model instanceof MiscModel) {
$this->entityMappingService->with($model->entity);
Expand Down
6 changes: 4 additions & 2 deletions app/Services/AttributeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,10 @@ public function replaceMentions(int $sourceId): self
$searchAttributes[] = '{attribute:' . $sourceAttributes[$slug] . '}';
$replaceAttributes[] = '{attribute:' . $attribute->id . '}';
}
$entry = Str::replace($searchAttributes, $replaceAttributes, $this->entity->child->entry);
$this->entity->child->update(['entry' => $entry]);
if ($this->entity->child->hasEntry()) {
$entry = Str::replace($searchAttributes, $replaceAttributes, $this->entity->child->entry);
$this->entity->child->update(['entry' => $entry]);
}
foreach ($this->entity->posts as $post) {
$post->entry = Str::replace($searchAttributes, $replaceAttributes, $post->entry);
$post->updateQuietly();
Expand Down
2 changes: 1 addition & 1 deletion app/Services/Campaign/Import/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ protected function secondCampaign(): self
$this->campaign->excerpt = $this->mentions($this->campaign->excerpt);
$this->campaign->save();

$this->entityMappingService->silent()->mapCampaign($this->campaign);
$this->entityMappingService->silent()->with($this->campaign)->map();
return $this;
}

Expand Down
3 changes: 1 addition & 2 deletions app/Services/Entity/PostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ public function handle(MovePostRequest $request): Post
protected function copy(): Post
{
$entity = Entity::findOrFail($this->entityId);
/** @var Post $newPost */
$newPost = $this->post->copyTo($entity);

// Update the "mentioned in" mapping for the entity
$this->mappingService->mapPost($newPost);
$this->mappingService->with($newPost)->map();

$this->log($newPost, EntityLog::ACTION_CREATE_POST);

Expand Down
31 changes: 23 additions & 8 deletions app/Services/EntityMappingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ protected function createNewMention(int $target): void
if ($this->model instanceof Campaign) {
$mention->campaign_id = $this->model->id;
} elseif ($this->model instanceof Post) {
$mention->post_id = $this->model->id;
$mention->entity_id = $this->model->entity_id;
$mention->post_id = $this->post()->id;
$mention->entity_id = $this->post()->entity_id;

// If we are making a reference to ourselves, no need to save it
if ($this->model->entity_id == $target) {
Expand All @@ -97,10 +97,11 @@ protected function createNewMention(int $target): void
return;
}
} else {
// @phpstan-ignore-next-line
$mention->entity_id = $this->model->id;

// If we are making a reference to ourselves, no need to save it
if ($this->model->id == $target) {
if ($this->model->id == $target) { // @phpstan-ignore-line
return;
}
}
Expand All @@ -118,9 +119,9 @@ protected function images(): self
}
$images = [];
if ($this->model instanceof Entity) {
$images = $this->extractImages($this->model->child->entry);
$images = $this->extractImages($this->entity()->child->entry);
} elseif ($this->model instanceof Post) {
$images = $this->extractImages($this->model->entry);
$images = $this->extractImages($this->post()->entry);
}
$existingTargets = [];
if ($this->model instanceof Entity) {
Expand All @@ -139,9 +140,9 @@ protected function images(): self

// Determine the real campaign id from the model.
if ($this->model instanceof Post) {
$campaignId = $this->model->entity->campaign_id;
$campaignId = $this->post()->entity->campaign_id;
} else {
$campaignId = $this->model->campaign_id;
$campaignId = $this->entity()->campaign_id;
}

/** @var Image|null $target */
Expand Down Expand Up @@ -179,15 +180,17 @@ protected function images(): self
protected function entry(): self
{
$existingTargets = [];
// @phpstan-ignore-next-line
foreach ($this->model->mentions as $map) {
$existingTargets[$map->target_id] = $map;
}
$createdMappings = 0;
$existingMappings = 0;

if ($this->model instanceof Entity) {
$mentions = $this->extract($this->model->child->entry);
$mentions = $this->extract($this->entity()->child->entry);
} else {
// @phpstan-ignore-next-line
$mentions = $this->extract($this->model->{$this->model->entryFieldName()});
}

Expand Down Expand Up @@ -255,6 +258,7 @@ protected function campaignID(): int
} elseif ($this->model instanceof QuestElement) {
return $this->model->quest->campaign_id;
}
// @phpstan-ignore-next-line
return $this->model->campaign_id;
}

Expand Down Expand Up @@ -289,4 +293,15 @@ protected function log(string $message = null)
}
}
}

protected function post(): Post
{
// @phpstan-ignore-next-line
return $this->model;
}
protected function entity(): Entity
{
// @phpstan-ignore-next-line
return $this->model;
}
}

0 comments on commit 6c1eb35

Please sign in to comment.