Skip to content

Commit

Permalink
Composer update + phpstan annotations + Improved speed. (#2448)
Browse files Browse the repository at this point in the history
* Composer update: add all the required annotations for phpstan
* Remove some queries. (#2455)
* Remove middleware when we already passed the landing point (#2456)
* Add missing primary key in Jobs_history + option to disable feedback completely. (#2457)
* Wrap collections to avoid double queries (#2458)
  • Loading branch information
ildyria authored Jun 8, 2024
1 parent 0e1f7f8 commit 728853d
Show file tree
Hide file tree
Showing 228 changed files with 2,576 additions and 1,435 deletions.
6 changes: 5 additions & 1 deletion app/Actions/Album/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Archive extends Action
protected int $deflateLevel = -1;

/**
* @param Collection<AbstractAlbum> $albums
* @param Collection<int,AbstractAlbum> $albums
*
* @return StreamedResponse
*
Expand Down Expand Up @@ -101,6 +101,10 @@ public function do(Collection $albums): StreamedResponse

/**
* Create the title of the ZIP archive.
*
* @param Collection<int,AbstractAlbum> $albums
*
* @return string
*/
private static function createZipTitle(Collection $albums): string
{
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/Album/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function do(array $albumIDs): FileDeleter
// find all photos in those and their descendants
// Only load necessary attributes for tree; in particular avoid
// loading expensive `min_taken_at` and `max_taken_at`.
/** @var Collection<Album> $albums */
/** @var Collection<int,Album> $albums */
$albums = Album::query()
->without(['cover', 'thumb'])
->select(['id', 'parent_id', '_lft', '_rgt', 'track_short_path'])
Expand Down Expand Up @@ -182,7 +182,7 @@ public function do(array $albumIDs): FileDeleter
* The latter is more efficient, because we do not reload models
* from the DB.
*
* @param Collection<Album> $albums
* @param Collection<int,Album> $albums
*
* @return void
*
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/Album/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Merge extends Action
* Merges the content of the given source albums (photos and sub-albums)
* into the target.
*
* @param Album $targetAlbum
* @param Collection<Album> $albums
* @param Album $targetAlbum
* @param Collection<int,Album> $albums
*
* @throws ModelNotFoundException
* @throws ModelDBException
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/Album/Move.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Move extends Action
/**
* Moves the given albums into the target.
*
* @param Album|null $targetAlbum
* @param Collection<Album> $albums
* @param Album|null $targetAlbum
* @param Collection<int,Album> $albums
*
* @throws ModelNotFoundException
* @throws ModelDBException
Expand Down
13 changes: 8 additions & 5 deletions app/Actions/Albums/Top.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function get(): TopAlbumsResource
if (Configs::getValueAsBool('SA_enabled')) {
// Do not eagerly load the relation `photos` for each smart album.
// On the albums overview, we only need a thumbnail for each album.
/** @var BaseCollection<BaseSmartAlbum> $smartAlbums */
/** @var BaseCollection<int,BaseSmartAlbum> $smartAlbums */
$smartAlbums = $this->albumFactory
->getAllBuiltInSmartAlbums(false)
->map(
Expand All @@ -75,7 +75,9 @@ public function get(): TopAlbumsResource

$tagAlbumQuery = $this->albumQueryPolicy
->applyVisibilityFilter(TagAlbum::query()->with(['access_permissions', 'owner']));
/** @var BaseCollection<TagAlbum> $tagAlbums */

/** @var BaseCollection<int,TagAlbum> $tagAlbums */
/** @phpstan-ignore-next-line */
$tagAlbums = (new SortingDecorator($tagAlbumQuery))
->orderBy($this->sorting->column, $this->sorting->order)
->get();
Expand All @@ -87,22 +89,23 @@ public function get(): TopAlbumsResource
$userID = Auth::id();
if ($userID !== null) {
// For authenticated users we group albums by ownership.
/** @var BaseCollection<Album> $albums */
/** @var BaseCollection<int,Album> $albums */
$albums = (new SortingDecorator($query))
->orderBy(ColumnSortingType::OWNER_ID, OrderSortingType::ASC)
->orderBy($this->sorting->column, $this->sorting->order)
->get();

/**
* @var BaseCollection<Album> $a
* @var BaseCollection<Album> $b
* @var BaseCollection<int,Album> $a
* @var BaseCollection<int,Album> $b
*/
list($a, $b) = $albums->partition(fn ($album) => $album->owner_id === $userID);

return new TopAlbumsResource($smartAlbums, $tagAlbums, $a->values(), $b->values());
} else {
// For anonymous users we don't want to implicitly expose
// ownership via sorting.
/** @var BaseCollection<int,Album> */
$albums = (new SortingDecorator($query))
->orderBy($this->sorting->column, $this->sorting->order)
->get();
Expand Down
16 changes: 11 additions & 5 deletions app/Actions/Albums/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Models\Album;
use App\Models\Extensions\SortingDecorator;
use App\Policies\AlbumQueryPolicy;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Facades\Auth;
use Kalnoy\Nestedset\Collection as NsCollection;

Expand Down Expand Up @@ -61,9 +62,9 @@ public function get(): AlbumForestResource
}
$query->orderBy($this->sorting->column, $this->sorting->order);

/** @var NsCollection<Album> $albums */
/** @var NsCollection<int,Album> $albums */
$albums = $query->get();
/** @var ?NsCollection<Album> $sharedAlbums */
/** @var ?NsCollection<int,Album> $sharedAlbums */
$sharedAlbums = null;
$userID = Auth::id();
if ($userID !== null) {
Expand All @@ -74,15 +75,20 @@ public function get(): AlbumForestResource
// (sub)-tree and then `toTree` will return garbage as it does
// not find connected paths within `$albums` or `$sharedAlbums`,
// resp.
/** @var NsCollection<Album> $albums */
/** @var ?NsCollection<Album> $sharedAlbums */
/** @var NsCollection<int,Album> $albums */
/** @var ?NsCollection<int,Album> $sharedAlbums */
list($albums, $sharedAlbums) = $albums->partition(fn (Album $album) => $album->owner_id === $userID);
}

// We must explicitly pass `null` as the ID of the root album
// as there are several top-level albums below root.
// Otherwise, `toTree` uses the ID of the album with the lowest
// `_lft` value as the (wrong) root album.
return new AlbumForestResource($albums->toTree(null), $sharedAlbums?->toTree(null));
/** @var BaseCollection<int,\App\Contracts\Models\AbstractAlbum> $albumsTree */
$albumsTree = $albums->toTree(null);
/** @var BaseCollection<int,\App\Contracts\Models\AbstractAlbum> $sharedTree */
$sharedTree = $sharedAlbums?->toTree(null);

return new AlbumForestResource($albumsTree, $sharedTree);
}
}
8 changes: 4 additions & 4 deletions app/Actions/Db/BaseOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct()
/**
* Get the kind of driver used.
*
* @param array $ret reference array for return messages
* @param array<int,string> $ret reference array for return messages
*
* @return DbDriverType|null
*/
Expand Down Expand Up @@ -62,9 +62,9 @@ abstract public function do(): array;
/**
* Execute SQL statement.
*
* @param string $sql statment to be executed
* @param string $success success message
* @param array $ret reference array for return messages
* @param string $sql statment to be executed
* @param string $success success message
* @param array<int,string> $ret reference array for return messages
*
* @return void
*/
Expand Down
3 changes: 3 additions & 0 deletions app/Actions/Db/OptimizeDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class OptimizeDb extends BaseOptimizer
{
/**
* @return array<int, string>
*/
public function do(): array
{
$ret = ['Optimizing Database.'];
Expand Down
3 changes: 3 additions & 0 deletions app/Actions/Db/OptimizeTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class OptimizeTables extends BaseOptimizer
{
/**
* @return array<int, string>
*/
public function do(): array
{
$ret = ['Optimizing tables.'];
Expand Down
7 changes: 4 additions & 3 deletions app/Actions/Diagnostics/Pipes/Checks/ConfigSanityCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
class ConfigSanityCheck implements DiagnosticPipe
{
/** @var array<string,string> */
private array $settings;

/**
Expand All @@ -38,7 +39,7 @@ public function handle(array &$data, \Closure $next): array
/**
* Check that a certain set of configuration exists in the database.
*
* @param array $data
* @param array<int,string> $data
*
* @return void
*/
Expand All @@ -59,7 +60,7 @@ private function checkKeysExistsAndSet(array &$data): void
/**
* Warning if the Dropbox key does not exists.
*
* @param array $data
* @param array<int,string> $data
*
* @return void
*/
Expand All @@ -77,7 +78,7 @@ private function checkDropBoxKeyWarning(array &$data): void
/**
* Sanity check of the config.
*
* @param array $return
* @param array<int,string> $return
*/
private function sanity(array &$return): void
{
Expand Down
15 changes: 15 additions & 0 deletions app/Actions/Diagnostics/Pipes/Checks/ForeignKeyListInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public function handle(array &$data, \Closure $next): array
return $next($data);
}

/**
* @param array<int,string> $data
*
* @return void
*/
private function sqlite(array &$data): void
{
$fks = DB::select("SELECT m.name , p.* FROM sqlite_master m JOIN pragma_foreign_key_list(m.name) p ON m.name != p.\"table\" WHERE m.type = 'table' ORDER BY m.name;");
Expand All @@ -39,6 +44,11 @@ private function sqlite(array &$data): void
}
}

/**
* @param array<int,string> $data
*
* @return void
*/
private function mysql(array &$data): void
{
$fks = DB::select('select *
Expand All @@ -57,6 +67,11 @@ private function mysql(array &$data): void
}
}

/**
* @param array<int,string> $data
*
* @return void
*/
private function pgsql(array &$data): void
{
$fks = DB::select('SELECT tc.table_schema, tc.constraint_name, tc.table_name, kcu.column_name,
Expand Down
15 changes: 15 additions & 0 deletions app/Actions/Diagnostics/Pipes/Checks/PHPVersionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function handle(array &$data, \Closure $next): array
return $next($data);
}

/**
* @param array<int,string> $data
*
* @return void
*/
private function checkPhpVersion(array &$data): void
{
// As we cannot test this as those are just raising warnings which we cannot check via CICD.
Expand All @@ -44,6 +49,11 @@ private function checkPhpVersion(array &$data): void
}
}

/**
* @param array<int,string> $data
*
* @return void
*/
private function check32Bits(array &$data): void
{
// 32 or 64 bits ?
Expand All @@ -54,6 +64,11 @@ private function check32Bits(array &$data): void
}
}

/**
* @param array<int,string> $data
*
* @return void
*/
private function checkExtensions(array &$data): void
{
// Extensions
Expand Down
15 changes: 15 additions & 0 deletions app/Actions/Diagnostics/Pipes/Infos/CountForeignKeyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ public function handle(array &$data, \Closure $next): array
return $next($data);
}

/**
* @param array<int,string> $data
*
* @return void
*/
private function sqlite(array &$data): void
{
$fks = DB::select("SELECT m.name , p.* FROM sqlite_master m JOIN pragma_foreign_key_list(m.name) p ON m.name != p.\"table\" WHERE m.type = 'table' ORDER BY m.name;");
$data[] = Diagnostics::line('Number of foreign key:', sprintf('%d found.', count($fks)));
}

/**
* @param array<int,string> $data
*
* @return void
*/
private function mysql(array &$data): void
{
$fks = DB::select('select *
Expand All @@ -46,6 +56,11 @@ private function mysql(array &$data): void
$data[] = Diagnostics::line('Number of foreign key:', sprintf('%d found.', count($fks)));
}

/**
* @param array<int,string> $data
*
* @return void
*/
private function pgsql(array &$data): void
{
$fks = DB::select('SELECT tc.table_schema, tc.constraint_name, tc.table_name, kcu.column_name,
Expand Down
5 changes: 5 additions & 0 deletions app/Actions/Diagnostics/Pipes/Infos/SystemInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public function handle(array &$data, \Closure $next): array
return $next($data);
}

/**
* @param array<int,string> $data
*
* @return void
*/
private function getUploadLimit(array &$data): void
{
$size = Upload::getUploadLimit();
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/Import/Exec.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private static function normalizePath(string $path): string
*
* @param string $path
*
* @return array
* @return array<int,string>
*
* @throws FileOperationException
*/
Expand Down Expand Up @@ -368,7 +368,7 @@ private static function check_file_matches_pattern(string $pattern, string $file
}

/**
* @param array $my_array
* @param array<int,string> $my_array
*
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Import/FromUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FromUrl
* @param Album|null $album
* @param int $intendedOwnerId
*
* @return Collection<Photo> the collection of imported photos
* @return Collection<int,Photo> the collection of imported photos
*
* @throws MassImportException
*/
Expand Down
Loading

0 comments on commit 728853d

Please sign in to comment.