Skip to content

Commit

Permalink
Fix CS
Browse files Browse the repository at this point in the history
  • Loading branch information
mzur committed Dec 19, 2024
1 parent c4785d9 commit 8eb5637
Show file tree
Hide file tree
Showing 21 changed files with 87 additions and 146 deletions.
3 changes: 2 additions & 1 deletion app/Console/Commands/SyncUuids.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ protected function generateOutput()
{
$users = User::select('uuid', 'firstname', 'lastname', 'email')->get();
$bar = $this->output->createProgressBar($users->count());
$users = $users->map(function ($user) use ($bar) {
$users =
$users->map(function ($user) use ($bar) {
$bar->advance();

return [
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/Import/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Biigle\Http\Controllers\Api\Import;

use Biigle\Http\Controllers\Api\Controller;
use Biigle\Project;
use Biigle\Services\Import\ArchiveManager;
use Biigle\Services\Import\LabelTreeImport;
use Biigle\Services\Import\UserImport;
use Biigle\Services\Import\VolumeImport;
use Biigle\Project;
use Biigle\Volume;
use Exception;
use Illuminate\Http\Request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Biigle\Http\Controllers\Api\Controller;
use Biigle\LabelTree;
use Biigle\Services\Import\ArchiveManager;
use Biigle\Role;
use Biigle\Services\Import\ArchiveManager;
use DB;
use Exception;
use Illuminate\Http\Request;
Expand Down
10 changes: 3 additions & 7 deletions app/Http/Controllers/Views/Admin/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use Biigle\Http\Controllers\Views\Controller;
use Biigle\Label;
use Biigle\Role;
use Biigle\Services\Import\ArchiveManager;
use Biigle\Services\Import\LabelTreeImport;
use Biigle\Services\Import\UserImport;
use Biigle\Services\Import\VolumeImport;
use Biigle\Role;
use Biigle\User;

class ImportController extends Controller
Expand Down Expand Up @@ -102,9 +102,7 @@ protected function showLabelTreeImport(LabelTreeImport $import, $token)
$labelCandidates = $import->getLabelImportCandidates();
$labelCandidatesCount = $labelCandidates->count();
$conflictingParentIds = $labelCandidates->pluck('conflicting_parent_id')
->reject(function ($id) {
return is_null($id);
});
->reject(fn ($id) => is_null($id));
if ($conflictingParentIds->isNotEmpty()) {
$conflictingParents = Label::whereIn('id', $conflictingParentIds)->get();
} else {
Expand Down Expand Up @@ -153,9 +151,7 @@ protected function showVolumeImport(VolumeImport $import, string $token)

$labelCandidates = $import->getLabelImportCandidates();
$conflictingParentIds = $labelCandidates->pluck('conflicting_parent_id')
->reject(function ($id) {
return is_null($id);
});
->reject(fn ($id) => is_null($id));
if ($conflictingParentIds->isNotEmpty()) {
$conflictingParents = Label::whereIn('id', $conflictingParentIds)->get();
} else {
Expand Down
2 changes: 0 additions & 2 deletions app/Jobs/PostprocessVolumeImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Biigle\Jobs;

use Biigle\Image;
use Biigle\Jobs\Job;
use Biigle\Jobs\ProcessNewVolumeFiles;
use Biigle\Modules\Largo\Jobs\ProcessAnnotatedImage;
use Biigle\Modules\Largo\Jobs\ProcessAnnotatedVideo;
use Biigle\Video;
Expand Down
3 changes: 2 additions & 1 deletion app/Services/Export/LabelTreeExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class LabelTreeExport extends Export
*/
public function getContent()
{
$trees = LabelTree::where(function ($query) {
$trees =
LabelTree::where(function ($query) {
$query->whereIn('id', $this->ids)
// Also add master trees of all included versioned trees.
->orWhereIn('id', function ($query) {
Expand Down
4 changes: 1 addition & 3 deletions app/Services/Import/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ protected function expectedFiles()
*/
protected function files()
{
return array_map(function ($file) {
return File::basename($file);
}, File::files($this->path));
return array_map(fn ($file) => File::basename($file), File::files($this->path));
}

/**
Expand Down
65 changes: 22 additions & 43 deletions app/Services/Import/LabelTreeImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Biigle\Visibility;
use Carbon\Carbon;
use DB;
use Exception;
use Illuminate\Support\Collection;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;

Expand Down Expand Up @@ -52,9 +51,7 @@ public function perform(array $onlyTrees = null, array $onlyLabels = null, array
$this->attachLabelTreeMembers($insertTrees, $labelTreeIdMap, $userIdMap);

$labelCandidates = $this->getInsertLabels($onlyLabels, $labelTreeIdMap);
$labelHasConflict = function ($label) {
return array_key_exists('conflicting_name', $label) || array_key_exists('conflicting_parent_id', $label);
};
$labelHasConflict = fn ($label) => array_key_exists('conflicting_name', $label) || array_key_exists('conflicting_parent_id', $label);

$insertLabels = $labelCandidates->reject($labelHasConflict);
// Insert all labels with parent_id null first.
Expand Down Expand Up @@ -151,7 +148,8 @@ public function getLabelImportCandidates()
// Add the conflicting attributes to the labels if there are any. Discard any
// import labels that already exist and have no conflicts. Use values() to
// discard the original keys.
return $labels->map(function ($label) use ($existingLabels) {
return $labels
->map(function ($label) use ($existingLabels) {
$existingLabel = $existingLabels->get($label['uuid']);
if ($existingLabel) {
$label['discard'] = true;
Expand All @@ -171,9 +169,7 @@ public function getLabelImportCandidates()

return $label;
})
->reject(function ($label) {
return array_key_exists('discard', $label);
})
->reject(fn ($label) => array_key_exists('discard', $label))
->values();
}

Expand Down Expand Up @@ -248,16 +244,11 @@ protected function getInsertLabelTrees($onlyTrees)
{
$now = Carbon::now();
$candidates = $this->getLabelTreeImportCandidates();
$trees = $candidates->when(is_array($onlyTrees), function ($collection) use ($onlyTrees) {
return $collection->whereIn('id', $onlyTrees);
});
$trees = $candidates->when(is_array($onlyTrees), fn ($collection) => $collection->whereIn('id', $onlyTrees));

$masterTreeIdsMissing = $trees->reject(function ($tree) {
return !array_key_exists('version', $tree) || is_null($tree['version']);
})
->map(function ($tree) {
return $tree['version']['label_tree_id'];
});
$masterTreeIdsMissing = $trees
->reject(fn ($tree) => !array_key_exists('version', $tree) || is_null($tree['version']))
->map(fn ($tree) => $tree['version']['label_tree_id']);

$masterTrees = $candidates->whereIn('id', $masterTreeIdsMissing)
->whereNotIn('id', $trees->pluck('id'));
Expand Down Expand Up @@ -308,10 +299,9 @@ protected function insertLabelTrees($trees)
*/
protected function insertLabelTreeVersions($insertTrees, $labelTreeIdMap)
{
$this->getImportLabelTrees()->whereIn('uuid', $insertTrees->pluck('uuid'))
->reject(function ($tree) {
return !array_key_exists('version', $tree) || is_null($tree['version']);
})
$this->getImportLabelTrees()
->whereIn('uuid', $insertTrees->pluck('uuid'))
->reject(fn ($tree) => !array_key_exists('version', $tree) || is_null($tree['version']))
->each(function ($tree) use ($labelTreeIdMap) {
$id = LabelTreeVersion::insertGetId([
'name' => $tree['version']['name'],
Expand All @@ -336,9 +326,7 @@ protected function getInsertUserIds($trees)
->whereIn('uuid', $trees->pluck('uuid'))
->pluck('members')
->collapse()
->filter(function ($user) {
return $user['role_id'] === Role::adminId();
})
->filter(fn ($user) => $user['role_id'] === Role::adminId())
->pluck('id')
->unique()
->toArray();
Expand All @@ -363,9 +351,7 @@ protected function attachLabelTreeMembers($trees, $labelTreeIdMap, $userIdMap)
}, $tree['members']);
})
->collapse()
->filter(function ($user) use ($userIdMap) {
return array_key_exists($user['id'], $userIdMap);
})
->filter(fn ($user) => array_key_exists($user['id'], $userIdMap))
->map(function ($member) use ($labelTreeIdMap, $userIdMap) {
return [
'user_id' => $userIdMap[$member['id']],
Expand All @@ -387,9 +373,7 @@ protected function attachLabelTreeMembers($trees, $labelTreeIdMap, $userIdMap)
*/
protected function getInsertLabels($onlyLabels, $labelTreeIdMap)
{
$importedTrees = array_keys(array_filter($labelTreeIdMap, function ($value, $key) {
return $key !== $value;
}, ARRAY_FILTER_USE_BOTH));
$importedTrees = array_keys(array_filter($labelTreeIdMap, fn ($value, $key) => $key !== $value, ARRAY_FILTER_USE_BOTH));

// As the import label trees have been imported at this point, their import
// labels are included in the candidates now, too.
Expand Down Expand Up @@ -455,7 +439,8 @@ protected function insertLabels($labels, $labelTreeIdMap)
*/
protected function updateInsertedLabelParentIds($labels, $labelIdMap)
{
$labels->reject(function ($label) use ($labelIdMap) {
$labels
->reject(function ($label) use ($labelIdMap) {
return is_null($label['parent_id']) ||
// This might be the case if a user selectively imports a child label
// but not its parent.
Expand All @@ -477,29 +462,23 @@ protected function updateInsertedLabelParentIds($labels, $labelIdMap)
*/
protected function mergeLabels($mergeLabels, $nameConflictResolution, $parentConflictResolution, $labelIdMap)
{
$nameConflicts = $mergeLabels->filter(function ($label) {
return array_key_exists('conflicting_name', $label);
})
$nameConflicts = $mergeLabels
->filter(fn ($label) => array_key_exists('conflicting_name', $label))
->each(function ($label) use ($nameConflictResolution) {
if (!array_key_exists($label['id'], $nameConflictResolution)) {
throw new UnprocessableEntityHttpException("Unresolved name conflict for label '{$label['name']}'.");
}
})
->filter(function ($label) use ($nameConflictResolution) {
return $nameConflictResolution[$label['id']] === 'import';
});
->filter(fn ($label) => $nameConflictResolution[$label['id']] === 'import');

$parentConflicts = $mergeLabels->filter(function ($label) {
return array_key_exists('conflicting_parent_id', $label);
})
$parentConflicts = $mergeLabels
->filter(fn ($label) => array_key_exists('conflicting_parent_id', $label))
->each(function ($label) use ($parentConflictResolution) {
if (!array_key_exists($label['id'], $parentConflictResolution)) {
throw new UnprocessableEntityHttpException("Unresolved parent conflict for label '{$label['name']}'.");
}
})
->filter(function ($label) use ($parentConflictResolution) {
return $parentConflictResolution[$label['id']] === 'import';
});
->filter(fn ($label) => $parentConflictResolution[$label['id']] === 'import');

// Resolve name and parent conflicts *after* the check if every conflict has a
// resolution.
Expand Down
12 changes: 3 additions & 9 deletions app/Services/Import/UserImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ public function perform(array $only = null)
$users = $this->getImportUsers();
$now = Carbon::now();
$candidates = $this->getUserImportCandidates()
->when(is_array($only), function ($collection) use ($only) {
return $collection->whereIn('id', $only);
});
->when(is_array($only), fn ($collection) => $collection->whereIn('id', $only));

$conflicts = $candidates->whereIn('id', $this->getConflicts()->pluck('id'));
if ($conflicts->isNotEmpty()) {
$users = $conflicts->map(function ($user) {
return "{$user['firstname']} {$user['lastname']} ({$user['email']})";
})->implode(', ');
$users = $conflicts->map(fn ($user) => "{$user['firstname']} {$user['lastname']} ({$user['email']})")->implode(', ');

throw new UnprocessableEntityHttpException("Import cannot be performed. The following users exist according to their email address but the UUIDs do not match: {$users}.");
}
Expand Down Expand Up @@ -132,8 +128,6 @@ protected function getConflicts()
$users = $this->getImportUsers();
$existing = User::whereIn('email', $users->pluck('email'))->pluck('uuid', 'email');

return $users->filter(function ($user) use ($existing) {
return $existing->has($user['email']) && $user['uuid'] !== $existing->get($user['email']);
});
return $users->filter(fn ($user) => $existing->has($user['email']) && $user['uuid'] !== $existing->get($user['email']));
}
}
22 changes: 6 additions & 16 deletions app/Services/Import/VolumeImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Biigle\ImageLabel;
use Biigle\Jobs\PostprocessVolumeImport;
use Biigle\Label;
use Biigle\LabelTree;
use Biigle\MediaType;
use Biigle\Project;
use Biigle\Rules\VolumeUrl;
Expand All @@ -19,7 +18,6 @@
use Biigle\VideoLabel;
use Biigle\Volume;
use DB;
use Exception;
use Illuminate\Support\Collection;
use Ramsey\Uuid\Uuid;
use SplFileObject;
Expand Down Expand Up @@ -63,9 +61,7 @@ public function perform(Project $project, User $creator, array $only = null, arr
{
return DB::transaction(function () use ($project, $creator, $only, $newUrls, $nameConflictResolution, $parentConflictResolution) {
$volumeCandidates = $this->getVolumeImportCandidates()
->when(is_array($only), function ($collection) use ($only) {
return $collection->whereIn('id', $only);
})
->when(is_array($only), fn ($collection) => $collection->whereIn('id', $only))
->keyBy('id');

$volumes = $this->insertVolumes($volumeCandidates, $creator, $newUrls);
Expand Down Expand Up @@ -168,9 +164,7 @@ public function getVolumeImportCandidates()
$volume['labels'] = [];
}

$volume['label_trees'] = array_values(array_unique(array_map(function ($id) use ($labelToTreeMap) {
return $labelToTreeMap[$id];
}, $volume['labels'])));
$volume['label_trees'] = array_values(array_unique(array_map(fn ($id) => $labelToTreeMap[$id], $volume['labels'])));

return $volume;
});
Expand Down Expand Up @@ -425,13 +419,8 @@ protected function getRequiredEntities()
}
}

$labels = array_map(function ($ids) {
return array_values(array_unique($ids));
}, $labels);

$users = array_map(function ($ids) {
return array_values(array_unique($ids));
}, $users);
$labels = array_map(fn ($ids) => array_values(array_unique($ids)), $labels);
$users = array_map(fn ($ids) => array_values(array_unique($ids)), $users);

return compact('labels', 'users');
}
Expand All @@ -449,7 +438,8 @@ protected function insertVolumes(Collection $candidates, User $creator, array $n
{
$mediaTypes = MediaType::pluck('id', 'name');

return $candidates->map(function ($candidate) use ($creator, $newUrls, $mediaTypes) {
return $candidates
->map(function ($candidate) use ($creator, $newUrls, $mediaTypes) {
$volume = new Volume;
$volume->old_id = $candidate['id'];
$volume->name = $candidate['name'];
Expand Down
Loading

0 comments on commit 8eb5637

Please sign in to comment.