Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds duster as the linter #121

Merged
merged 8 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/duster-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Duster Lint

on:
push:
branches: [ main ]
pull_request:

jobs:
duster:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: "Duster Lint"
uses: tighten/duster-action@v2
with:
args: lint
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
- name: Install Dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "symfony/console:>=4.3.4" --no-interaction --no-update
composer remove "tightenco/duster" --dev --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute Tests
run: ./vendor/bin/phpunit --testdox
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"require-dev": {
"orchestra/testbench": "^7.0||^8.0",
"phpunit/phpunit": "^9.5.10||^10.0"
"phpunit/phpunit": "^9.5.10||^10.0",
"tightenco/duster": "^2.7"
},
"autoload": {
"psr-4": {
Expand All @@ -35,5 +36,9 @@
"Parental\\Tests\\": "tests/",
"Database\\Factories\\": "tests/factories/"
}
},
"scripts": {
"lint": "vendor/bin/duster lint",
"fix": "vendor/bin/duster fix"
}
}
57 changes: 18 additions & 39 deletions src/HasChildren.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;
use UnitEnum;

trait HasChildren
{
Expand All @@ -24,8 +25,7 @@ trait HasChildren
* Register a model event with the dispatcher.
*
* @param string $event
* @param Closure|string $callback
* @return void
* @param Closure|string $callback
*/
protected static function registerModelEvent($event, $callback): void
{
Expand All @@ -46,16 +46,13 @@ protected static function registerModelEvent($event, $callback): void
}
}

/**
* @return bool
*/
protected static function parentIsBooting(): bool
{
if (! isset(self::$parentBootMethods)) {
self::$parentBootMethods[] = 'boot';

foreach (class_uses_recursive(self::class) as $trait) {
self::$parentBootMethods[] = 'boot'.class_basename($trait);
self::$parentBootMethods[] = 'boot' . class_basename($trait);
}

self::$parentBootMethods = array_flip(self::$parentBootMethods);
Expand Down Expand Up @@ -131,14 +128,13 @@ public function newFromBuilder($attributes = [], $connection = null): self
* @param string|null $foreignKey
* @param string|null $ownerKey
* @param string|null $relation
* @return BelongsTo
*/
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null): BelongsTo
{
$instance = $this->newRelatedInstance($related);

if (is_null($foreignKey) && $instance->hasParent) {
$foreignKey = Str::snake($instance->getClassNameForRelationships()).'_'.$instance->getKeyName();
$foreignKey = Str::snake($instance->getClassNameForRelationships()) . '_' . $instance->getKeyName();
}

if (is_null($relation)) {
Expand All @@ -154,7 +150,6 @@ public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relat
* @param string $related
* @param string|null $foreignKey
* @param string|null $localKey
* @return HasMany
*/
public function hasMany($related, $foreignKey = null, $localKey = null): HasMany
{
Expand All @@ -171,7 +166,6 @@ public function hasMany($related, $foreignKey = null, $localKey = null): HasMany
* @param string|null $parentKey
* @param string|null $relatedKey
* @param string|null $relation
* @return BelongsToMany
*/
public function belongsToMany(
$related, $table = null,
Expand All @@ -198,45 +192,25 @@ public function belongsToMany(
);
}

/**
* @return string
*/
public function getClassNameForRelationships(): string
{
return class_basename($this);
}

/**
* @return string
*/
public function getInheritanceColumn(): string
{
return property_exists($this, 'childColumn') ? $this->childColumn : 'type';
}

/**
* @param array $attributes
* @return mixed
*/
protected function getChildModel(array $attributes)
{
$className = $this->classFromAlias(
$attributes[$this->getInheritanceColumn()]
);

return new $className((array) $attributes);
}

/**
* @param mixed $aliasOrClass
* @return string
* @param mixed $aliasOrClass
*/
public function classFromAlias($aliasOrClass): string
{
$childTypes = $this->getChildTypes();

// Handling Enum casting for `type` column
if ($aliasOrClass instanceof \UnitEnum) {
if ($aliasOrClass instanceof UnitEnum) {
$aliasOrClass = $aliasOrClass->value;
}

Expand All @@ -247,10 +221,6 @@ public function classFromAlias($aliasOrClass): string
return $aliasOrClass;
}

/**
* @param string $className
* @return string
*/
public function classToAlias(string $className): string
{
$childTypes = $this->getChildTypes();
Expand All @@ -262,9 +232,6 @@ public function classToAlias(string $className): string
return $className;
}

/**
* @return array
*/
public function getChildTypes(): array
{
if (method_exists($this, 'childTypes')) {
Expand All @@ -277,4 +244,16 @@ public function getChildTypes(): array

return [];
}

/**
* @return mixed
*/
protected function getChildModel(array $attributes)
{
$className = $this->classFromAlias(
$attributes[$this->getInheritanceColumn()]
);

return new $className((array) $attributes);
}
}
53 changes: 21 additions & 32 deletions src/HasParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Parental;

use Illuminate\Database\Eloquent\Model;
use ReflectionClass;
use Illuminate\Support\Str;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionException;

trait HasParent
Expand All @@ -16,14 +16,13 @@ trait HasParent
public $hasParent = true;

/**
* @return void
* @throws ReflectionException
*/
public static function bootHasParent(): void
{
// This adds support for using Parental with standalone Eloquent, outside a normal Laravel app.
if (static::getEventDispatcher() === null) {
static::setEventDispatcher(new Dispatcher());
static::setEventDispatcher(new Dispatcher);
}

static::creating(function ($model) {
Expand All @@ -38,21 +37,17 @@ public static function bootHasParent(): void
$instance = new static;

if ($instance->parentHasHasChildrenTrait()) {
$query->where($query->getModel()->getTable().'.'.$instance->getInheritanceColumn(), $instance->classToAlias(get_class($instance)));
$query->where($query->getModel()->getTable() . '.' . $instance->getInheritanceColumn(), $instance->classToAlias(get_class($instance)));
}
});
}

/**
* @return bool
*/
public function parentHasHasChildrenTrait(): bool
{
return $this->hasChildren ?? false;
}

/**
* @return string
* @throws ReflectionException
*/
public function getTable(): string
Expand All @@ -65,18 +60,17 @@ public function getTable(): string
}

/**
* @return string
* @throws ReflectionException
*/
public function getForeignKey(): string
{
return Str::snake(class_basename($this->getParentClass())).'_'.$this->primaryKey;
return Str::snake(class_basename($this->getParentClass())) . '_' . $this->primaryKey;
}

/**
* @param string $related
* @param null|Model $instance
* @return string
* @param string $related
* @param null|Model $instance
*
* @throws ReflectionException
*/
public function joiningTable($related, $instance = null): string
Expand All @@ -96,7 +90,6 @@ public function joiningTable($related, $instance = null): string
}

/**
* @return string
* @throws ReflectionException
*/
public function getClassNameForRelationships(): string
Expand All @@ -107,7 +100,6 @@ public function getClassNameForRelationships(): string
/**
* Get the class name for polymorphic relations.
*
* @return string
* @throws ReflectionException
*/
public function getMorphClass(): string
Expand All @@ -120,28 +112,13 @@ public function getMorphClass(): string
/**
* Get the class name for poly-type collections
*
* @return string
* @throws ReflectionException
*/
public function getClassNameForSerialization(): string
{
return $this->getParentClass();
}

/**
* Get the class name for Parent Class.
*
* @return string
* @throws ReflectionException
*/
protected function getParentClass(): string
{
static $parentClassName;

return $parentClassName ?: $parentClassName = (new ReflectionClass($this))->getParentClass()->getName();
}


/**
* Merge the fillable attributes for the model with those of its Parent Class
*
Expand All @@ -156,7 +133,19 @@ public function getFillable()
return $this->fillable;
}
$parentFillable = (new $parentClass)->getFillable();

return array_unique(array_merge($parentFillable, $this->fillable));
}

/**
* Get the class name for Parent Class.
*
* @throws ReflectionException
*/
protected function getParentClass(): string
{
static $parentClassName;

return $parentClassName ?: $parentClassName = (new ReflectionClass($this))->getParentClass()->getName();
}
}
6 changes: 0 additions & 6 deletions src/Providers/NovaResourceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

class NovaResourceProvider extends ServiceProvider
{
/**
* @return void
*/
public function boot(): void
{
if (class_exists(Nova::class)) {
Expand All @@ -21,9 +18,6 @@ public function boot(): void
}
}

/**
* @return void
*/
protected function setNovaResources(): void
{
$map = [];
Expand Down
5 changes: 1 addition & 4 deletions tests/Features/AuthUserMethodReturnsChildModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

namespace Parental\Tests\Features;

use Parental\Tests\Models\Car;
use Parental\Tests\Models\Plane;
use Parental\Tests\Models\Vehicle;
use Parental\Tests\TestCase;

class AuthUserMethodReturnsChildModel extends TestCase
{
/** @test */
function auth_user_returns_child_model_if_it_exists()
public function auth_user_returns_child_model_if_it_exists()
{
Admin::create();
User::create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ChildModelFillablesMergeWithParentModelFillablesTest extends TestCase
{
/** @test */
function child_fillables_are_merged_with_parent_fillables()
public function child_fillables_are_merged_with_parent_fillables()
{
$workshop = Workshop::create([
'name' => 'Scaling Laravel',
Expand Down
Loading
Loading