Skip to content

Commit 52011f8

Browse files
authored
Merge branch 'whitecube:master' into collapsed_preview
2 parents 7dd393a + 056db7f commit 52011f8

24 files changed

+291
-103
lines changed

.github/workflows/psalm.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Psalm
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
paths:
7+
- '**.php'
8+
- 'composer*'
9+
- 'psalm*'
10+
11+
jobs:
12+
psalm:
13+
name: Psalm
14+
runs-on: ubuntu-latest
15+
timeout-minutes: 6
16+
steps:
17+
- uses: actions/checkout@v3
18+
with:
19+
ref: ${{ github.head_ref }}
20+
21+
# mtime needs to be restored for Psalm cache to work correctly
22+
- name: Restore mtimes
23+
uses: chetan/git-restore-mtime-action@v1
24+
25+
- name: Setup PHP
26+
uses: shivammathur/setup-php@v2
27+
with:
28+
php-version: 8.1
29+
coverage: none
30+
31+
- name: Install composer dependencies
32+
run: |
33+
composer config --ansi -- http-basic.nova.laravel.com ${{ secrets.NOVA_USERNAME }} ${{ secrets.NOVA_LICENSE_KEY }}
34+
composer install --no-interaction --no-progress --no-scripts
35+
36+
# the way cache keys are set up will always cause a cache miss
37+
# but will restore the cache generated during the previous run based on partial match
38+
- name: Retrieve Psalm’s cache
39+
uses: actions/cache@v3
40+
with:
41+
path: ./cache/psalm
42+
key: ${{ runner.os }}-psalm-cache-${{ hashFiles('psalm.xml', 'psalm-baseline.xml', './composer.json') }}
43+
44+
- name: Run Psalm
45+
run: ./vendor/bin/psalm --find-unused-psalm-suppress --output-format=github

composer.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
],
2020
"require": {
2121
"php": "^7.3|^8.0",
22-
"laravel/framework": "^8.0|^9.0",
22+
"ext-json": "*",
23+
"laravel/framework": "^8.0|^9.0|^10.0",
2324
"laravel/nova": "^4.0",
2425
"nova-kit/nova-packages-tool": "^1.3.1"
2526
},
@@ -52,10 +53,13 @@
5253
"minimum-stability": "dev",
5354
"prefer-stable": true,
5455
"require-dev": {
56+
"laravel/pint": "^1.2",
5557
"phpunit/phpunit": "^9.5",
56-
"laravel/pint": "^1.2"
58+
"psalm/plugin-laravel": "^2.0"
5759
},
5860
"scripts": {
61+
"psalm": "psalm --find-unused-psalm-suppress --output-format=phpstorm",
62+
"psalm-update-baseline": "psalm --set-baseline=psalm-baseline.xml",
5963
"test": "phpunit --colors=always tests",
6064
"fix-style": "./vendor/bin/pint"
6165
}

docs/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ The `get` method is used to resolve the field's content. It is responsible to re
412412
*
413413
* @param mixed $resource
414414
* @param string $attribute
415-
* @param Whitecube\NovaFlexibleContent\Layouts\Collection $layouts
416-
* @return Illuminate\Support\Collection
415+
* @param \Whitecube\NovaFlexibleContent\Layouts\Collection $layouts
416+
* @return \Illuminate\Support\Collection
417417
*/
418418
public function get($resource, $attribute, $layouts) {
419419
$blocks = $resource->blocks()->orderBy('order')->get();
@@ -438,7 +438,7 @@ The `set` method is responsible for saving the Flexible's content somewhere the
438438
*
439439
* @param mixed $model
440440
* @param string $attribute
441-
* @param Illuminate\Support\Collection $groups
441+
* @param \Illuminate\Support\Collection $groups
442442
* @return void
443443
*/
444444
public function set($model, $attribute, $groups)

psalm-baseline.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<files psalm-version="5.6.0@e784128902dfe01d489c4123d69918a9f3c1eac5">
3+
<file src="src/FileAdder/FileAdder.php">
4+
<UndefinedClass>
5+
<code>OriginalFileAdder</code>
6+
</UndefinedClass>
7+
</file>
8+
<file src="src/FileAdder/FileAdderFactory.php">
9+
<UndefinedClass>
10+
<code>OriginalFileAdderFactory</code>
11+
</UndefinedClass>
12+
</file>
13+
<file src="src/Flexible.php">
14+
<UndefinedClass>
15+
<code>$model</code>
16+
<code>\Whitecube\NovaPage\Pages\Template</code>
17+
</UndefinedClass>
18+
</file>
19+
<file src="src/Layouts/Collection.php">
20+
<UnimplementedInterfaceMethod>
21+
<code>Collection</code>
22+
</UnimplementedInterfaceMethod>
23+
</file>
24+
<file src="src/Layouts/Layout.php">
25+
<UndefinedTrait>
26+
<code>HasAttributes</code>
27+
</UndefinedTrait>
28+
</file>
29+
</files>

psalm.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="7"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
findUnusedBaselineEntry="true"
9+
errorBaseline="psalm-baseline.xml"
10+
>
11+
<projectFiles>
12+
<directory name="src"/>
13+
<ignoreFiles>
14+
<directory name="vendor"/>
15+
</ignoreFiles>
16+
</projectFiles>
17+
<plugins>
18+
<pluginClass class="Psalm\LaravelPlugin\Plugin"/>
19+
</plugins>
20+
</psalm>

src/Commands/CreateCast.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected function buildClass()
117117
], [
118118
$this->classname,
119119
],
120-
$this->files->get(__DIR__.'/../Stubs/Cast.php')
120+
$this->files->get(__DIR__.'/../Stubs/Cast.stub')
121121
);
122122
}
123123
}

src/Commands/CreateLayout.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ protected function buildClass()
141141
$this->classname,
142142
$this->name,
143143
],
144-
$this->files->get(__DIR__.'/../Stubs/Layout.php')
144+
$this->files->get(__DIR__.'/../Stubs/Layout.stub')
145145
);
146146
}
147147
}

src/Commands/CreatePreset.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected function buildClass()
115115
return str_replace(
116116
':classname',
117117
$this->classname,
118-
$this->files->get(__DIR__.'/../Stubs/Preset.php')
118+
$this->files->get(__DIR__.'/../Stubs/Preset.stub')
119119
);
120120
}
121121
}

src/Commands/CreateResolver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected function buildClass()
115115
return str_replace(
116116
':classname',
117117
$this->classname,
118-
$this->files->get(__DIR__.'/../Stubs/Resolver.php')
118+
$this->files->get(__DIR__.'/../Stubs/Resolver.stub')
119119
);
120120
}
121121
}

src/Concerns/HasFlexible.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Collection as BaseCollection;
66
use Laravel\Nova\NovaServiceProvider;
7+
use Laravel\Nova\Support\Fluent;
78
use Whitecube\NovaFlexibleContent\Layouts\Collection;
89
use Whitecube\NovaFlexibleContent\Layouts\Layout;
910
use Whitecube\NovaFlexibleContent\Value\FlexibleCast;
@@ -27,7 +28,7 @@ public function flexible($attribute, $layoutMapping = [])
2728
/**
2829
* Cast a Flexible Content value
2930
*
30-
* @param array $value
31+
* @param mixed $value
3132
* @param array $layoutMapping
3233
* @return \Whitecube\NovaFlexibleContent\Layouts\Collection
3334
*/
@@ -43,7 +44,7 @@ public function cast($value, $layoutMapping = [])
4344
/**
4445
* Parse a Flexible Content from value
4546
*
46-
* @param mixed $value
47+
* @param array|string|\Illuminate\Support\Collection|null $value
4748
* @param array $layoutMapping
4849
* @return \Whitecube\NovaFlexibleContent\Layouts\Collection
4950
*/
@@ -63,7 +64,7 @@ public function toFlexible($value, $layoutMapping = [])
6364
/**
6465
* Transform incoming value into an array of usable layouts
6566
*
66-
* @param mixed $value
67+
* @param array|string|\Illuminate\Support\Collection|null $value
6768
* @return array|null
6869
*/
6970
protected function getFlexibleArrayFromValue($value)
@@ -104,7 +105,7 @@ protected function getMappedFlexibleLayouts(array $flexible, array $layoutMappin
104105
*
105106
* @param mixed $item
106107
* @param array $layoutMapping
107-
* @return null|Whitecube\NovaFlexibleContent\Layouts\LayoutInterface
108+
* @return null|\Whitecube\NovaFlexibleContent\Layouts\LayoutInterface
108109
*/
109110
protected function getMappedLayout($item, array $layoutMapping)
110111
{
@@ -119,8 +120,8 @@ protected function getMappedLayout($item, array $layoutMapping)
119120
if (is_array($item)) {
120121
$name = $item['layout'] ?? null;
121122
$key = $item['key'] ?? null;
122-
$attributes = (array) $item['attributes'] ?? [];
123-
} elseif (is_a($item, \stdClass::class)) {
123+
$attributes = (array) ($item['attributes'] ?? []);
124+
} elseif (is_a($item, \stdClass::class) || is_a($item, Fluent::class)) {
124125
$name = $item->layout ?? null;
125126
$key = $item->key ?? null;
126127
$attributes = (array) ($item->attributes ?? []);
@@ -131,7 +132,7 @@ protected function getMappedLayout($item, array $layoutMapping)
131132
}
132133

133134
if (is_null($name)) {
134-
return;
135+
return null;
135136
}
136137

137138
return $this->createMappedLayout($name, $key, $attributes, $layoutMapping);

src/Flexible.php

+21-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Whitecube\NovaFlexibleContent;
44

5+
use Illuminate\Support\Collection;
56
use Laravel\Nova\Fields\Field;
67
use Laravel\Nova\Fields\SupportsDependentFields;
78
use Laravel\Nova\Http\Requests\NovaRequest;
@@ -27,21 +28,21 @@ class Flexible extends Field
2728
/**
2829
* The available layouts collection
2930
*
30-
* @var Whitecube\NovaFlexibleContent\Layouts\Collection
31+
* @var \Whitecube\NovaFlexibleContent\Layouts\Collection
3132
*/
3233
protected $layouts;
3334

3435
/**
3536
* The currently defined layout groups
3637
*
37-
* @var Illuminate\Support\Collection
38+
* @var \Illuminate\Support\Collection
3839
*/
3940
protected $groups;
4041

4142
/**
4243
* The field's value setter & getter
4344
*
44-
* @var Whitecube\NovaFlexibleContent\Value\ResolverInterface
45+
* @var \Whitecube\NovaFlexibleContent\Value\ResolverInterface
4546
*/
4647
protected $resolver;
4748

@@ -229,7 +230,7 @@ public function collapsed(bool $value = true)
229230
/**
230231
* Push a layout instance into the layouts collection
231232
*
232-
* @param Whitecube\NovaFlexibleContent\Layouts\LayoutInterface $layout
233+
* @param \Whitecube\NovaFlexibleContent\Layouts\LayoutInterface $layout
233234
* @return void
234235
*/
235236
protected function registerLayout(LayoutInterface $layout)
@@ -301,7 +302,7 @@ public function isShownOnDetail(NovaRequest $request, $resource): bool
301302
* @param string $requestAttribute
302303
* @param object $model
303304
* @param string $attribute
304-
* @return null|Closure
305+
* @return void|\Closure
305306
*/
306307
protected function fillAttribute(NovaRequest $request, $requestAttribute, $model, $attribute)
307308
{
@@ -335,26 +336,26 @@ protected function fillAttribute(NovaRequest $request, $requestAttribute, $model
335336
* @param string $requestAttribute
336337
* @return array
337338
*/
338-
protected function syncAndFillGroups(NovaRequest $request, $requestAttribute)
339+
protected function syncAndFillGroups(NovaRequest $request, $requestAttribute): array
339340
{
340341
if (! ($raw = $this->extractValue($request, $requestAttribute))) {
341342
$this->fireRemoveCallbacks(collect());
342343
$this->groups = collect();
343344

344-
return;
345+
return [];
345346
}
346347

347348
$callbacks = [];
348349

349-
$new_groups = collect($raw)->map(function ($item, $key) use ($request, &$callbacks) {
350+
$new_groups = collect($raw)->map(function ($item) use ($request, &$callbacks) {
350351
$layout = $item['layout'];
351352
$key = $item['key'];
352353
$attributes = $item['attributes'];
353354

354355
$group = $this->findGroup($key) ?? $this->newGroup($layout, $key);
355356

356-
if (! $group) {
357-
return;
357+
if (! $group instanceof Layout) {
358+
return [];
358359
}
359360

360361
$scope = ScopedRequest::scopeFrom($request, $attributes, $key);
@@ -373,9 +374,9 @@ protected function syncAndFillGroups(NovaRequest $request, $requestAttribute)
373374
/**
374375
* Fire's the remove callbacks on the layouts
375376
*
376-
* @param $new_groups This should be (all) the new groups to bne compared against to find the removed groups
377+
* @param Collection $new_groups This should be (all) the new groups to bne compared against to find the removed groups
377378
*/
378-
protected function fireRemoveCallbacks($new_groups)
379+
protected function fireRemoveCallbacks(Collection $new_groups)
379380
{
380381
$new_group_keys = $new_groups->map(function ($item) {
381382
return $item->inUseKey();
@@ -414,8 +415,8 @@ protected function extractValue(NovaRequest $request, $attribute)
414415
/**
415416
* Resolve all contained groups and their fields
416417
*
417-
* @param Illuminate\Support\Collection $groups
418-
* @return Illuminate\Support\Collection
418+
* @param \Illuminate\Support\Collection $groups
419+
* @return \Illuminate\Support\Collection
419420
*/
420421
protected function resolveGroups($groups)
421422
{
@@ -428,8 +429,8 @@ protected function resolveGroups($groups)
428429
* Resolve all contained groups and their fields for display on index and
429430
* detail views.
430431
*
431-
* @param Illuminate\Support\Collection $groups
432-
* @return Illuminate\Support\Collection
432+
* @param \Illuminate\Support\Collection $groups
433+
* @return \Illuminate\Support\Collection
433434
*/
434435
protected function resolveGroupsForDisplay($groups)
435436
{
@@ -444,7 +445,7 @@ protected function resolveGroupsForDisplay($groups)
444445
*
445446
* @param mixed $resource
446447
* @param string $attribute
447-
* @return Illuminate\Support\Collection
448+
* @return \Illuminate\Support\Collection
448449
*/
449450
protected function buildGroups($resource, $attribute)
450451
{
@@ -473,14 +474,14 @@ protected function findGroup($key)
473474
*
474475
* @param string $layout
475476
* @param string $key
476-
* @return \Whitecube\NovaFlexibleContent\Layouts\Layout
477+
* @return null|\Whitecube\NovaFlexibleContent\Layouts\Layout
477478
*/
478479
protected function newGroup($layout, $key)
479480
{
480481
$layout = $this->layouts->find($layout);
481482

482-
if (! $layout) {
483-
return;
483+
if (! $layout instanceof Layout) {
484+
return null;
484485
}
485486

486487
return $layout->duplicate($key);

src/Http/FlexibleAttribute.php

-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ public function nest($key)
262262
/**
263263
* Check attribute is an "upload" attribute and define it on the object
264264
*
265-
* @param mixed $group
266265
* @return void
267266
*/
268267
protected function setUpload()

0 commit comments

Comments
 (0)