1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 08:24:04 +00:00

fix phpstan errors

This commit is contained in:
Samuel Štancl 2024-08-06 04:35:25 +02:00
parent 18a16e6a74
commit 4372e1bef2
20 changed files with 105 additions and 16 deletions

View file

@ -14,8 +14,11 @@ parameters:
- Illuminate\Database\Eloquent\Model
ignoreErrors:
-
identifier: missingType.iterableValue
- '#Cannot access offset (.*?) on Illuminate\\Contracts\\Foundation\\Application#'
- '#Cannot access offset (.*?) on Illuminate\\Contracts\\Config\\Repository#'
- '#Call to an undefined method(.*?)Tenant\:\:run\(\).#'
-
message: '#Call to an undefined (method|static method) Illuminate\\Database\\Eloquent\\(Model|Builder)#'
paths:
@ -23,7 +26,10 @@ parameters:
- src/Commands/ClearPendingTenants.php
- src/Database/Concerns/PendingScope.php
- src/Database/ParentModelScope.php
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Builder\:\:withPending\(\)#'
-
message: '#Call to an undefined method Illuminate\\Database\\Eloquent\\Builder<Illuminate\\Database\\Eloquent\\Model&Stancl\\Tenancy\\Contracts\\Tenant>\:\:withPending\(\)#'
paths:
- src/Concerns/HasTenantOptions.php
-
message: '#invalid type Laravel\\Telescope\\IncomingEntry#'
paths:
@ -53,6 +59,4 @@ parameters:
paths:
- src/RLS/PolicyManagers/TraitRLSManager.php
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false # later we may want to enable this
treatPhpDocTypesAsCertain: false

View file

@ -73,6 +73,9 @@ class CloneRoutesAsTenant
return $this;
}
/**
* @return Collection<int, Route>
*/
protected function getRoutesToClone(): Collection
{
$tenantParameterName = PathTenantResolver::tenantParameterName();

View file

@ -16,6 +16,9 @@ class CreateStorageSymlinksAction
{
use DealsWithTenantSymlinks;
/**
* @param Tenant|Collection<covariant int|string, Tenant&\Illuminate\Database\Eloquent\Model>|LazyCollection<covariant int|string, Tenant&\Illuminate\Database\Eloquent\Model> $tenants
*/
public function __invoke(Tenant|Collection|LazyCollection $tenants, bool $relativeLink = false, bool $force = false): void
{
$tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants;

View file

@ -15,6 +15,9 @@ class RemoveStorageSymlinksAction
{
use DealsWithTenantSymlinks;
/**
* @param Tenant|Collection<covariant int|string, Tenant&\Illuminate\Database\Eloquent\Model>|LazyCollection<covariant int|string, Tenant&\Illuminate\Database\Eloquent\Model> $tenants
*/
public function __invoke(Tenant|Collection|LazyCollection $tenants): void
{
$tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants;

View file

@ -42,6 +42,9 @@ class Link extends Command
return 0;
}
/**
* @param LazyCollection<covariant int|string, \Stancl\Tenancy\Contracts\Tenant&\Illuminate\Database\Eloquent\Model> $tenants
*/
protected function removeLinks(LazyCollection $tenants): void
{
(new RemoveStorageSymlinksAction)($tenants);
@ -49,6 +52,9 @@ class Link extends Command
$this->components->info('The links have been removed.');
}
/**
* @param LazyCollection<covariant int|string, \Stancl\Tenancy\Contracts\Tenant&\Illuminate\Database\Eloquent\Model> $tenants
*/
protected function createLinks(LazyCollection $tenants): void
{
(new CreateStorageSymlinksAction)(

View file

@ -52,20 +52,23 @@ class Migrate extends MigrateCommand
if ($this->getProcesses() > 1) {
return $this->runConcurrently($this->getTenantChunks()->map(function ($chunk) {
return $this->getTenants(array_values($chunk->all()));
return $this->getTenants($chunk->all());
}));
}
return $this->migrateTenants($this->getTenants()) ? 0 : 1;
}
protected function childHandle(...$args): bool
protected function childHandle(mixed ...$args): bool
{
$chunk = $args[0];
return $this->migrateTenants($chunk);
}
/**
* @param LazyCollection<covariant int|string, \Stancl\Tenancy\Contracts\Tenant&\Illuminate\Database\Eloquent\Model> $tenants
*/
protected function migrateTenants(LazyCollection $tenants): bool
{
$success = true;

View file

@ -38,14 +38,18 @@ class MigrateFresh extends BaseCommand
if ($this->getProcesses() > 1) {
return $this->runConcurrently($this->getTenantChunks()->map(function ($chunk) {
return $this->getTenants(array_values($chunk->all()));
return $this->getTenants($chunk->all());
}));
}
tenancy()->runForMultiple($this->getTenants(), function ($tenant) use (&$success) {
$this->components->info("Tenant: {$tenant->getTenantKey()}");
$this->components->task('Dropping tables', fn () => $success = $success && $this->wipeDB());
$this->components->task('Migrating', fn () => $success = $success && $this->migrateTenant($tenant));
$this->components->task('Dropping tables', function () use (&$success) {
$success = $success && $this->wipeDB();
});
$this->components->task('Migrating', function () use ($tenant, &$success) {
$success = $success && $this->migrateTenant($tenant);
});
});
return $success ? 0 : 1;
@ -69,13 +73,16 @@ class MigrateFresh extends BaseCommand
]) === 0;
}
protected function childHandle(...$args): bool
protected function childHandle(mixed ...$args): bool
{
$chunk = $args[0];
return $this->migrateFreshTenants($chunk);
}
/**
* @param LazyCollection<covariant int|string, \Stancl\Tenancy\Contracts\Tenant&\Illuminate\Database\Eloquent\Model> $tenants
*/
protected function migrateFreshTenants(LazyCollection $tenants): bool
{
$success = true;

View file

@ -42,7 +42,7 @@ class Rollback extends RollbackCommand
if ($this->getProcesses() > 1) {
return $this->runConcurrently($this->getTenantChunks()->map(function ($chunk) {
return $this->getTenants(array_values($chunk->all()));
return $this->getTenants($chunk->all());
}));
}
@ -54,13 +54,16 @@ class Rollback extends RollbackCommand
return 'tenants:rollback';
}
protected function childHandle(...$args): bool
protected function childHandle(mixed ...$args): bool
{
$chunk = $args[0];
return $this->rollbackTenants($chunk);
}
/**
* @param LazyCollection<covariant int|string, \Stancl\Tenancy\Contracts\Tenant&\Illuminate\Database\Eloquent\Model> $tenants
*/
protected function rollbackTenants(LazyCollection $tenants): bool
{
$success = true;

View file

@ -41,6 +41,9 @@ class TenantList extends Command
}
/** Generate the visual CLI output for the domain names. */
/**
* @param Collection<int|string, string>|null $domains
*/
protected function domainsCLI(?Collection $domains): ?string
{
if (! $domains) {

View file

@ -22,11 +22,17 @@ trait HasTenantOptions
], parent::getOptions());
}
/**
* @return LazyCollection<int, \Stancl\Tenancy\Contracts\Tenant&\Illuminate\Database\Eloquent\Model>
*/
protected function getTenants(?array $tenantKeys = null): LazyCollection
{
return $this->getTenantsQuery($tenantKeys)->cursor();
}
/**
* @return Builder<\Stancl\Tenancy\Contracts\Tenant&\Illuminate\Database\Eloquent\Model>
*/
protected function getTenantsQuery(?array $tenantKeys = null): Builder
{
return tenancy()->query()

View file

@ -13,14 +13,14 @@ trait ParallelCommand
{
public const MAX_PROCESSES = 24;
abstract protected function childHandle(...$args): bool;
abstract protected function childHandle(mixed ...$args): bool;
public function addProcessesOption(): void
{
$this->addOption('processes', 'p', InputOption::VALUE_OPTIONAL, 'How many processes to spawn. Maximum value: ' . static::MAX_PROCESSES . ', recommended value: core count', 1);
}
protected function forkProcess(...$args): int
protected function forkProcess(mixed ...$args): int
{
$pid = pcntl_fork();
@ -53,14 +53,25 @@ trait ParallelCommand
return $processes;
}
/**
* @return Collection<int, Collection<int, \Stancl\Tenancy\Contracts\Tenant&\Illuminate\Database\Eloquent\Model>>>
*/
protected function getTenantChunks(): Collection
{
$idCol = tenancy()->model()->getTenantKeyName();
$tenants = tenancy()->model()->orderBy($idCol, 'asc')->pluck($idCol);
return $tenants->chunk(ceil($tenants->count() / $this->getProcesses()));
return $tenants->chunk((int) ceil($tenants->count() / $this->getProcesses()))->map(function ($chunk) {
$chunk = array_values($chunk->all());
/** @var Collection<int, \Stancl\Tenancy\Contracts\Tenant&\Illuminate\Database\Eloquent\Model> $chunk */
return $chunk;
});
}
/**
* @param array|ArrayAccess<int, mixed>|null $args
*/
protected function runConcurrently(array|ArrayAccess|null $args = null): int
{
$processes = $this->getProcesses();

View file

@ -5,15 +5,19 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Contracts;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use \Illuminate\Database\Eloquent\Model;
/**
* @property-read Tenant $tenant
*
* @see \Stancl\Tenancy\Database\Models\Domain
*
* @mixin \Illuminate\Database\Eloquent\Model
* @mixin Model
*/
interface Domain
{
/**
* @return BelongsTo<Tenant&Model, Model>
*/
public function tenant(): BelongsTo;
}

View file

@ -20,6 +20,8 @@ class PendingScope implements Scope
/**
* Apply the scope to a given Eloquent query builder.
*
* @param Builder<\Stancl\Tenancy\Contracts\Tenant&Model> $builder
*
* @return void
*/
public function apply(Builder $builder, Model $model)
@ -32,6 +34,8 @@ class PendingScope implements Scope
/**
* Extend the query builder with the needed functions.
*
* @param Builder<\Stancl\Tenancy\Contracts\Tenant&Model> $builder
*
* @return void
*/
public function extend(Builder $builder)
@ -43,6 +47,8 @@ class PendingScope implements Scope
/**
* Add the with-pending extension to the builder.
*
* @param Builder<\Stancl\Tenancy\Contracts\Tenant&Model> $builder
*
* @return void
*/
protected function addWithPending(Builder $builder)
@ -59,6 +65,8 @@ class PendingScope implements Scope
/**
* Add the without-pending extension to the builder.
*
* @param Builder<\Stancl\Tenancy\Contracts\Tenant&Model> $builder
*
* @return void
*/
protected function addWithoutPending(Builder $builder)
@ -75,6 +83,8 @@ class PendingScope implements Scope
/**
* Add the only-pending extension to the builder.
*
* @param Builder<\Stancl\Tenancy\Contracts\Tenant&Model> $builder
*
* @return void
*/
protected function addOnlyPending(Builder $builder)

View file

@ -10,6 +10,9 @@ use Illuminate\Database\Eloquent\Scope;
class ParentModelScope implements Scope
{
/**
* @param Builder<Model> $builder
*/
public function apply(Builder $builder, Model $model): void
{
if (! tenancy()->initialized) {
@ -19,6 +22,9 @@ class ParentModelScope implements Scope
$builder->whereHas($builder->getModel()->getRelationshipToPrimaryModel());
}
/**
* @param Builder<Model> $builder
*/
public function extend(Builder $builder): void
{
$builder->macro('withoutParentModel', function (Builder $builder) {

View file

@ -14,6 +14,8 @@ use Stancl\Tenancy\Contracts\Tenant;
* @method Tenant[] toArray()
* @method Tenant offsetGet($key)
* @method Tenant first()
*
* @extends Collection<int|string, Tenant&\Illuminate\Database\Eloquent\Model>
*/
class TenantCollection extends Collection
{

View file

@ -11,6 +11,9 @@ use Stancl\Tenancy\Tenancy;
class TenantScope implements Scope
{
/**
* @param Builder<Model> $builder
*/
public function apply(Builder $builder, Model $model)
{
if (! tenancy()->initialized) {
@ -20,6 +23,9 @@ class TenantScope implements Scope
$builder->where($model->qualifyColumn(Tenancy::tenantKeyColumn()), tenant()->getTenantKey());
}
/**
* @param Builder<Model> $builder
*/
public function extend(Builder $builder): void
{
$builder->macro('withoutTenancy', function (Builder $builder) {

View file

@ -64,7 +64,7 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
} elseif (static::$cookie && $request->hasCookie(static::$cookie)) {
$payload = $request->cookie(static::$cookie);
if ($payload) {
if ($payload && is_string($payload)) {
$payload = $this->getTenantFromCookie($payload);
}
} else {

View file

@ -72,6 +72,9 @@ class TraitRLSManager implements RLSPolicyManager
SQL;
}
/**
* @param BelongsTo<Model, Model> $parentRelationship
*/
protected function generateIndirectRLSPolicyQuery(Model $model, BelongsTo $parentRelationship): string
{
$table = $model->getTable();

View file

@ -16,6 +16,9 @@ use Stancl\Tenancy\Contracts\Tenant;
*/
interface SyncMaster extends Syncable
{
/**
* @return BelongsToMany<Tenant&Model>
*/
public function tenants(): BelongsToMany;
public function getTenantModelName(): string;

View file

@ -128,6 +128,9 @@ class Tenancy
return in_array($bootstrapper, static::getBootstrappers(), true);
}
/**
* @return Builder<Tenant&Model>
*/
public static function query(): Builder
{
return static::model()->query();