Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
  • Loading branch information
ellukesmith committed Oct 9, 2023
2 parents 7b8786c + 056db7f commit 17aac98
Show file tree
Hide file tree
Showing 24 changed files with 290 additions and 102 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Psalm

on:
workflow_dispatch:
push:
paths:
- '**.php'
- 'composer*'
- 'psalm*'

jobs:
psalm:
name: Psalm
runs-on: ubuntu-latest
timeout-minutes: 6
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

# mtime needs to be restored for Psalm cache to work correctly
- name: Restore mtimes
uses: chetan/git-restore-mtime-action@v1

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
coverage: none

- name: Install composer dependencies
run: |
composer config --ansi -- http-basic.nova.laravel.com ${{ secrets.NOVA_USERNAME }} ${{ secrets.NOVA_LICENSE_KEY }}
composer install --no-interaction --no-progress --no-scripts
# the way cache keys are set up will always cause a cache miss
# but will restore the cache generated during the previous run based on partial match
- name: Retrieve Psalm’s cache
uses: actions/cache@v3
with:
path: ./cache/psalm
key: ${{ runner.os }}-psalm-cache-${{ hashFiles('psalm.xml', 'psalm-baseline.xml', './composer.json') }}

- name: Run Psalm
run: ./vendor/bin/psalm --find-unused-psalm-suppress --output-format=github
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
],
"require": {
"php": "^7.3|^8.0",
"ext-json": "*",
"laravel/framework": "^8.0|^9.0|^10.0",
"laravel/nova": "^4.0",
"nova-kit/nova-packages-tool": "^1.3.1"
Expand Down Expand Up @@ -52,10 +53,13 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require-dev": {
"laravel/pint": "^1.2",
"phpunit/phpunit": "^9.5",
"laravel/pint": "^1.2"
"psalm/plugin-laravel": "^2.0"
},
"scripts": {
"psalm": "psalm --find-unused-psalm-suppress --output-format=phpstorm",
"psalm-update-baseline": "psalm --set-baseline=psalm-baseline.xml",
"test": "phpunit --colors=always tests",
"fix-style": "./vendor/bin/pint"
}
Expand Down
6 changes: 3 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ The `get` method is used to resolve the field's content. It is responsible to re
*
* @param mixed $resource
* @param string $attribute
* @param Whitecube\NovaFlexibleContent\Layouts\Collection $layouts
* @return Illuminate\Support\Collection
* @param \Whitecube\NovaFlexibleContent\Layouts\Collection $layouts
* @return \Illuminate\Support\Collection
*/
public function get($resource, $attribute, $layouts) {
$blocks = $resource->blocks()->orderBy('order')->get();
Expand All @@ -438,7 +438,7 @@ The `set` method is responsible for saving the Flexible's content somewhere the
*
* @param mixed $model
* @param string $attribute
* @param Illuminate\Support\Collection $groups
* @param \Illuminate\Support\Collection $groups
* @return void
*/
public function set($model, $attribute, $groups)
Expand Down
29 changes: 29 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.6.0@e784128902dfe01d489c4123d69918a9f3c1eac5">
<file src="src/FileAdder/FileAdder.php">
<UndefinedClass>
<code>OriginalFileAdder</code>
</UndefinedClass>
</file>
<file src="src/FileAdder/FileAdderFactory.php">
<UndefinedClass>
<code>OriginalFileAdderFactory</code>
</UndefinedClass>
</file>
<file src="src/Flexible.php">
<UndefinedClass>
<code>$model</code>
<code>\Whitecube\NovaPage\Pages\Template</code>
</UndefinedClass>
</file>
<file src="src/Layouts/Collection.php">
<UnimplementedInterfaceMethod>
<code>Collection</code>
</UnimplementedInterfaceMethod>
</file>
<file src="src/Layouts/Layout.php">
<UndefinedTrait>
<code>HasAttributes</code>
</UndefinedTrait>
</file>
</files>
20 changes: 20 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<psalm
errorLevel="7"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\LaravelPlugin\Plugin"/>
</plugins>
</psalm>
2 changes: 1 addition & 1 deletion src/Commands/CreateCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected function buildClass()
], [
$this->classname,
],
$this->files->get(__DIR__.'/../Stubs/Cast.php')
$this->files->get(__DIR__.'/../Stubs/Cast.stub')
);
}
}
2 changes: 1 addition & 1 deletion src/Commands/CreateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ protected function buildClass()
$this->classname,
$this->name,
],
$this->files->get(__DIR__.'/../Stubs/Layout.php')
$this->files->get(__DIR__.'/../Stubs/Layout.stub')
);
}
}
2 changes: 1 addition & 1 deletion src/Commands/CreatePreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function buildClass()
return str_replace(
':classname',
$this->classname,
$this->files->get(__DIR__.'/../Stubs/Preset.php')
$this->files->get(__DIR__.'/../Stubs/Preset.stub')
);
}
}
2 changes: 1 addition & 1 deletion src/Commands/CreateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected function buildClass()
return str_replace(
':classname',
$this->classname,
$this->files->get(__DIR__.'/../Stubs/Resolver.php')
$this->files->get(__DIR__.'/../Stubs/Resolver.stub')
);
}
}
15 changes: 8 additions & 7 deletions src/Concerns/HasFlexible.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection as BaseCollection;
use Laravel\Nova\NovaServiceProvider;
use Laravel\Nova\Support\Fluent;
use Whitecube\NovaFlexibleContent\Layouts\Collection;
use Whitecube\NovaFlexibleContent\Layouts\Layout;
use Whitecube\NovaFlexibleContent\Value\FlexibleCast;
Expand All @@ -27,7 +28,7 @@ public function flexible($attribute, $layoutMapping = [])
/**
* Cast a Flexible Content value
*
* @param array $value
* @param mixed $value
* @param array $layoutMapping
* @return \Whitecube\NovaFlexibleContent\Layouts\Collection
*/
Expand All @@ -43,7 +44,7 @@ public function cast($value, $layoutMapping = [])
/**
* Parse a Flexible Content from value
*
* @param mixed $value
* @param array|string|\Illuminate\Support\Collection|null $value
* @param array $layoutMapping
* @return \Whitecube\NovaFlexibleContent\Layouts\Collection
*/
Expand All @@ -63,7 +64,7 @@ public function toFlexible($value, $layoutMapping = [])
/**
* Transform incoming value into an array of usable layouts
*
* @param mixed $value
* @param array|string|\Illuminate\Support\Collection|null $value
* @return array|null
*/
protected function getFlexibleArrayFromValue($value)
Expand Down Expand Up @@ -104,7 +105,7 @@ protected function getMappedFlexibleLayouts(array $flexible, array $layoutMappin
*
* @param mixed $item
* @param array $layoutMapping
* @return null|Whitecube\NovaFlexibleContent\Layouts\LayoutInterface
* @return null|\Whitecube\NovaFlexibleContent\Layouts\LayoutInterface
*/
protected function getMappedLayout($item, array $layoutMapping)
{
Expand All @@ -119,8 +120,8 @@ protected function getMappedLayout($item, array $layoutMapping)
if (is_array($item)) {
$name = $item['layout'] ?? null;
$key = $item['key'] ?? null;
$attributes = (array) $item['attributes'] ?? [];
} elseif (is_a($item, \stdClass::class)) {
$attributes = (array) ($item['attributes'] ?? []);
} elseif (is_a($item, \stdClass::class) || is_a($item, Fluent::class)) {
$name = $item->layout ?? null;
$key = $item->key ?? null;
$attributes = (array) ($item->attributes ?? []);
Expand All @@ -131,7 +132,7 @@ protected function getMappedLayout($item, array $layoutMapping)
}

if (is_null($name)) {
return;
return null;
}

return $this->createMappedLayout($name, $key, $attributes, $layoutMapping);
Expand Down
41 changes: 21 additions & 20 deletions src/Flexible.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Whitecube\NovaFlexibleContent;

use Illuminate\Support\Collection;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Fields\SupportsDependentFields;
use Laravel\Nova\Http\Requests\NovaRequest;
Expand All @@ -27,21 +28,21 @@ class Flexible extends Field
/**
* The available layouts collection
*
* @var Whitecube\NovaFlexibleContent\Layouts\Collection
* @var \Whitecube\NovaFlexibleContent\Layouts\Collection
*/
protected $layouts;

/**
* The currently defined layout groups
*
* @var Illuminate\Support\Collection
* @var \Illuminate\Support\Collection
*/
protected $groups;

/**
* The field's value setter & getter
*
* @var Whitecube\NovaFlexibleContent\Value\ResolverInterface
* @var \Whitecube\NovaFlexibleContent\Value\ResolverInterface
*/
protected $resolver;

Expand Down Expand Up @@ -229,7 +230,7 @@ public function collapsed(bool $value = true)
/**
* Push a layout instance into the layouts collection
*
* @param Whitecube\NovaFlexibleContent\Layouts\LayoutInterface $layout
* @param \Whitecube\NovaFlexibleContent\Layouts\LayoutInterface $layout
* @return void
*/
protected function registerLayout(LayoutInterface $layout)
Expand Down Expand Up @@ -301,7 +302,7 @@ public function isShownOnDetail(NovaRequest $request, $resource): bool
* @param string $requestAttribute
* @param object $model
* @param string $attribute
* @return null|Closure
* @return void|\Closure
*/
protected function fillAttribute(NovaRequest $request, $requestAttribute, $model, $attribute)
{
Expand Down Expand Up @@ -335,26 +336,26 @@ protected function fillAttribute(NovaRequest $request, $requestAttribute, $model
* @param string $requestAttribute
* @return array
*/
protected function syncAndFillGroups(NovaRequest $request, $requestAttribute)
protected function syncAndFillGroups(NovaRequest $request, $requestAttribute): array
{
if (! ($raw = $this->extractValue($request, $requestAttribute))) {
$this->fireRemoveCallbacks(collect());
$this->groups = collect();

return;
return [];
}

$callbacks = [];

$new_groups = collect($raw)->map(function ($item, $key) use ($request, &$callbacks) {
$new_groups = collect($raw)->map(function ($item) use ($request, &$callbacks) {
$layout = $item['layout'];
$key = $item['key'];
$attributes = $item['attributes'];

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

if (! $group) {
return;
if (! $group instanceof Layout) {
return [];
}

$scope = ScopedRequest::scopeFrom($request, $attributes, $key);
Expand All @@ -373,9 +374,9 @@ protected function syncAndFillGroups(NovaRequest $request, $requestAttribute)
/**
* Fire's the remove callbacks on the layouts
*
* @param $new_groups This should be (all) the new groups to bne compared against to find the removed groups
* @param Collection $new_groups This should be (all) the new groups to bne compared against to find the removed groups
*/
protected function fireRemoveCallbacks($new_groups)
protected function fireRemoveCallbacks(Collection $new_groups)
{
$new_group_keys = $new_groups->map(function ($item) {
return $item->inUseKey();
Expand Down Expand Up @@ -414,8 +415,8 @@ protected function extractValue(NovaRequest $request, $attribute)
/**
* Resolve all contained groups and their fields
*
* @param Illuminate\Support\Collection $groups
* @return Illuminate\Support\Collection
* @param \Illuminate\Support\Collection $groups
* @return \Illuminate\Support\Collection
*/
protected function resolveGroups($groups)
{
Expand All @@ -428,8 +429,8 @@ protected function resolveGroups($groups)
* Resolve all contained groups and their fields for display on index and
* detail views.
*
* @param Illuminate\Support\Collection $groups
* @return Illuminate\Support\Collection
* @param \Illuminate\Support\Collection $groups
* @return \Illuminate\Support\Collection
*/
protected function resolveGroupsForDisplay($groups)
{
Expand All @@ -444,7 +445,7 @@ protected function resolveGroupsForDisplay($groups)
*
* @param mixed $resource
* @param string $attribute
* @return Illuminate\Support\Collection
* @return \Illuminate\Support\Collection
*/
protected function buildGroups($resource, $attribute)
{
Expand Down Expand Up @@ -473,14 +474,14 @@ protected function findGroup($key)
*
* @param string $layout
* @param string $key
* @return \Whitecube\NovaFlexibleContent\Layouts\Layout
* @return null|\Whitecube\NovaFlexibleContent\Layouts\Layout
*/
protected function newGroup($layout, $key)
{
$layout = $this->layouts->find($layout);

if (! $layout) {
return;
if (! $layout instanceof Layout) {
return null;
}

return $layout->duplicate($key);
Expand Down
1 change: 0 additions & 1 deletion src/Http/FlexibleAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ public function nest($key)
/**
* Check attribute is an "upload" attribute and define it on the object
*
* @param mixed $group
* @return void
*/
protected function setUpload()
Expand Down
Loading

0 comments on commit 17aac98

Please sign in to comment.