Skip to content

Commit

Permalink
Some cleanup and best practices from phpstan feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ifox committed Feb 28, 2020
1 parent 9d3578e commit e1e6794
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 58 deletions.
21 changes: 6 additions & 15 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,19 @@ parameters:
paths:
- src
- routes
- migrations
- config
- tests

# 8 is the highest level
level: 6
level: 1

autoload_directories:
- migrations

autoload_files:
- migrations/add_locale_column_to_twill_mediables.php
- migrations/add_locale_column_to_twill_mediables.php
- migrations/add_two_factor_auth_columns_to_twill_users.php
- migrations/change_locale_column_in_twill_fileables.php
- migrations/create_blocks_table.php
- migrations/create_features_table.php
- migrations/create_files_tables.php
- migrations/create_medias_tables.php
- migrations/create_related_table.php
- migrations/create_settings_table.php
- migrations/create_tags_tables.php
- migrations/create_twill_activity_log_table.php
- migrations/create_twill_users_tables.php
- tests/helpers.php

ignoreErrors:

excludes_analyse:
- tests/stubs/*
40 changes: 40 additions & 0 deletions src/Commands/ModuleMake.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,46 @@ class ModuleMake extends Command
*/
protected $config;

/**
* @var bool
*/
protected $blockable;

/**
* @var bool
*/
protected $translatable;

/**
* @var bool
*/
protected $sluggable;

/**
* @var bool
*/
protected $mediable;

/**
* @var bool
*/
protected $fileable;

/**
* @var bool
*/
protected $sortable;

/**
* @var bool
*/
protected $revisionable;

/**
* @var bool
*/
protected $defaultsAnswserToNo;

/**
* @param Filesystem $files
* @param Composer $composer
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/Admin/FeaturedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private function getFeaturedSources(Request $request, $featuredSection, $search
$featuredSources = [];

Collection::make($featuredSection['buckets'])->map(function ($bucket, $bucketKey) use (&$fetchedModules, $search, $request) {
return Collection::make($bucket['bucketables'])->mapWithKeys(function ($bucketable) use (&$fetchedModules, $bucketKey, $search, $request) {
return Collection::make($bucket['bucketables'])->mapWithKeys(function ($bucketable) use (&$fetchedModules, $search, $request) {

$module = $bucketable['module'];
$repository = $this->getRepository($module, $bucketable['repository'] ?? null);
Expand Down Expand Up @@ -177,7 +177,7 @@ private function getFeaturedSources(Request $request, $featuredSection, $search
]];
});
})->each(function ($bucketables, $bucket) use (&$featuredSources) {
$bucketables->each(function ($bucketableData, $bucketable) use ($bucket, &$featuredSources) {
$bucketables->each(function ($bucketableData, $bucketable) use (&$featuredSources) {
$featuredSources[$bucketable]['name'] = $bucketableData['name'];
$featuredSources[$bucketable]['maxPage'] = $bucketableData['items']->lastPage();
$featuredSources[$bucketable]['offset'] = $bucketableData['items']->perPage();
Expand Down
38 changes: 26 additions & 12 deletions src/Http/Controllers/Admin/FileLibraryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,28 @@ class FileLibraryController extends ModuleController implements SignUploadListen
*/
protected $endpointType;

/**
* @var Illuminate\Routing\UrlGenerator
*/
protected $urlGenerator;

/**
* @var Illuminate\Routing\ResponseFactory
*/
protected $responseFactory;

/**
* @var Illuminate\Config\Repository
*/
protected $config;

public function __construct(
Application $app,
Request $request,
UrlGenerator $urlGenerator,
ResponseFactory $responseFactory,
Config $config
)
{
) {
parent::__construct($app, $request);
$this->urlGenerator = $urlGenerator;
$this->responseFactory = $responseFactory;
Expand Down Expand Up @@ -108,14 +122,14 @@ public function getIndexData($prependScope = [])
private function buildFile($item)
{
return $item->toCmsArray() + [
'tags' => $item->tags->map(function ($tag) {
return $tag->name;
}),
'deleteUrl' => $item->canDeleteSafely() ? moduleRoute($this->moduleName, $this->routePrefix, 'destroy', $item->id) : null,
'updateUrl' => $this->urlGenerator->route('admin.file-library.files.single-update'),
'updateBulkUrl' => $this->urlGenerator->route('admin.file-library.files.bulk-update'),
'deleteBulkUrl' => $this->urlGenerator->route('admin.file-library.files.bulk-delete'),
];
'tags' => $item->tags->map(function ($tag) {
return $tag->name;
}),
'deleteUrl' => $item->canDeleteSafely() ? moduleRoute($this->moduleName, $this->routePrefix, 'destroy', $item->id) : null,
'updateUrl' => $this->urlGenerator->route('admin.file-library.files.single-update'),
'updateBulkUrl' => $this->urlGenerator->route('admin.file-library.files.bulk-update'),
'deleteBulkUrl' => $this->urlGenerator->route('admin.file-library.files.bulk-delete'),
];
}

/**
Expand Down Expand Up @@ -265,8 +279,8 @@ public function signAzureUpload(Request $request, SignAzureUpload $signAzureUplo
public function uploadIsSigned($signature, $isJsonResponse = true)
{
return $isJsonResponse
? $this->responseFactory->json($signature, 200)
: $this->responseFactory->make($signature, 200, ['Content-Type' => 'text/plain']);
? $this->responseFactory->json($signature, 200)
: $this->responseFactory->make($signature, 200, ['Content-Type' => 'text/plain']);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Http/Controllers/Admin/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class LoginController extends Controller
*/
protected $viewFactory;

/**
* The path the user should be redirected to.
*
* @var string
*/
protected $redirectTo;

public function __construct(
Config $config,
AuthManager $authManager,
Expand Down
26 changes: 17 additions & 9 deletions src/Http/Controllers/Admin/MediaLibraryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use A17\Twill\Services\Uploader\SignS3Upload;
use A17\Twill\Services\Uploader\SignUploadListener;
use Illuminate\Config\Repository as Config;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -59,13 +58,22 @@ class MediaLibraryController extends ModuleController implements SignUploadListe
*/
protected $customFields;

/**
* @var Illuminate\Routing\ResponseFactory
*/
protected $responseFactory;

/**
* @var Illuminate\Config\Repository
*/
protected $config;

public function __construct(
Application $app,
Config $config,
Request $request,
ResponseFactory $responseFactory
)
{
) {
parent::__construct($app, $request);
$this->responseFactory = $responseFactory;
$this->config = $config;
Expand Down Expand Up @@ -224,7 +232,7 @@ public function bulkUpdate()
return is_null($meta);
})->toArray();

$extraMetadatas = array_diff_key($metadatasFromRequest, array_flip((array)$this->request->get('fieldsRemovedFromBulkEditing', [])));
$extraMetadatas = array_diff_key($metadatasFromRequest, array_flip((array) $this->request->get('fieldsRemovedFromBulkEditing', [])));

if (in_array('tags', $this->request->get('fieldsRemovedFromBulkEditing', []))) {
$this->repository->addIgnoreFieldsBeforeSave('bulk_tags');
Expand All @@ -235,9 +243,9 @@ public function bulkUpdate()

foreach ($ids as $id) {
$this->repository->update($id, [
'bulk_tags' => $newTags ?? [],
'previous_common_tags' => $previousCommonTags ?? [],
] + $extraMetadatas);
'bulk_tags' => $newTags ?? [],
'previous_common_tags' => $previousCommonTags ?? [],
] + $extraMetadatas);
}

$scopes = $this->filterScope(['id' => $ids]);
Expand Down Expand Up @@ -279,8 +287,8 @@ public function signAzureUpload(Request $request, SignAzureUpload $signAzureUplo
public function uploadIsSigned($signature, $isJsonResponse = true)
{
return $isJsonResponse
? $this->responseFactory->json($signature, 200)
: $this->responseFactory->make($signature, 200, ['Content-Type' => 'text/plain']);
? $this->responseFactory->json($signature, 200)
: $this->responseFactory->make($signature, 200, ['Content-Type' => 'text/plain']);
}

/**
Expand Down
51 changes: 48 additions & 3 deletions src/Http/Controllers/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ abstract class ModuleController extends Controller
*/
protected $request;

/**
* @var string
*/
protected $namespace;

/**
* @var string
*/
Expand All @@ -46,6 +51,11 @@ abstract class ModuleController extends Controller
*/
protected $modelName;

/**
* @var string
*/
protected $modelTitle;

/**
* @var \A17\Twill\Repositories\ModuleRepository
*/
Expand Down Expand Up @@ -173,6 +183,41 @@ abstract class ModuleController extends Controller
*/
protected $disableEditor = false;

/**
* @var array
*/
protected $indexOptions;

/**
* @var array
*/
protected $indexColumns;

/**
* @var array
*/
protected $browserColumns;

/**
* @var string
*/
protected $permalinkBase;

/**
* @var array
*/
protected $defaultFilters;

/**
* @var string
*/
protected $viewPrefix;

/**
* @var string
*/
protected $previewView;

/**
* List of permissions keyed by a request field. Can be used to prevent unauthorized field updates.
*
Expand Down Expand Up @@ -1024,7 +1069,7 @@ protected function getIndexUrls($moduleName, $routePrefix)
'feature',
'bulkFeature',
'bulkDelete',
])->mapWithKeys(function ($endpoint) use ($moduleName, $routePrefix) {
])->mapWithKeys(function ($endpoint) {
return [
$endpoint . 'Url' => $this->getIndexOption($endpoint) ? moduleRoute(
$this->moduleName, $this->routePrefix, $endpoint,
Expand Down Expand Up @@ -1096,7 +1141,7 @@ protected function getBrowserTableData($items)
$withImage = $this->moduleHas('medias');

return $items->map(function ($item) use ($withImage) {
$columnsData = Collection::make($this->browserColumns)->mapWithKeys(function ($column) use ($item, $withImage) {
$columnsData = Collection::make($this->browserColumns)->mapWithKeys(function ($column) use ($item) {
return $this->getItemColumnData($item, $column);
})->toArray();

Expand Down Expand Up @@ -1240,7 +1285,7 @@ protected function form($id, $item = null)
'permalinkPrefix' => $this->getPermalinkPrefix($baseUrl),
'saveUrl' => $this->getModuleRoute($item->id, 'update'),
'editor' => $this->moduleHas('revisions') && $this->moduleHas('blocks') && !$this->disableEditor,
'blockPreviewUrl' => Route::has('admin.blocks.preview')? URL::route('admin.blocks.preview') : '#',
'blockPreviewUrl' => Route::has('admin.blocks.preview') ? URL::route('admin.blocks.preview') : '#',
'revisions' => $this->moduleHas('revisions') ? $item->revisionsArray() : null,
] + (Route::has($previewRouteName) ? [
'previewUrl' => moduleRoute($this->moduleName, $this->routePrefix, 'preview', $item->id),
Expand Down
7 changes: 7 additions & 0 deletions src/Http/Controllers/Admin/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class ResetPasswordController extends Controller
*/
protected $config;

/**
* The path the user should be redirected to.
*
* @var string
*/
protected $redirectTo;

/**
* @var ViewFactory
*/
Expand Down
9 changes: 5 additions & 4 deletions src/Repositories/Behaviors/HandleBrowsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace A17\Twill\Repositories\Behaviors;

use A17\Twill\Models\Behaviors\HasMedias;
use Illuminate\Support\Str;

trait HandleBrowsers
{
/**
* All browsers used in the model, as an array of browser names:
* All browsers used in the model, as an array of browser names:
* [
* 'books',
* 'publications'
* ].
*
*
* When only the browser name is given here, its rest information will be inferred from the name.
* Each browser's detail can also be override with an array
* [
Expand Down Expand Up @@ -141,8 +142,8 @@ public function getFormFieldsForRelatedBrowser($object, $relation)
})->values()->toArray();
}

/**
* Get all browser' detail info from the $browsers attribute.
/**
* Get all browser' detail info from the $browsers attribute.
* The missing information will be inferred by convention of Twill.
*
* @return Illuminate\Support\Collection
Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/BlockRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function hydrate($object, $fields)
if (Schema::hasTable(config('twill.related_table', 'twill_related'))) {
$relatedItems = Collection::make();

Collection::make($fields['browsers'])->each(function ($items, $browserName) use ($object, &$relatedItems) {
Collection::make($fields['browsers'])->each(function ($items, $browserName) use (&$relatedItems) {
Collection::make($items)->each(function ($item) use ($browserName, &$relatedItems) {
try {
$repository = $this->getModelRepository($item['endpointType'] ?? $browserName);
Expand Down
Loading

0 comments on commit e1e6794

Please sign in to comment.