Skip to content

Commit

Permalink
Fix PHPStan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Feb 21, 2023
1 parent 533d034 commit c390f1e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ parameters:
strictCalls: false
switchConditionsMatchingType: true
noVariableVariables: false

stubFiles:
- tests/Static/phpstan/stubs/Model.php
8 changes: 7 additions & 1 deletion src/Behaviours/MakeTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@

trait MakeTag
{
public function makeModelName(Model $model): string|null
public function makeModelName(mixed $model): string|null
{
if (!$model instanceof Model) {
$model = json_encode($model);

return $model === false ? null : sha1($model);
}

try {
return method_exists($model, 'getCDNCacheTag')
? $model->getCDNCacheTag()
Expand Down
4 changes: 3 additions & 1 deletion src/Services/Invalidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public function absorbCloudFront(AwsResult $invalidation): self
return $this;
}

$statusCode = $invalidation['@metadata']['statusCode'];
$metadata = $invalidation['@metadata'];

$statusCode = is_array($metadata) ? $metadata['statusCode'] : 500;

$invalidation = Helpers::toArray($invalidation['Invalidation']);

Expand Down
25 changes: 14 additions & 11 deletions src/Services/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,20 @@ public function storeCacheTags(
}

public function dispatchInvalidationsForModel(Collection|string|Model $models): void {
Helpers::debug([
'dispatchInvalidationsForModel - models: ',
$models->map(fn(Model $model) => get_class($model)." ({$model->id})")->implode(', ')
]);

if (blank($models)) {
return;
}

if (is_string($models)) {
$this->dispatchInvalidationsForUpdatedModel($models);
$this->dispatchInvalidationsForUpdatedModel(collect($models));

return;
}

if ($models instanceof Model) {
$models = new Collection($models);
}

$models = $this->onlyValidModels($models);

$models = $this->notYetDispatched($models);
Expand All @@ -235,6 +234,12 @@ public function dispatchInvalidationsForModel(Collection|string|Model $models):
return;
}

Helpers::debug([
'dispatchInvalidationsForModel - models: ',
/** @phpstan-ignore-next-line */
$models->map(fn(Model $model) => get_class($model)." ({$model->id})")->implode(', ')
]);

/**
* @var Model $model
*/
Expand Down Expand Up @@ -280,6 +285,7 @@ public function dispatchInvalidationsForUpdatedModel(Collection $models): void {
'INVALIDATING tags for models: ' .
$models
->map(
/** @phpstan-ignore-next-line */
fn(Model|string $model) => $model instanceof Model
? $this->makeModelName($model)
: $model,
Expand Down Expand Up @@ -674,19 +680,16 @@ public function markUrlsAsWarmed(Collection $urls): void
$this->dbStatement($sql);
}

public function onlyValidModels($models)
public function onlyValidModels(Collection $models): Collection
{
$models =
$models instanceof Model ? collect([$models]) : collect($models);

return $models->filter(
fn($model) => $this->tagIsNotExcluded(
$model instanceof Model ? get_class($model) : $model,
),
);
}

public function notYetDispatched($models)
public function notYetDispatched(Collection $models): Collection
{
$tags = $models->mapWithKeys(
function ($model) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Static/phpstan/stubs/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Database\Eloquent;

/**
* @property int $id
*/
class Model
{
}

0 comments on commit c390f1e

Please sign in to comment.