diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a7c44dd --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_size = 2 diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 98b36e7..b4ae1c4 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -2,31 +2,54 @@ Contributions are **welcome** and will be fully **credited**. -We accept contributions via Pull Requests on [Github](https://github.com/spatie/eloquent-sortable). +Please read and understand the contribution guide before creating an issue or pull request. +## Etiquette -## Pull Requests +This project is open source, and as such, the maintainers give their free time to build and maintain the source code +held within. They make the code freely available in the hope that it will be of use to other developers. It would be +extremely unfair for them to suffer abuse or anger for their hard work. -- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). +Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the +world that developers are civilized and selfless people. -- **Add tests!** - Your patch won't be accepted if it doesn't have tests. +It's the duty of the maintainer to ensure that all submissions to the project are of sufficient +quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. -- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. +## Viability -- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. +When requesting or submitting new features, first consider whether it might be useful to others. Open +source projects are used by many developers, who may have entirely different needs to your own. Think about +whether or not your feature is likely to be used by other users of the project. -- **Create feature branches** - Don't ask us to pull from your master branch. +## Procedure -- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. +Before filing an issue: + +- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. +- Check to make sure your feature suggestion isn't already present within the project. +- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. +- Check the pull requests tab to ensure that the feature isn't already in progress. + +Before submitting a pull request: + +- Check the codebase to ensure that your feature doesn't already exist. +- Check the pull requests to ensure that another person hasn't already submitted the feature or fix. -- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. +## Requirements +If the project maintainer has any additional requirements, you will find them listed here. -## Running Tests +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). -``` bash -$ vendor/bin/phpunit -``` +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. **Happy coding**! diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 5ccc87c..fe5143b 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1 +1,2 @@ github: spatie +custom: https://spatie.be/open-source/support-us diff --git a/.github/SECURITY.md b/.github/SECURITY.md new file mode 100644 index 0000000..ca91343 --- /dev/null +++ b/.github/SECURITY.md @@ -0,0 +1,3 @@ +# Security Policy + +If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker. diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 5851e9e..3ac9eb2 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,16 +9,14 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - php: [8.0, 7.4, 7.3] - laravel: [8.*, 7.*, 6.*] + php: [8.0] + laravel: [8.*, 7.*] dependency-version: [prefer-lowest, prefer-stable] include: - laravel: 8.* testbench: 6.* - laravel: 7.* testbench: 5.* - - laravel: 6.* - testbench: 4.* name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} @@ -44,4 +42,4 @@ jobs: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction - name: Execute tests - run: vendor/bin/phpunit + run: vendor/bin/phpunit \ No newline at end of file diff --git a/.gitignore b/.gitignore index cd48823..06cfc8c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,14 @@ -/vendor -composer.phar +.idea +.php_cs +.php_cs.cache +.phpunit.result.cache +build composer.lock +coverage +docs +phpunit.xml +psalm.xml +testbench.yaml +vendor +composer.phar tests/_output/* -.php_cs.cache - diff --git a/.php_cs.dist b/.php_cs.dist index 1c4e7d5..c7d380c 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -29,9 +29,15 @@ return PhpCsFixer\Config::create() ], 'phpdoc_single_line_var_spacing' => true, 'phpdoc_var_without_name' => true, + 'class_attributes_separation' => [ + 'elements' => [ + 'method', + ], + ], 'method_argument_space' => [ 'on_multiline' => 'ensure_fully_multiline', 'keep_multiple_spaces_after_comma' => true, - ] + ], + 'single_trait_insert_per_statement' => true, ]) ->setFinder($finder); diff --git a/CHANGELOG.md b/CHANGELOG.md index 56ac9c8..1bb8b0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `eloquent-sortable` will be documented in this file +## 4.0.0 - unreleased + +- require PHP 8+ +- drop support for all PHP 7.x versions +- use PHP 8 syntax + ## 3.11.0 - 2021-01-18 - add methods to determine whether element is the last or first in order (#102) diff --git a/README.md b/README.md index a2ce8d3..a4a3c1e 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ We highly appreciate you sending us a postcard from your hometown, mentioning wh ## Installation +> For Laravel 6.x or PHP 7.x, use version 3.x of this package. + This package can be installed through Composer. ``` @@ -216,13 +218,13 @@ Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed re Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. -## Security +## Security Vulnerabilities -If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker. +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. ## Credits -- [Freek Van der Herten](https://murze.be) +- [Freek Van der Herten](https://github.com/freekmurze) - [All Contributors](../../contributors) ## Alternatives diff --git a/composer.json b/composer.json index 717da21..2f9a0af 100644 --- a/composer.json +++ b/composer.json @@ -19,13 +19,14 @@ ], "license": "MIT", "require": { - "php": "^7.3|^8.0", - "illuminate/database": "^6.0|^7.0|^8.0", - "illuminate/support": "^6.0|^7.0|^8.0" + "php": "^8.0", + "illuminate/database": "^7.0|^8.0", + "illuminate/support": "^7.0|^8.0", + "spatie/laravel-package-tools": "^1.6" }, "require-dev": { "phpunit/phpunit" : "^8.0|^9.0", - "orchestra/testbench": "^4.0|^5.0|^6.0" + "orchestra/testbench": "^5.0|^6.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.bak b/phpunit.xml.bak deleted file mode 100644 index 9576993..0000000 --- a/phpunit.xml.bak +++ /dev/null @@ -1,29 +0,0 @@ - - - - - ./tests/ - - - - - app/ - - - - - - - - - - - diff --git a/src/EloquentSortableServiceProvider.php b/src/EloquentSortableServiceProvider.php index 81b98ad..0180c5c 100644 --- a/src/EloquentSortableServiceProvider.php +++ b/src/EloquentSortableServiceProvider.php @@ -2,16 +2,15 @@ namespace Spatie\EloquentSortable; -use Illuminate\Support\ServiceProvider; +use Spatie\LaravelPackageTools\Package; +use Spatie\LaravelPackageTools\PackageServiceProvider; -class EloquentSortableServiceProvider extends ServiceProvider +class EloquentSortableServiceProvider extends PackageServiceProvider { - public function boot() + public function configurePackage(Package $package): void { - if ($this->app->runningInConsole()) { - $this->publishes([ - __DIR__.'/../config/eloquent-sortable.php' => config_path('eloquent-sortable.php'), - ], 'config'); - } + $package + ->name('eloquent-sortable') + ->hasConfigFile(); } } diff --git a/src/Sortable.php b/src/Sortable.php index deeb4d0..8c313be 100644 --- a/src/Sortable.php +++ b/src/Sortable.php @@ -9,7 +9,7 @@ interface Sortable /** * Modify the order column value. */ - public function setHighestOrderNumber(); + public function setHighestOrderNumber(): void; /** * Let's be nice and provide an ordered scope. @@ -27,7 +27,7 @@ public function scopeOrdered(Builder $query); * @param array|\ArrayAccess $ids * @param int $startOrder */ - public static function setNewOrder($ids, int $startOrder = 1); + public static function setNewOrder($ids, int $startOrder = 1): void; /** * Determine if the order column should be set when saving a new model instance. diff --git a/src/SortableTrait.php b/src/SortableTrait.php index 0e569b2..f5c73dd 100644 --- a/src/SortableTrait.php +++ b/src/SortableTrait.php @@ -18,7 +18,7 @@ public static function bootSortableTrait() }); } - public function setHighestOrderNumber() + public function setHighestOrderNumber(): void { $orderColumnName = $this->determineOrderColumnName(); @@ -40,7 +40,7 @@ public function scopeOrdered(Builder $query, string $direction = 'asc') return $query->orderBy($this->determineOrderColumnName(), $direction); } - public static function setNewOrder($ids, int $startOrder = 1, string $primaryKeyColumn = null) + public static function setNewOrder($ids, int $startOrder = 1, string $primaryKeyColumn = null): void { if (! is_array($ids) && ! $ids instanceof ArrayAccess) { throw new InvalidArgumentException('You must pass an array or ArrayAccess object to setNewOrder'); @@ -68,9 +68,7 @@ public static function setNewOrderByCustomColumn(string $primaryKeyColumn, $ids, public function determineOrderColumnName(): string { - return isset($this->sortable['order_column_name']) - ? $this->sortable['order_column_name'] - : config('eloquent-sortable.order_column_name', 'order_column'); + return $this->sortable['order_column_name'] ?? config('eloquent-sortable.order_column_name', 'order_column'); } /** @@ -78,12 +76,10 @@ public function determineOrderColumnName(): string */ public function shouldSortWhenCreating(): bool { - return isset($this->sortable['sort_when_creating']) - ? $this->sortable['sort_when_creating'] - : config('eloquent-sortable.sort_when_creating', true); + return $this->sortable['sort_when_creating'] ?? config('eloquent-sortable.sort_when_creating', true); } - public function moveOrderDown() + public function moveOrderDown(): static { $orderColumnName = $this->determineOrderColumnName(); @@ -99,7 +95,7 @@ public function moveOrderDown() return $this->swapOrderWithModel($swapWithModel); } - public function moveOrderUp() + public function moveOrderUp(): static { $orderColumnName = $this->determineOrderColumnName(); @@ -115,7 +111,7 @@ public function moveOrderUp() return $this->swapOrderWithModel($swapWithModel); } - public function swapOrderWithModel(Sortable $otherModel) + public function swapOrderWithModel(Sortable $otherModel): static { $orderColumnName = $this->determineOrderColumnName(); @@ -130,12 +126,12 @@ public function swapOrderWithModel(Sortable $otherModel) return $this; } - public static function swapOrder(Sortable $model, Sortable $otherModel) + public static function swapOrder(Sortable $model, Sortable $otherModel): void { $model->swapOrderWithModel($otherModel); } - public function moveToStart() + public function moveToStart(): static { $firstModel = $this->buildSortQuery()->limit(1) ->ordered() @@ -155,7 +151,7 @@ public function moveToStart() return $this; } - public function moveToEnd() + public function moveToEnd(): static { $maxOrder = $this->getHighestOrderNumber(); @@ -191,7 +187,7 @@ public function isFirstInOrder(): bool return (int)$this->$orderColumnName === $this->getLowestOrderNumber(); } - public function buildSortQuery() + public function buildSortQuery(): Builder { return static::query(); } diff --git a/tests/DummyWithSoftDeletes.php b/tests/DummyWithSoftDeletes.php index 7c28150..47208b9 100644 --- a/tests/DummyWithSoftDeletes.php +++ b/tests/DummyWithSoftDeletes.php @@ -9,7 +9,8 @@ class DummyWithSoftDeletes extends Model implements Sortable { - use SoftDeletes, SortableTrait; + use SoftDeletes; + use SortableTrait; protected $table = 'dummies'; protected $guarded = []; diff --git a/tests/SortableTest.php b/tests/SortableTest.php index b05cf29..9a2c5c6 100644 --- a/tests/SortableTest.php +++ b/tests/SortableTest.php @@ -325,6 +325,7 @@ public function it_can_move_a_model_to_the_last_place() } } } + /** @test */ public function it_can_use_config_properties() {