From 051a32575c075b9259569b61c3d1b30446e6d786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 23 May 2021 13:17:03 +0200 Subject: [PATCH] Initial commit --- .gitattributes | 13 +++ .github/workflows/ci.yml | 60 ++++++++++++++ .gitignore | 8 ++ .php_cs.php | 144 +++++++++++++++++++++++++++++++++ LICENSE | 21 +++++ README.md | 41 ++++++++++ check | 56 +++++++++++++ composer.json | 39 +++++++++ docker-compose.yml | 12 +++ phpstan.neon | 24 ++++++ phpunit.xml | 31 +++++++ src/REPLACEServiceProvider.php | 32 ++++++++ tests/Pest.php | 45 +++++++++++ tests/Pest/ExampleTest.php | 9 +++ tests/TestCase.php | 16 ++++ 15 files changed, 551 insertions(+) create mode 100644 .gitattributes create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 .php_cs.php create mode 100644 LICENSE create mode 100644 README.md create mode 100755 check create mode 100644 composer.json create mode 100644 docker-compose.yml create mode 100644 phpstan.neon create mode 100644 phpunit.xml create mode 100644 src/REPLACEServiceProvider.php create mode 100644 tests/Pest.php create mode 100644 tests/Pest/ExampleTest.php create mode 100644 tests/TestCase.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..832cd4f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,13 @@ +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore + +/docker-compose.yml export-ignore +/tests export-ignore + +/phpstan.neon export-ignore +/.php_cs.php export-ignore +/psalm.xml export-ignore +/phpunit.xml export-ignore +/check export-ignore +/coverage export-ignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..7a66f64 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,60 @@ +name: CI + +env: + COMPOSE_INTERACTIVE_NO_CLI: 1 + PHP_CS_FIXER_IGNORE_ENV: 1 + MYSQL_PORT: 3307 + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + +on: + push: + pull_request: + branches: [ master ] + +jobs: + phpunit: + name: Tests (PHPUnit) + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Start docker containers + run: docker-compose up -d + - name: Install composer dependencies + run: composer install + - name: Run tests + run: vendor/bin/phpunit + + steps: + - uses: actions/checkout@v2 + - name: Install composer dependencies + run: composer install + - name: Run psalm + run: vendor/bin/psalm + + phpstan: + name: Static analysis (PHPStan) + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Install composer dependencies + run: composer install + - name: Run phpstan + run: vendor/bin/phpstan analyse + + php-cs-fixer: + name: Code style (php-cs-fixer) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install php-cs-fixer + run: composer global require friendsofphp/php-cs-fixer + - name: Run php-cs-fixer + run: $HOME/.composer/vendor/bin/php-cs-fixer fix --config=.php_cs.php + - name: Commit changes from php-cs-fixer + uses: EndBug/add-and-commit@v5 + with: + author_name: "PHP CS Fixer" + author_email: "phpcsfixer@example.com" + message: Fix code style (php-cs-fixer) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d9c3777 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.phpunit.result.cache +package-lock.json +composer.lock +vendor/ +.php_cs.cache +.vscode/ +coverage/ +node_modules diff --git a/.php_cs.php b/.php_cs.php new file mode 100644 index 0000000..4841dd7 --- /dev/null +++ b/.php_cs.php @@ -0,0 +1,144 @@ + ['syntax' => 'short'], + 'binary_operator_spaces' => [ + 'default' => 'single_space', + 'operators' => [ + '=>' => null, + '|' => 'no_space', + ] + ], + 'blank_line_after_namespace' => true, + 'blank_line_after_opening_tag' => true, + 'no_superfluous_phpdoc_tags' => true, + 'blank_line_before_statement' => [ + 'statements' => ['return'] + ], + 'braces' => true, + 'cast_spaces' => true, + 'class_attributes_separation' => [ + 'elements' => ['method'] + ], + 'class_definition' => true, + 'concat_space' => [ + 'spacing' => 'one' + ], + 'declare_equal_normalize' => true, + 'elseif' => true, + 'encoding' => true, + 'full_opening_tag' => true, + 'declare_strict_types' => true, + 'fully_qualified_strict_types' => true, // added by Shift + 'function_declaration' => true, + 'function_typehint_space' => true, + 'heredoc_to_nowdoc' => true, + 'include' => true, + 'increment_style' => ['style' => 'post'], + 'indentation_type' => true, + 'linebreak_after_opening_tag' => true, + 'line_ending' => true, + 'lowercase_cast' => true, + 'lowercase_constants' => true, + 'lowercase_keywords' => true, + 'lowercase_static_reference' => true, // added from Symfony + 'magic_method_casing' => true, // added from Symfony + 'magic_constant_casing' => true, + 'method_argument_space' => true, + 'native_function_casing' => true, + 'no_alias_functions' => true, + 'no_extra_blank_lines' => [ + 'tokens' => [ + 'extra', + 'throw', + 'use', + 'use_trait', + ] + ], + 'no_blank_lines_after_class_opening' => true, + 'no_blank_lines_after_phpdoc' => true, + 'no_closing_tag' => true, + 'no_empty_phpdoc' => true, + 'no_empty_statement' => true, + 'no_leading_import_slash' => true, + 'no_leading_namespace_whitespace' => true, + 'no_mixed_echo_print' => [ + 'use' => 'echo' + ], + 'no_multiline_whitespace_around_double_arrow' => true, + 'multiline_whitespace_before_semicolons' => [ + 'strategy' => 'no_multi_line' + ], + 'no_short_bool_cast' => true, + 'no_singleline_whitespace_before_semicolons' => true, + 'no_spaces_after_function_name' => true, + 'no_spaces_around_offset' => true, + 'no_spaces_inside_parenthesis' => true, + 'no_trailing_comma_in_list_call' => true, + 'no_trailing_comma_in_singleline_array' => true, + 'no_trailing_whitespace' => true, + 'no_trailing_whitespace_in_comment' => true, + 'no_unneeded_control_parentheses' => true, + 'no_unreachable_default_argument_value' => true, + 'no_useless_return' => true, + 'no_whitespace_before_comma_in_array' => true, + 'no_whitespace_in_blank_line' => true, + 'normalize_index_brace' => true, + 'not_operator_with_successor_space' => true, + 'object_operator_without_whitespace' => true, + 'ordered_imports' => ['sortAlgorithm' => 'alpha'], + 'phpdoc_indent' => true, + 'phpdoc_inline_tag' => true, + 'phpdoc_no_access' => true, + 'phpdoc_no_package' => true, + 'phpdoc_no_useless_inheritdoc' => true, + 'phpdoc_scalar' => true, + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_summary' => true, + 'phpdoc_to_comment' => false, + 'phpdoc_trim' => true, + 'phpdoc_types' => true, + 'phpdoc_var_without_name' => true, + 'psr4' => true, + 'self_accessor' => true, + 'short_scalar_cast' => true, + 'simplified_null_return' => false, // disabled by Shift + 'single_blank_line_at_eof' => true, + 'single_blank_line_before_namespace' => true, + 'single_class_element_per_statement' => true, + 'single_import_per_statement' => true, + 'single_line_after_imports' => true, + 'no_unused_imports' => true, + 'single_line_comment_style' => [ + 'comment_types' => ['hash'] + ], + 'single_quote' => true, + 'space_after_semicolon' => true, + 'standardize_not_equals' => true, + 'switch_case_semicolon_to_colon' => true, + 'switch_case_space' => true, + 'ternary_operator_spaces' => true, + 'trailing_comma_in_multiline_array' => true, + 'trim_array_spaces' => true, + 'unary_operator_spaces' => true, + 'whitespace_after_comma_in_array' => true, +]; + +$project_path = getcwd(); +$finder = Finder::create() + ->in([ + $project_path . '/src', + ]) + ->name('*.php') + ->notName('*.blade.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true); + +return Config::create() + ->setFinder($finder) + ->setRules($rules) + ->setRiskyAllowed(true) + ->setUsingCache(true); diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b285610 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 ArchTech Development, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d7bf5b4 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# REPLACE + +Simple and flexible package template. + +# Usage + +- Replace all occurances of `REPLACE` (case sensitive) with the name of the package namespace. E.g. the `Foo` in `ArchTech\Foo`. +- Replace all occurances of `replace2` with the name of the package on composer, e.g. the `bar` in `archtechx/bar`. +- If MySQL is not needed, remove `docker-compose.yml`, remove the line that runs docker from `./check`, and set `DB_CONNECTION` in `phpunit.xml` to `sqlite`, and `DB_DATABASE` to `:memory:`. + +--- + +## Installation + +```sh +composer require stancl/replace2 +``` + +## Usage + +```php +// ... +``` + +## Development + +Running all checks locally: + +```sh +./check +``` + +Running tests: + +```sh +MYSQL_PORT=3307 docker-compose up -d + +phpunit +``` + +Code style will be automatically fixed by php-cs-fixer. diff --git a/check b/check new file mode 100755 index 0000000..12ca616 --- /dev/null +++ b/check @@ -0,0 +1,56 @@ +#!/bin/bash +set -e + +offer_run() { + read -p "For more output, run $1. Run it now (Y/n)? " run + + case ${run:0:1} in + n|N ) + exit 1 + ;; + * ) + $1 + ;; + esac + + exit 1 +} + +if (php-cs-fixer fix --dry-run --config=.php_cs.php > /dev/null 2>/dev/null); then + echo '✅ php-cs-fixer OK' +else + read -p "⚠️ php-cs-fixer found issues. Fix (Y/n)? " fix + case ${fix:0:1} in + n|N ) + echo '❌ php-cs-fixer FAIL' + offer_run 'php-cs-fixer fix --config=.php_cs.php' + ;; + * ) + if (php-cs-fixer fix --config=.php_cs.php > /dev/null 2>/dev/null); then + echo '✅ php-cs-fixer OK' + else + echo '❌ php-cs-fixer FAIL' + offer_run 'php-cs-fixer fix --config=.php_cs.php' + fi + ;; + esac +fi + +if (./vendor/bin/phpstan analyse > /dev/null 2>/dev/null); then + echo '✅ PHPStan OK' +else + echo '❌ PHPStan FAIL' + offer_run './vendor/bin/phpstan analyse' +fi + +(MYSQL_PORT=3307 docker-compose up -d > /dev/null 2>/dev/null) || true + +if (./vendor/bin/pest > /dev/null 2>/dev/null); then + echo '✅ PEST OK' +else + echo '❌ PEST FAIL' + offer_run './vendor/bin/pest' +fi + +echo '==================' +echo '✅ Everything OK' diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cf0d20f --- /dev/null +++ b/composer.json @@ -0,0 +1,39 @@ +{ + "name": "archtechx/replace", + "description": "", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Samuel Štancl", + "email": "samuel@archte.ch" + } + ], + "autoload": { + "psr-4": { + "ArchTech\\REPLACE\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "ArchTech\\REPLACE\\Tests\\": "tests/" + } + }, + "require": { + "illuminate/support": "^8.24" + }, + "require-dev": { + "orchestra/testbench": "^6.9", + "vimeo/psalm": "^4.2", + "nunomaduro/larastan": "^0.6.10", + "pestphp/pest": "^1.2", + "pestphp/pest-plugin-laravel": "^1.0" + }, + "extra": { + "laravel": { + "providers": [ + "ArchTech\\REPLACE\\PackageServiceProvider" + ] + } + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..45edba6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: '3' +services: + mysql: + image: mysql:5.7 + environment: + MYSQL_ROOT_PASSWORD: password + MYSQL_DATABASE: main + MYSQL_USER: user + MYSQL_PASSWORD: password + MYSQL_TCP_PORT: ${MYSQL_PORT} + ports: + - "${MYSQL_PORT}:${MYSQL_PORT}" diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..5b8dfd1 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,24 @@ +includes: + - ./vendor/nunomaduro/larastan/extension.neon + +parameters: + paths: + - src + + level: 8 + + universalObjectCratesClasses: + - Illuminate\Routing\Route + + ignoreErrors: + # - + # message: '#Offset (.*?) does not exist on array\|null#' + # paths: + # - tests/* + # - + # message: '#expects resource, resource\|false given#' + # paths: + # - tests/* + # - '#should return \$this#' + + checkMissingIterableValueType: false diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..273f090 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,31 @@ + + + + + ./src + + + + + + + + + ./tests + + + + + + + + + + + + + + + + + diff --git a/src/REPLACEServiceProvider.php b/src/REPLACEServiceProvider.php new file mode 100644 index 0000000..f687b0d --- /dev/null +++ b/src/REPLACEServiceProvider.php @@ -0,0 +1,32 @@ +loadViewsFrom(__DIR__ . '/../assets/views', 'package'); + + // $this->publishes([ + // __DIR__ . '/../assets/views' => resource_path('views/vendor/package'), + // ], 'package-views'); + + // $this->mergeConfigFrom( + // __DIR__ . '/../assets/package.php', + // 'package' + // ); + + // $this->publishes([ + // __DIR__ . '/../assets/package.php' => config_path('package.php'), + // ], 'package-config'); + } +} diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..9103936 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,45 @@ +in('Pest'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation API at any time. +| +*/ + +expect()->extend('toBeOne', function () { + return $this->toBe(1); +}); + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can also expose helpers as +| global functions to help you to reduce the number of lines of code in your test files. +| +*/ + +function something() +{ + // .. +} diff --git a/tests/Pest/ExampleTest.php b/tests/Pest/ExampleTest.php new file mode 100644 index 0000000..5d10dd3 --- /dev/null +++ b/tests/Pest/ExampleTest.php @@ -0,0 +1,9 @@ +toBeTrue(); +}); + +it('fails', function () { + expect(false)->toBeTrue(); +}); diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..ee3c5db --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,16 @@ +