diff --git a/.editorconfig b/.editorconfig index 2a5ddba..a7c44dd 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,3 @@ -; This file is for unifying the coding style for different editors and IDEs. -; More information at https://editorconfig.org - root = true [*] @@ -14,5 +11,5 @@ trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false -[*.yml] +[*.{yml,yaml}] indent_size = 2 diff --git a/.gitattributes b/.gitattributes index bb6265e..0215303 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,10 +2,13 @@ # https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html # Ignore all test and documentation with "export-ignore". +/.github export-ignore /.gitattributes export-ignore /.gitignore export-ignore /.travis.yml export-ignore /phpunit.xml.dist export-ignore -/.scrutinizer.yml export-ignore /tests export-ignore /.editorconfig export-ignore +/.php_cs.dist export-ignore +/psalm.xml export-ignore +/psalm.xml.dist export-ignore diff --git a/CONTRIBUTING.md b/.github/CONTRIBUTING.md similarity index 99% rename from CONTRIBUTING.md rename to .github/CONTRIBUTING.md index 5ac52a0..7ff004d 100644 --- a/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -27,4 +27,3 @@ Before submitting a pull request: - **One pull request per feature**: If you want to do more than one thing, send multiple pull requests. - **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. - diff --git a/.github/SECURITY.md b/.github/SECURITY.md new file mode 100644 index 0000000..b60f4d3 --- /dev/null +++ b/.github/SECURITY.md @@ -0,0 +1,3 @@ +# Security Policy + +If you discover any security related issues, please email security@simplesquid.co.za instead of using the issue tracker. diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml new file mode 100644 index 0000000..2d6f8f1 --- /dev/null +++ b/.github/workflows/php-cs-fixer.yml @@ -0,0 +1,29 @@ +name: Check & fix styling + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + style: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + + - name: Run PHP CS Fixer + uses: docker://oskarstark/php-cs-fixer-ga + with: + args: --config=.php_cs.dist.php --allow-risky=yes + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Fix styling. diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..504efdc --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,50 @@ +name: Run tests + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ ubuntu-latest ] + php: [ 8.0, 8.1 ] + laravel: [ "^9.0" ] + stability: [ prefer-lowest, prefer-stable ] + include: + - laravel: "^9.0" + testbench: "^7.0" + + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick + coverage: none + + - name: Setup problem matchers + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Install dependencies + run: | + composer config "http-basic.nova.laravel.com" "${{ secrets.NOVA_USERNAME }}" "${{ secrets.NOVA_PASSWORD }}" + composer require "illuminate/support:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update + composer update --${{ matrix.stability }} --prefer-dist --no-interaction + + - name: Execute tests + run: vendor/bin/phpunit diff --git a/.gitignore b/.gitignore index 6394476..576f263 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,12 @@ node_modules phpunit.xml .phpunit.* build +coverage + +.php_cs +.php_cs.cache +psalm.xml +.php-cs-fixer.cache .idea @@ -16,4 +22,3 @@ build Thumbs.db mix-manifest.json - diff --git a/.php_cs.dist.php b/.php_cs.dist.php new file mode 100644 index 0000000..8d8a790 --- /dev/null +++ b/.php_cs.dist.php @@ -0,0 +1,40 @@ +in([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->name('*.php') + ->notName('*.blade.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true); + +return (new PhpCsFixer\Config()) + ->setRules([ + '@PSR12' => true, + 'array_syntax' => ['syntax' => 'short'], + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + 'no_unused_imports' => true, + 'not_operator_with_successor_space' => true, + 'trailing_comma_in_multiline' => true, + 'phpdoc_scalar' => true, + 'unary_operator_spaces' => true, + 'binary_operator_spaces' => true, + 'blank_line_before_statement' => [ + 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], + ], + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_var_without_name' => true, + 'class_attributes_separation' => [ + 'elements' => [ + 'method' => 'one', + ], + ], + '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/.styleci.yml b/.styleci.yml deleted file mode 100644 index 892e40c..0000000 --- a/.styleci.yml +++ /dev/null @@ -1,8 +0,0 @@ -preset: laravel - -enabled: - - concat_with_spaces - -disabled: - - concat_without_spaces - - self_accessor diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1d4fb5c..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: php - -php: - - 7.3 - -env: - matrix: - - COMPOSER_FLAGS="--prefer-lowest" - - COMPOSER_FLAGS="" - -before_script: - - travis_retry composer self-update - - composer config http-basic.nova.laravel.com ${NOVA_USERNAME} ${NOVA_PASSWORD} - - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - -script: - - vendor/bin/phpunit diff --git a/CHANGELOG.md b/CHANGELOG.md index ddd1bc1..434d26d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,21 +2,21 @@ All notable changes to `nova-advanced-number-field` will be documented in this file. -## 1.0.0 - 2019-09-07 - -- Initial release. - -## 1.0.1 - 2019-09-07 +## 3.0.0 - 2022-11-29 -- Push to Packagist. +- Laravel v9 is now required +- Laravel Nova v4 is now required +- PHP 8.0 or higher is now required +- Add type-hinting +- Swap to GitHub Actions -## 1.0.2 - 2019-09-07 +## 2.0.0 - 2020-04-25 -- Version fix. +- Fix minimum Nova version. -## 1.0.3 - 2019-09-07 +## 1.1.1 - 2019-11-01 -- Version fix. +- Fix minimum Nova version. ## 1.1.0 - 2019-11-01 @@ -24,6 +24,18 @@ All notable changes to `nova-advanced-number-field` will be documented in this f - Refactor and remove unnecessary code. - Add tests. -## 1.1.1 - 2019-11-01 +## 1.0.3 - 2019-09-07 -- Fix minimum Nova version. +- Version fix. + +## 1.0.2 - 2019-09-07 + +- Version fix. + +## 1.0.1 - 2019-09-07 + +- Push to Packagist. + +## 1.0.0 - 2019-09-07 + +- Initial release. diff --git a/README.md b/README.md index ed4c0af..7f7a87a 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ # An advanced number field for Laravel Nova [![Latest Version on Packagist](https://img.shields.io/packagist/v/simplesquid/nova-advanced-number-field.svg?style=flat-square)](https://packagist.org/packages/simplesquid/nova-advanced-number-field) +[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/simplesquid/nova-advanced-number-field/Run%20tests?label=tests)](https://github.com/simplesquid/nova-advanced-number-field/actions?query=workflow%3A"Run+tests"+branch%3Amain) +[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/simplesquid/nova-advanced-number-field/Check%20&%20fix%20styling?label=code%20style)](https://github.com/simplesquid/nova-advanced-number-field/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain) [![MIT License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) [![Total Downloads](https://img.shields.io/packagist/dt/simplesquid/nova-advanced-number-field.svg?style=flat-square)](https://packagist.org/packages/simplesquid/nova-advanced-number-field) A Laravel Nova field which adds additional functionality to the default Number field by using PHP's `number_format()` function. -![Screenshot of the advanced number field](https://github.com/simplesquid/nova-advanced-number-field/raw/master/docs/screenshot.png) +![Screenshot of the advanced number field](https://github.com/simplesquid/nova-advanced-number-field/raw/main/docs/screenshot.png) ## Installation @@ -26,55 +28,23 @@ The `AdvancedNumber` field provides an additional 5 methods to the default `Numb - `->decimals(3)`: Sets the number of decimal points to be used as well as the step value. - `->suffix('%')`: Sets the suffix to be used when displaying the number. -You can use the field in your Nova resource like so: - -```php -namespace App\Nova; - -use SimpleSquid\Nova\Fields\AdvancedNumber\AdvancedNumber; - -class User extends Resource -{ - // ... - - public function fields(Request $request) - { - return [ - // ... - - AdvancedNumber::make('Price') - ->prefix('$') - ->thousandsSeparator(','), - - // AdvancedNumber extends Number, so you can use Number methods too: - AdvancedNumber::make('Markup') - ->decimals(0) - ->suffix('%') - ->min(0)->max(100), - - // ... - ]; - } -} -``` - -### Testing +## Testing ``` bash composer test ``` -### Changelog +## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. ## Contributing -Please see [CONTRIBUTING](CONTRIBUTING.md) for details. +Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. -### Security +## Security Vulnerabilities -If you discover any security related issues, please email security@simplesquid.co.za instead of using the issue tracker. +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. ## Credits @@ -85,7 +55,7 @@ Package skeleton based on [spatie/skeleton-php](https://github.com/spatie/skelet ## About us -SimpleSquid is a small web development and design company based in Cape Town, South Africa. +SimpleSquid is a small web development and design company based in Valkenburg, Netherlands. ## License diff --git a/composer.json b/composer.json index a20ddef..c76f46c 100644 --- a/composer.json +++ b/composer.json @@ -26,13 +26,15 @@ } ], "require": { - "php": ">=7.3.0", - "laravel/nova": "^3.0" + "php": "^8.0", + "illuminate/support": "^9.0", + "laravel/nova": "^4.0" }, "require-dev": { - "symfony/var-dumper": "^4.3", - "orchestra/testbench": "^5.0", - "phpunit/phpunit": "^8.0" + "nunomaduro/collision": "^6.1", + "orchestra/testbench": "^7.0", + "phpunit/phpunit": "^9.3.3", + "symfony/var-dumper": "^6.0" }, "autoload": { "psr-4": { @@ -41,13 +43,19 @@ }, "autoload-dev": { "psr-4": { - "SimpleSquid\\Skeleton\\Tests\\": "tests" + "SimpleSquid\\Nova\\Fields\\AdvancedNumber\\Tests\\": "tests" + } + }, + "extra": { + "laravel": { + "providers": [] } }, "scripts": { - "test": "vendor/bin/phpunit" + "test": "vendor/bin/phpunit --colors=always" }, "config": { "sort-packages": true - } + }, + "minimum-stability": "dev" } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ea0df26..91e0f65 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,6 @@ - + stopOnFailure="false" + printerClass="NunoMaduro\Collision\Adapters\Phpunit\Printer" + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> + + + src + + tests - - - src - - diff --git a/src/AdvancedNumber.php b/src/AdvancedNumber.php index 8735bcc..b7bd1c9 100644 --- a/src/AdvancedNumber.php +++ b/src/AdvancedNumber.php @@ -1,7 +1,4 @@ dec_point = $dec_point; @@ -80,12 +63,8 @@ public function decimalPoint($dec_point) /** * Sets the number of decimal points to be used as well as the step value. - * - * @param int $decimals - * - * @return $this */ - public function decimals($decimals) + public function decimals(int $decimals): static { $this->decimals = $decimals; @@ -96,12 +75,8 @@ public function decimals($decimals) /** * Sets the prefix to be used when displaying the number. - * - * @param string $prefix - * - * @return $this */ - public function prefix($prefix) + public function prefix(string $prefix): static { $this->prefix = $prefix; @@ -110,12 +85,8 @@ public function prefix($prefix) /** * Sets the suffix to be used when displaying the number. - * - * @param string $suffix - * - * @return $this */ - public function suffix($suffix) + public function suffix(string $suffix): static { $this->suffix = $suffix; @@ -124,12 +95,8 @@ public function suffix($suffix) /** * Set the thousands separator symbol to be used when displaying the number. - * - * @param string $thousands_sep - * - * @return $this */ - public function thousandsSeparator($thousands_sep) + public function thousandsSeparator(string $thousands_sep): static { $this->thousands_sep = $thousands_sep; diff --git a/tests/AdvancedNumberTest.php b/tests/AdvancedNumberTest.php index e265c53..56b51ce 100644 --- a/tests/AdvancedNumberTest.php +++ b/tests/AdvancedNumberTest.php @@ -1,14 +1,12 @@ assertArrayHasKey('step', $this->field->meta); - - $this->assertEquals('0.01', $this->field->meta['step']); + $this->assertEquals('0.01', $this->field->step ?? $this->field->meta['step']); } /** @test */ @@ -36,7 +32,7 @@ public function field_displays_number_correctly_using_defaults() { $this->field->resolveForDisplay(['number_field' => 12345.678]); - $this->assertEquals('12 345.68', $this->field->value); + $this->assertEquals('12 345.68', $this->field->displayedAs ?? $this->field->value); } /** @test */ @@ -46,7 +42,7 @@ public function prefix_can_be_set_and_is_displayed() $this->field->resolveForDisplay(['number_field' => 12345.678]); - $this->assertEquals('$12 345.68', $this->field->value); + $this->assertEquals('$12 345.68', $this->field->displayedAs ?? $this->field->value); } /** @test */ @@ -56,7 +52,7 @@ public function suffix_can_be_set_and_is_displayed() $this->field->resolveForDisplay(['number_field' => 12345.678]); - $this->assertEquals('12 345.68%', $this->field->value); + $this->assertEquals('12 345.68%', $this->field->displayedAs ?? $this->field->value); } /** @test */ @@ -66,7 +62,7 @@ public function number_of_decimal_places_can_be_set_and_is_displayed() $this->field->resolveForDisplay(['number_field' => 12345.678]); - $this->assertEquals('12 345.678', $this->field->value); + $this->assertEquals('12 345.678', $this->field->displayedAs ?? $this->field->value); } /** @test */ @@ -76,7 +72,7 @@ public function decimal_point_can_be_set_and_is_displayed() $this->field->resolveForDisplay(['number_field' => 12345.678]); - $this->assertEquals('12 345,68', $this->field->value); + $this->assertEquals('12 345,68', $this->field->displayedAs ?? $this->field->value); } /** @test */ @@ -86,7 +82,7 @@ public function thousands_separator_can_be_set_and_is_displayed() $this->field->resolveForDisplay(['number_field' => 12345.678]); - $this->assertEquals('12,345.68', $this->field->value); + $this->assertEquals('12,345.68', $this->field->displayedAs ?? $this->field->value); } /** @test */ @@ -108,7 +104,7 @@ public function modifiers_can_be_used_together() $this->field->resolveForDisplay(['number_field' => 12345.678]); - $this->assertEquals('$12,345 and 68c', $this->field->value); + $this->assertEquals('$12,345 and 68c', $this->field->displayedAs ?? $this->field->value); $this->field->resolve(['number_field' => 12345.678]); diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..3861e66 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,16 @@ +