Skip to content

Commit

Permalink
Add PHP 8-only Support (v4) (#106)
Browse files Browse the repository at this point in the history
* drop support for PHP 7, Laravel 6.x

* implement spatie/laravel-package-tools

* use PHP 8 syntax

* add/update files to more closely match spatie/package-skeleton-laravel

* add note to use version 3 of the package for Laravel 6/PHP 7; minor link updates, etc.

* update changelog

* update FUNDING.yml

* add/update files to more closely match spatie/package-skeleton-laravel

* Fix styling

* remove unneeded backup file
  • Loading branch information
patinthehat authored Mar 31, 2021
1 parent b06fa88 commit dca7c2d
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 85 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
49 changes: 36 additions & 13 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**!
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github: spatie
custom: https://spatie.be/open-source/support-us
3 changes: 3 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Security Policy

If you discover any security related issues, please email [email protected] instead of using the issue tracker.
8 changes: 3 additions & 5 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand All @@ -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
16 changes: 12 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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

8 changes: 7 additions & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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);
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

```
Expand Down Expand Up @@ -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 [email protected] 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
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
29 changes: 0 additions & 29 deletions phpunit.xml.bak

This file was deleted.

15 changes: 7 additions & 8 deletions src/EloquentSortableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
26 changes: 11 additions & 15 deletions src/SortableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function bootSortableTrait()
});
}

public function setHighestOrderNumber()
public function setHighestOrderNumber(): void
{
$orderColumnName = $this->determineOrderColumnName();

Expand All @@ -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');
Expand Down Expand Up @@ -68,22 +68,18 @@ 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');
}

/**
* Determine if the order column should be set when saving a new model instance.
*/
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();

Expand All @@ -99,7 +95,7 @@ public function moveOrderDown()
return $this->swapOrderWithModel($swapWithModel);
}

public function moveOrderUp()
public function moveOrderUp(): static
{
$orderColumnName = $this->determineOrderColumnName();

Expand All @@ -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();

Expand All @@ -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()
Expand All @@ -155,7 +151,7 @@ public function moveToStart()
return $this;
}

public function moveToEnd()
public function moveToEnd(): static
{
$maxOrder = $this->getHighestOrderNumber();

Expand Down Expand Up @@ -191,7 +187,7 @@ public function isFirstInOrder(): bool
return (int)$this->$orderColumnName === $this->getLowestOrderNumber();
}

public function buildSortQuery()
public function buildSortQuery(): Builder
{
return static::query();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/DummyWithSoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

class DummyWithSoftDeletes extends Model implements Sortable
{
use SoftDeletes, SortableTrait;
use SoftDeletes;
use SortableTrait;

protected $table = 'dummies';
protected $guarded = [];
Expand Down
1 change: 1 addition & 0 deletions tests/SortableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ public function it_can_move_a_model_to_the_last_place()
}
}
}

/** @test */
public function it_can_use_config_properties()
{
Expand Down

0 comments on commit dca7c2d

Please sign in to comment.