use PHP 7.4 syntaxes

This commit is contained in:
Pig Fang 2020-10-14 09:48:45 +08:00
parent 0959ec02d1
commit a5921770f0
No known key found for this signature in database
GPG Key ID: A8198F548DADA9E2
29 changed files with 123 additions and 226 deletions

View File

@ -37,10 +37,8 @@ class UpdateCommand extends Command
protected function procedures()
{
return collect([
'0.0.1' => function () {
// this is just for testing
event('__0.0.1');
},
// this is just for testing
'0.0.1' => fn () => event('__0.0.1'),
'5.0.0' => function () {
if (option('home_pic_url') === './app/bg.jpg') {
option(['home_pic_url' => './app/bg.webp']);

View File

@ -6,8 +6,7 @@ use App\Services\Plugin;
class PluginBootFailed extends Event
{
/** @var Plugin */
public $plugin;
public Plugin $plugin;
public function __construct(Plugin $plugin)
{

View File

@ -41,12 +41,8 @@ class Handler extends ExceptionHandler
'message' => $e->getMessage(),
'exception' => true,
'trace' => collect($e->getTrace())
->map(function ($trace) {
return Arr::only($trace, ['file', 'line']);
})
->filter(function ($trace) {
return Arr::has($trace, 'file');
})
->map(fn ($trace) => Arr::only($trace, ['file', 'line']))
->filter(fn ($trace) => Arr::has($trace, 'file'))
->map(function ($trace) {
$trace['file'] = str_replace(base_path().DIRECTORY_SEPARATOR, '', $trace['file']);

View File

@ -47,25 +47,13 @@ class AdminController extends Controller
public function chartData()
{
$xAxis = Collection::times(31, function ($i) {
return Carbon::today()->subDays(31 - $i)->format('m-d');
});
$xAxis = Collection::times(31, fn ($i) => Carbon::today()->subDays(31 - $i)->format('m-d'));
$oneMonthAgo = Carbon::today()->subMonth();
$grouping = function ($field) {
return function ($item) use ($field) {
return substr($item->$field, 5, 5);
};
};
$mapping = function ($item) {
return count($item);
};
$aligning = function ($data) {
return function ($day) use ($data) {
return $data->get($day) ?? 0;
};
};
$grouping = fn ($field) => fn ($item) => substr($item->$field, 5, 5);
$mapping = fn ($item) => count($item);
$aligning = fn ($data) => fn ($day) => ($data->get($day) ?? 0);
$userRegistration = User::where('register_at', '>=', $oneMonthAgo)
->select('register_at')
@ -105,9 +93,9 @@ class AdminController extends Controller
'pgsql' => 'PostgreSQL',
], config('database.default'), '');
$enabledPlugins = $plugins->getEnabledPlugins()->map(function ($plugin) {
return ['title' => trans($plugin->title), 'version' => $plugin->version];
});
$enabledPlugins = $plugins->getEnabledPlugins()->map(fn ($plugin) => [
'title' => trans($plugin->title), 'version' => $plugin->version,
]);
if ($filesystem->exists(base_path('.git'))) {
$process = new \Symfony\Component\Process\Process(

View File

@ -51,14 +51,15 @@ class ClosetController extends Controller
return $user
->closet()
->when($category === 'cape', function (Builder $query) {
return $query->where('type', 'cape');
}, function (Builder $query) {
return $query->whereIn('type', ['steve', 'alex']);
})
->when($request->input('q'), function (Builder $query, $search) {
return $query->like('item_name', $search);
})
->when(
$category === 'cape',
fn (Builder $query) => $query->where('type', 'cape'),
fn (Builder $query) => $query->whereIn('type', ['steve', 'alex']),
)
->when(
$request->input('q'),
fn (Builder $query, $search) => $query->like('item_name', $search)
)
->paginate((int) $request->input('perPage', 6));
}

View File

@ -45,12 +45,12 @@ class NotificationsController extends Controller
public function all()
{
return auth()->user()->unreadNotifications->map(function ($notification) {
return [
return auth()->user()
->unreadNotifications
->map(fn ($notification) => [
'id' => $notification->id,
'title' => $notification->data['title'],
];
});
]);
}
public function read($id)
@ -58,9 +58,7 @@ class NotificationsController extends Controller
$notification = auth()
->user()
->unreadNotifications
->first(function ($notification) use ($id) {
return $notification->id === $id;
});
->first(fn ($notification) => $notification->id === $id);
$notification->markAsRead();
$converter = new GithubFlavoredMarkdownConverter();

View File

@ -68,17 +68,13 @@ class SkinlibController extends Controller
$sortBy = $sort == 'time' ? 'upload_at' : $sort;
return Texture::orderBy($sortBy, 'desc')
->when($type === 'skin', function (Builder $query) {
return $query->whereIn('type', ['steve', 'alex']);
}, function (Builder $query) use ($type) {
return $query->where('type', $type);
})
->when($keyword, function (Builder $query, $keyword) {
return $query->like('name', $keyword);
})
->when($uploader, function (Builder $query, $uploader) {
return $query->where('uploader', $uploader);
})
->when(
$type === 'skin',
fn (Builder $query) => $query->whereIn('type', ['steve', 'alex']),
fn (Builder $query) => $query->where('type', $type),
)
->when($keyword, fn (Builder $query, $keyword) => $query->like('name', $keyword))
->when($uploader, fn (Builder $query, $uploader) => $query->where('uploader', $uploader))
->when($user, function (Builder $query, User $user) {
if (!$user->isAdmin()) {
// use closure-style `where` clause to lift up SQL priority
@ -262,10 +258,9 @@ class SkinlibController extends Controller
$user = Auth::user();
$duplicated = Texture::where('hash', $hash)
->where(function ($query) use ($user) {
return $query->where('public', true)
->orWhere('uploader', $user->uid);
})
->where(
fn (Builder $query) => $query->where('public', true)->orWhere('uploader', $user->uid)
)
->first();
if ($duplicated) {
// if the texture already uploaded was set to private,

View File

@ -12,20 +12,15 @@ use Illuminate\View\View;
class FootComposer
{
/** @var Request */
protected $request;
protected Request $request;
/** @var Webpack */
protected $webpack;
protected Webpack $webpack;
/** @var JavaScript */
protected $javascript;
protected JavaScript $javascript;
/** @var Dispatcher */
protected $dispatcher;
protected Dispatcher $dispatcher;
/** @var Filter */
protected $filter;
protected Filter $filter;
public function __construct(
Request $request,

View File

@ -12,17 +12,13 @@ use Illuminate\View\View;
class HeadComposer
{
/** @var Webpack */
protected $webpack;
protected Webpack $webpack;
/** @var Dispatcher */
protected $dispatcher;
protected Dispatcher $dispatcher;
/** @var Request */
protected $request;
protected Request $request;
/** @var Filter */
protected $filter;
protected Filter $filter;
public function __construct(
Webpack $webpack,

View File

@ -8,8 +8,7 @@ use Illuminate\View\View;
class LanguagesMenuComposer
{
/** @var Request */
protected $request;
protected Request $request;
public function __construct(Request $request)
{
@ -22,9 +21,7 @@ class LanguagesMenuComposer
$path = $this->request->path();
$langs = collect(config('locales'))
->reject(function ($locale) {
return Arr::has($locale, 'alias');
})
->reject(fn ($locale) => Arr::has($locale, 'alias'))
->map(function ($locale, $id) use ($query, $path) {
$query = array_merge($query, ['lang' => $id]);
$locale['url'] = url($path.'?'.http_build_query($query));

View File

@ -3,6 +3,7 @@
namespace App\Http\View\Composers;
use App\Events;
use App\Services\Plugin;
use App\Services\PluginManager;
use Blessing\Filter;
use Illuminate\Http\Request;
@ -11,11 +12,9 @@ use Illuminate\View\View;
class SideMenuComposer
{
/** @var Request */
protected $request;
protected Request $request;
/** @var Filter */
protected $filter;
protected Filter $filter;
public function __construct(Request $request, Filter $filter)
{
@ -44,9 +43,7 @@ class SideMenuComposer
$menu = $menu[$type];
$menu = $this->filter->apply('side_menu', $menu, [$type]);
$view->with('items', array_map(function ($item) {
return $this->transform($item);
}, $menu));
$view->with('items', array_map(fn ($item) => $this->transform($item), $menu));
}
public function transform(array $item): array
@ -66,9 +63,10 @@ class SideMenuComposer
}
if (Arr::has($item, 'children')) {
$item['children'] = array_map(function ($item) {
return $this->transform($item);
}, $item['children']);
$item['children'] = array_map(
fn ($item) => $this->transform($item),
$item['children'],
);
}
$item['classes'] = $classes;
@ -82,9 +80,7 @@ class SideMenuComposer
if (Arr::get($item, 'id') === 'plugin-configs') {
$pluginConfigs = resolve(PluginManager::class)
->getEnabledPlugins()
->filter(function ($plugin) {
return $plugin->hasConfig();
})
->filter(fn (Plugin $plugin) => $plugin->hasConfig())
->map(function ($plugin) {
return [
'title' => trans($plugin->title),
@ -95,7 +91,7 @@ class SideMenuComposer
// Don't display this menu item when no plugin config is available
if ($pluginConfigs->isNotEmpty()) {
$item['children'] = array_merge($item['children'], $pluginConfigs->values()->all());
array_push($item['children'], ...$pluginConfigs->values()->all());
return $item;
}

View File

@ -8,11 +8,9 @@ use Illuminate\View\View;
class UserMenuComposer
{
/** @var Request */
protected $request;
protected Request $request;
/** @var Filter */
protected $filter;
protected Filter $filter;
public function __construct(Request $request, Filter $filter)
{

View File

@ -9,11 +9,9 @@ use Illuminate\View\View;
class UserPanelComposer
{
/** @var Dispatcher */
protected $dispatcher;
protected Dispatcher $dispatcher;
/** @var Filter */
protected $filter;
protected Filter $filter;
public function __construct(Dispatcher $dispatcher, Filter $filter)
{

View File

@ -7,8 +7,7 @@ use Symfony\Component\Finder\SplFileInfo;
class CleanUpFrontEndLocaleFiles
{
/** @var Filesystem */
protected $filesystem;
protected Filesystem $filesystem;
public function __construct(Filesystem $filesystem)
{

View File

@ -7,8 +7,7 @@ use Illuminate\Filesystem\Filesystem;
class CopyPluginAssets
{
/** @var Filesystem */
protected $filesystem;
protected Filesystem $filesystem;
public function __construct(Filesystem $filesystem)
{

View File

@ -7,8 +7,7 @@ use Illuminate\Http\Request;
class SetAppLocale
{
/** @var Request */
protected $request;
protected Request $request;
public function __construct(Request $request)
{

View File

@ -66,9 +66,7 @@ class Hook
$urls = collect($urls);
$pages = collect($pages);
resolve(Filter::class)->add('head_links', function ($links) use ($urls, $pages) {
$matched = $pages->some(function ($page) {
return request()->is($page);
});
$matched = $pages->some(fn ($page) => request()->is($page));
if ($matched) {
$urls->each(function ($url) use (&$links) {
$links[] = [
@ -88,9 +86,7 @@ class Hook
$urls = collect($urls);
$pages = collect($pages);
resolve(Filter::class)->add('scripts', function ($scripts) use ($urls, $pages) {
$matched = $pages->some(function ($page) {
return request()->is($page);
});
$matched = $pages->some(fn ($page) => request()->is($page));
if ($matched) {
$urls->each(function ($url) use (&$scripts) {
$scripts[] = ['src' => $url, 'crossorigin' => 'anonymous'];

View File

@ -21,9 +21,9 @@ class Option
}
try {
$this->items = DB::table('options')->get()->mapWithKeys(function ($item) {
return [$item->option_name => $item->option_value];
});
$this->items = DB::table('options')
->get()
->mapWithKeys(fn ($item) => [$item->option_name => $item->option_value]);
} catch (QueryException $e) {
$this->items = collect();
}

View File

@ -329,7 +329,7 @@ class OptionForm
$this->assignValues();
return view('forms.form')
->with(array_merge(get_object_vars($this)))
->with(get_object_vars($this))
->render();
}

View File

@ -15,19 +15,11 @@ class Plugin
'README.MD',
];
/**
* The full path of this plugin.
*
* @var string
*/
protected $path;
/** The full path of this plugin. */
protected string $path;
/**
* package.json of the package.
*
* @var array
*/
protected $manifest;
/** package.json of the package. */
protected array $manifest;
protected $enabled = false;
@ -61,9 +53,10 @@ class Plugin
public function getReadme()
{
return Arr::first(self::README_FILES, function ($filename) {
return file_exists($this->path.'/'.$filename);
});
return Arr::first(
self::README_FILES,
fn ($filename) => file_exists($this->path.'/'.$filename)
);
}
public function hasConfig(): bool

View File

@ -16,29 +16,21 @@ use Illuminate\Support\Str;
class PluginManager
{
/** @var bool */
protected $booted = false;
protected bool $booted = false;
/** @var Application */
protected $app;
protected Application $app;
/** @var Option */
protected $option;
protected Option $option;
/** @var Dispatcher */
protected $dispatcher;
protected Dispatcher $dispatcher;
/** @var Filesystem */
protected $filesystem;
protected Filesystem $filesystem;
/** @var ClassLoader */
protected $loader;
protected ClassLoader $loader;
/** @var Collection|null */
protected $plugins;
protected ?Collection $plugins;
/** @var Collection */
protected $enabled;
protected Collection $enabled;
public function __construct(
Application $app,
@ -56,28 +48,20 @@ class PluginManager
public function all(): Collection
{
if (filled($this->plugins)) {
if (isset($this->plugins)) {
return $this->plugins;
}
$this->enabled = collect(json_decode($this->option->get('plugins_enabled', '[]'), true))
->reject(function ($item) {
return is_string($item);
})
->mapWithKeys(function ($item) {
return [$item['name'] => ['version' => $item['version']]];
});
->reject(fn ($item) => is_string($item))
->mapWithKeys(fn ($item) => [$item['name'] => ['version' => $item['version']]]);
$plugins = collect();
$versionChanged = [];
$this->getPluginsDirs()
->flatMap(function ($directory) {
return $this->filesystem->directories($directory);
})
->flatMap(fn ($dir) => $this->filesystem->directories($dir))
->unique()
->filter(function ($directory) {
return $this->filesystem->exists($directory.DIRECTORY_SEPARATOR.'package.json');
})
->filter(fn ($dir) => $this->filesystem->exists($dir.DIRECTORY_SEPARATOR.'package.json'))
->each(function ($directory) use (&$plugins, &$versionChanged) {
$manifest = json_decode(
$this->filesystem->get($directory.DIRECTORY_SEPARATOR.'package.json'),
@ -126,18 +110,12 @@ class PluginManager
return;
}
$this->all()->each(function ($plugin) {
$this->loadViewsAndTranslations($plugin);
});
$this->all()->each(fn ($plugin) => $this->loadViewsAndTranslations($plugin));
$enabled = $this->getEnabledPlugins();
$enabled->each(function ($plugin) {
$this->registerPlugin($plugin);
});
$enabled->each(fn ($plugin) => $this->registerPlugin($plugin));
$this->loader->register();
$enabled->each(function ($plugin) {
$this->bootPlugin($plugin);
});
$enabled->each(fn ($plugin) => $this->bootPlugin($plugin));
$this->registerLifecycleHooks();
$this->booted = true;
@ -324,9 +302,7 @@ class PluginManager
public function getEnabledPlugins(): Collection
{
return $this->all()->filter(function ($plugin) {
return $plugin->isEnabled();
});
return $this->all()->filter(fn ($plugin) => $plugin->isEnabled());
}
/**
@ -334,9 +310,13 @@ class PluginManager
*/
protected function saveEnabled()
{
$this->option->set('plugins_enabled', $this->enabled->map(function ($info, $name) {
return array_merge(compact('name'), $info);
})->values()->toJson());
$this->option->set(
'plugins_enabled',
$this->enabled
->map(fn ($info, $name) => array_merge(['name' => $name], $info))
->values()
->toJson()
);
}
public function getUnsatisfied(Plugin $plugin)
@ -419,9 +399,7 @@ class PluginManager
$config = config('plugins.directory');
if ($config) {
return collect(preg_split('/,\s*/', $config))
->map(function ($directory) {
return realpath($directory) ?: $directory;
});
->map(fn ($directory) => realpath($directory) ?: $directory);
} else {
return collect([base_path('plugins')]);
}

View File

@ -9,14 +9,11 @@ use Illuminate\Filesystem\Filesystem;
class JavaScript
{
/** @var Filesystem */
protected $filesystem;
protected Filesystem $filesystem;
/** @var Repository */
protected $cache;
protected Repository $cache;
/** @var PluginManager */
protected $plugins;
protected PluginManager $plugins;
protected $prefix = 'front-end-trans-';
@ -34,16 +31,10 @@ class JavaScript
{
$plugins = $this->plugins->getEnabledPlugins();
$sourceFiles = $plugins
->map(function (Plugin $plugin) use ($locale) {
return $plugin->getPath()."/lang/$locale/front-end.yml";
})
->filter(function ($path) {
return $this->filesystem->exists($path);
});
->map(fn (Plugin $plugin) => $plugin->getPath()."/lang/$locale/front-end.yml")
->filter(fn ($path) => $this->filesystem->exists($path));
$sourceFiles->push(resource_path("lang/$locale/front-end.yml"));
$sourceModified = $sourceFiles->max(function ($path) {
return $this->filesystem->lastModified($path);
});
$sourceModified = $sourceFiles->max(fn ($path) => $this->filesystem->lastModified($path));
$compiled = public_path("lang/$locale.js");
$compiledModified = (int) $this->cache->get($this->prefix.$locale, 0);
@ -51,10 +42,7 @@ class JavaScript
if ($sourceModified > $compiledModified || !$this->filesystem->exists($compiled)) {
$translations = trans('front-end');
foreach ($plugins as $plugin) {
$translations = array_merge(
$translations,
[$plugin->name => trans($plugin->namespace.'::front-end')]
);
$translations[$plugin->name] = trans($plugin->namespace.'::front-end');
}
$content = 'blessing.i18n = '.json_encode($translations, JSON_UNESCAPED_UNICODE);

View File

@ -7,8 +7,7 @@ use Symfony\Component\Yaml\Yaml as YamlParser;
class Yaml
{
/** @var Repository */
protected $cache;
protected Repository $cache;
protected $prefix = 'yaml-trans-';

View File

@ -10,11 +10,9 @@ use ZipArchive;
class Unzip
{
/** @var Filesystem */
protected $filesystem;
protected Filesystem $filesystem;
/** @var ZipArchive */
protected $zipper;
protected ZipArchive $zipper;
public function __construct(Filesystem $filesystem, ZipArchive $zipper)
{

View File

@ -11,12 +11,11 @@ use Illuminate\Support\Str;
class Webpack
{
protected $manifest = [];
protected array $manifest = [];
/** @var Option */
protected $options;
protected Option $options;
protected $urlGenerator;
protected UrlGenerator $urlGenerator;
public function __construct(
Filesystem $filesystem,

View File

@ -11,8 +11,8 @@ class HandlerTest extends TestCase
$json = $this->get('/abc', ['Accept' => 'application/json'])->decodeResponseJson();
$this->assertIsString($json['message']);
$this->assertTrue($json['exception']);
$this->assertTrue(collect($json['trace'])->every(function ($trace) {
return Str::startsWith($trace['file'], 'app') && is_int($trace['line']);
}));
$this->assertTrue(collect($json['trace'])->every(
fn ($trace) => Str::startsWith($trace['file'], 'app') && is_int($trace['line'])
));
}
}

View File

@ -15,7 +15,7 @@ class Filter extends BaseFilter
if (!Arr::has($this->applied, $hook)) {
$this->applied[$hook] = [];
}
$this->applied[$hook][] = array_merge([$init], $args);
$this->applied[$hook][] = [$init, ...$args];
return parent::apply($hook, $init, $args);
}
@ -56,9 +56,7 @@ class Filter extends BaseFilter
$result = Arr::first(
$this->applied[$hook],
function ($arguments) use ($predicate) {
return call_user_func_array($predicate, $arguments);
}
fn ($arguments) => call_user_func_array($predicate, $arguments),
);
Assert::assertNotNull(
$result,

View File

@ -242,9 +242,7 @@ class OptionFormTest extends TestCase
public function testHandle()
{
$form = new OptionForm('test');
$form->text('t')->format(function ($data) {
return "formatted $data";
});
$form->text('t')->format(fn ($data) => "formatted $data");
$request = request();
$request->setMethod('POST');

View File

@ -375,9 +375,7 @@ class PluginManagerTest extends TestCase
->with('/mayaka/bootstrap.php')
->andReturn(function () {
throw new \Exception();
}, function () {
abort(500);
});
}, fn () => abort(500));
});
app()->forgetInstance(PluginManager::class);