Skip to content

Commit

Permalink
Merge pull request #165 from conedevelopment/prepare-phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo authored Jan 17, 2024
2 parents 12b6511 + 3b88305 commit cd61916
Show file tree
Hide file tree
Showing 31 changed files with 140 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/js-tests export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/public/build/assets/ linguist-generated
/public/build/assets/** linguist-generated
/tests export-ignore
/yarn.lock export-ignore linguist-generated
/vite.config.js export-ignore
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/back-end.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ jobs:
name: "Search for $this->$this typo 🐌"
run: |
! git grep --line-number -e '\$this\s*->\s*\$this' -- ':!:*/back-end\.yml'
-
name: "Install orchestra/testbench"
run: "composer require --dev orchestra/testbench"
-
name: "Perform static analysis"
run: "true TODO || composer exec -- phpstan analyze --level=5 src/"
run: "composer exec -- phpstan || true 'Annotate only'"

coding_standards:
name: "4️⃣ Coding Standards"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable-integrity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ jobs:
wget --secure-protocol=TLSv1_3 --max-redirect=1 --retry-on-host-error --retry-connrefused --tries=3 \
--no-verbose --output-document="${{ runner.temp }}/cloc/cloc" "${RELEASE_ASSET_URL}"
{
git ls-files -- ':!:LICENSE' ':!:package-lock.json' >"${{ runner.temp }}/cloc/include-list"
git ls-files -- ':!:LICENSE' ':!:yarn.lock' >"${{ runner.temp }}/cloc/include-list"
echo '```'
perl "${{ runner.temp }}/cloc/cloc" --hide-rate \
--list-file="${{ runner.temp }}/cloc/include-list" \
Expand Down
2 changes: 1 addition & 1 deletion database/factories/MediumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MediumFactory extends Factory
/**
* The name of the factory's corresponding model.
*
* @var string
* @var class-string<\Cone\Root\Models\Medium>
*/
protected $model = Medium::class;

Expand Down
2 changes: 1 addition & 1 deletion database/factories/MetaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MetaFactory extends Factory
/**
* The name of the factory's corresponding model.
*
* @var string
* @var class-string<\Cone\Root\Models\Meta>
*/
protected $model = Meta::class;

Expand Down
2 changes: 1 addition & 1 deletion database/factories/NotificationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class NotificationFactory extends Factory
/**
* The name of the factory's corresponding model.
*
* @var string
* @var class-string<\Cone\Root\Models\Notification>
*/
protected $model = Notification::class;

Expand Down
18 changes: 18 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,29 @@ parameters:
databaseMigrationsPath:
- database/migrations/
- vendor/orchestra/testbench-core/laravel/migrations/
scanFiles:
- tests/phpstan/FieldWithRoute.php
- tests/phpstan/MetaDataModel.php
paths:
- src/
- config/
- database/
- routes/
level: 5
checkAlwaysTrueCheckTypeFunctionCall: true
checkAlwaysTrueInstanceof: true
checkAlwaysTrueStrictComparison: true
checkAlwaysTrueLooseComparison: true
checkClassCaseSensitivity: false
checkDynamicProperties: true
checkExplicitMixed: false
checkImplicitMixed: false
checkExplicitMixedMissingReturn: true
checkFunctionNameCase: true
checkInternalClassCaseSensitivity: true
ignoreErrors:
- '#^Unsafe usage of new static#'
# Illuminate\View\View vs. Illuminate\Contracts\View\View
- '#^Method Cone\\\S+::render\(\) should return Illuminate\\View\\View but returns Illuminate\\Contracts\\View\\View\.$#'
# SoftDeletes
- '#(\$forceDeleting|::withTrashed|::onlyTrashed|::trashed|::restore)#'
5 changes: 5 additions & 0 deletions src/Fields/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Illuminate\Http\Request;
use Illuminate\Routing\Router;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\BelongsTo
*
* @extends \Cone\Root\Fields\Relation<TRelation>
*/
class BelongsTo extends Relation
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
use Illuminate\Routing\Router;
use Illuminate\Support\Arr;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\BelongsToMany
*
* @extends \Cone\Root\Fields\Relation<TRelation>
*/
class BelongsToMany extends Relation
{
/**
Expand Down
1 change: 1 addition & 0 deletions src/Fields/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public function registerRoutes(Request $request, Router $router): void
$router->prefix('fields')->group(function (Router $router) use ($request): void {
$this->each(static function (Field $field) use ($request, $router): void {
if (in_array(RegistersRoutes::class, class_uses_recursive($field))) {
/** @var \Tests\FieldWithRoute $field */
$field->registerRoutes($request, $router);
}
});
Expand Down
7 changes: 6 additions & 1 deletion src/Fields/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ protected function stored(Request $request, Model $model, string $path): array
call_user_func_array($this->storageResolver, [$request, $medium, $path]);
}

$request->user()->uploads()->save($medium);
/** @var \Illuminate\Foundation\Auth\User&\Cone\Root\Interfaces\Models\User $user */
$user = $request->user();
$user->uploads()->save($medium);

MoveFile::withChain($medium->convertible() ? [new PerformConversions($medium)] : [])
->dispatch($medium, $path, false);
Expand Down Expand Up @@ -234,6 +236,7 @@ public function prune(Request $request, Model $model, array $keys): int
*/
public function toOption(Request $request, Model $model, Model $related): array
{
/** @var \Cone\Root\Models\Medium $related */
$option = parent::toOption($request, $model, $related);

$name = sprintf(
Expand All @@ -245,6 +248,8 @@ public function toOption(Request $request, Model $model, Model $related): array

$option['attrs']->merge(['name' => $name]);

/** @var \Cone\Root\Models\Medium $related */

return array_merge($option, [
'fileName' => $related->file_name,
'isImage' => $related->isImage,
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany as EloquentRelation;

/**
* @extends \Cone\Root\Fields\HasOneOrMany<\Illuminate\Database\Eloquent\Relations\HasMany>
*/
class HasMany extends HasOneOrMany
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne as EloquentRelation;

/**
* @extends \Cone\Root\Fields\HasOneOrMany<\Illuminate\Database\Eloquent\Relations\HasOne>
*/
class HasOne extends HasOneOrMany
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\HasOneOrMany
*
* @extends \Cone\Root\Fields\Relation<TRelation>
*/
abstract class HasOneOrMany extends Relation
{
/**
Expand Down
4 changes: 4 additions & 0 deletions src/Fields/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Illuminate\Database\Eloquent\Relations\MorphOne as EloquentRelation;
use Illuminate\Http\Request;

/**
* @extends \Cone\Root\Fields\MorphOne<\Illuminate\Database\Eloquent\Relations\MorphOne>
*/
class Meta extends MorphOne
{
/**
Expand All @@ -21,6 +24,7 @@ class Meta extends MorphOne
public function __construct(string $label, Closure|string|null $modelAttribute = null, Closure|string|null $relation = null)
{
$relation ??= function (Model $model): EloquentRelation {
/** @phpstan-var \Tests\MetaDataModel $model */
$related = $model->metaData()->make();

return $model->metaData()
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany as EloquentRelation;

/**
* @extends \Cone\Root\Fields\MorphOneOrMany<\Illuminate\Database\Eloquent\Relations\MorphMany>
*/
class MorphMany extends MorphOneOrMany
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/MorphOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOne as EloquentRelation;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\MorphOne
*
* @extends \Cone\Root\Fields\MorphOneOrMany<TRelation>
*/
class MorphOne extends MorphOneOrMany
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/MorphOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany as EloquentRelation;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\MorphOneOrMany
*
* @extends \Cone\Root\Fields\HasOneOrMany<TRelation>
*/
abstract class MorphOneOrMany extends HasOneOrMany
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Illuminate\Routing\Router;
use Illuminate\Support\Str;

/**
* @extends \Cone\Root\Fields\BelongsTo<\Illuminate\Database\Eloquent\Relations\MorphTo>
*/
class MorphTo extends BelongsTo
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Illuminate\Database\Eloquent\Relations\MorphToMany as EloquentRelation;
use Illuminate\Http\Request;

/**
* @extends \Cone\Root\Fields\BelongsToMany<\Illuminate\Database\Eloquent\Relations\MorphToMany>
*/
class MorphToMany extends BelongsToMany
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
use Illuminate\Support\MessageBag;
use Illuminate\Support\Str;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\Relation
*/
abstract class Relation extends Field implements Form
{
use AsForm;
Expand Down Expand Up @@ -118,6 +121,8 @@ public static function scopeQuery(Closure $callback): void

/**
* Get the relation instance.
*
* @phpstan-return TRelation
*/
public function getRelation(Model $model): EloquentRelation
{
Expand Down
1 change: 1 addition & 0 deletions src/Http/Controllers/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ActionController extends Controller
*/
public function __invoke(Request $request): Response
{
/** @var \Cone\Root\Actions\Action $action */
$action = $request->route('action');

Gate::allowIf($action->authorized($request));
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Controllers/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class MediaController extends Controller
*/
public function __invoke(Request $request): JsonResponse
{
/** @var \Cone\Root\Resources\Resource $resource */
$resource = $request->route('_resource');

/** @var \Cone\Root\Fields\Media $field */
$field = $request->route('field');

$model = $request->filled('model')
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Controllers/RepeaterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class RepeaterController extends Controller
*/
public function __invoke(Request $request): JsonResponse
{
/** @var \Cone\Root\Resources\Resource $resource */
$resource = $request->route('_resource');

/** @var \Cone\Root\Fields\Repeater $field */
$field = $request->route('field');

$model = $request->filled('model')
Expand Down
1 change: 1 addition & 0 deletions src/Http/Controllers/WidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class WidgetController extends Controller
*/
public function __invoke(Request $request): Response
{
/** @var \Cone\Root\Widgets\Widget $widget */
$widget = $request->route('widget');

$data = $widget->data($request);
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public function user(): BelongsTo

/**
* Determine if the file is image.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<bool, never>
*/
protected function isImage(): Attribute
{
Expand All @@ -172,6 +174,8 @@ protected function isImage(): Attribute

/**
* Get the conversion URLs.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<array<string, string>, never>
*/
protected function urls(): Attribute
{
Expand All @@ -190,6 +194,8 @@ function (array $urls, string $conversion): array {

/**
* Get the formatted size attribute.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
*/
protected function formattedSize(): Attribute
{
Expand All @@ -210,6 +216,8 @@ protected function formattedSize(): Attribute

/**
* Get the dimensions attribute.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<string|null, never>
*/
protected function dimensions(): Attribute
{
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function getMorphClass(): string

/**
* Get the subject attribute.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<string|null, never>
*/
protected function subject(): Attribute
{
Expand All @@ -72,6 +74,8 @@ protected function subject(): Attribute

/**
* Get the formatted created at attribute.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<string|null, never>
*/
protected function formattedCreatedAt(): Attribute
{
Expand All @@ -82,6 +86,8 @@ protected function formattedCreatedAt(): Attribute

/**
* Get the formatted created at attribute.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<bool, never>
*/
protected function isRead(): Attribute
{
Expand All @@ -92,6 +98,8 @@ protected function isRead(): Attribute

/**
* Get the URL attribute.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<string|null, never>
*/
protected function url(): Attribute
{
Expand Down
6 changes: 6 additions & 0 deletions src/Navigation/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
use Cone\Root\Traits\Makeable;
use Illuminate\Support\Facades\URL;

/**
* @property string $url
* @property string $label
* @property string $icon
* @property string $group
*/
class Item
{
use HasAttributes;
Expand Down
Loading

0 comments on commit cd61916

Please sign in to comment.