From a1b9d67e3ae8ee9d6e9f5f021b33d29802cb4a76 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 14 Dec 2023 20:57:02 +0100 Subject: [PATCH] CI setup (#1) --- .gitattributes | 2 ++ .github/workflows/php-linter.yml | 29 ++++++++++++++++++ .github/workflows/static-analysis.yml | 44 +++++++++++++++++++++++++++ .github/workflows/unit-tests.yml | 44 +++++++++++++++++++++++++++ .gitignore | 1 + LICENSE | 21 +++++++++++++ README.md | 4 +++ composer.json | 2 +- phpstan.neon | 8 ++++- src/TodoByRule.php | 8 ++++- tests/TodoByRuleTest.php | 9 +++++- tests/data/example.php | 11 ++++--- 12 files changed, 174 insertions(+), 9 deletions(-) create mode 100644 .gitattributes create mode 100644 .github/workflows/php-linter.yml create mode 100644 .github/workflows/static-analysis.yml create mode 100644 .github/workflows/unit-tests.yml create mode 100644 LICENSE diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..d0e7db1 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +/tests export-ignore +phpstan.neon export-ignore diff --git a/.github/workflows/php-linter.yml b/.github/workflows/php-linter.yml new file mode 100644 index 0000000..b12205e --- /dev/null +++ b/.github/workflows/php-linter.yml @@ -0,0 +1,29 @@ +name: PHP Linter + +on: + push: + branches: + - main + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + +jobs: + php-linter: + runs-on: ubuntu-latest + if: github.event.pull_request.draft == false + + steps: + - uses: actions/checkout@v4 + with: + repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }} + ref: ${{ github.event.client_payload.pull_request.head.ref }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.0 + coverage: none + tools: parallel-lint + + - name: Lint PHP + run: composer exec --no-interaction -- parallel-lint src/ tests/ diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..5ebc072 --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,44 @@ +name: Static Analysis + +on: + push: + branches: + - main + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + +jobs: + phpstan: + name: phpstan static code analysis + runs-on: ${{ matrix.os }} + + strategy: + matrix: + include: + - os: ubuntu-latest + php-version: '8.0' + - os: ubuntu-latest + php-version: '8.1' + - os: ubuntu-latest + php-version: '8.2' + - os: ubuntu-latest + php-version: '8.3' + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + extensions: gd, intl, pdo_mysql + coverage: none # disable xdebug, pcov + + - name: Composer install + uses: ramsey/composer-install@v2 + with: + composer-options: '--ansi --prefer-dist' + + - name: Run phpstan analysis + run: vendor/bin/phpstan analyse --ansi diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..75128c1 --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,44 @@ +name: Unit tests + +on: + push: + branches: + - main + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + +jobs: + phpunit: + runs-on: ${{ matrix.os }} + + strategy: + matrix: + include: + - os: ubuntu-latest + php-version: '8.0' + - os: ubuntu-latest + php-version: '8.1' + - os: ubuntu-latest + php-version: '8.2' + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + extensions: gd, intl, pdo_mysql + coverage: none # disable xdebug, pcov + + - name: Composer install + uses: ramsey/composer-install@v2 + with: + composer-options: '--ansi --prefer-dist' + + - name: Setup Problem Matchers for PHPUnit + run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Run unit tests + run: vendor/bin/phpunit tests/ --colors=always diff --git a/.gitignore b/.gitignore index 4fbb073..4d66ffe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ /composer.lock +/test.php diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..91188b6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Markus Staab + +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 index 552729b..930177d 100644 --- a/README.md +++ b/README.md @@ -43,3 +43,7 @@ includes: ``` + +## 💌 Give back some love + +[Consider supporting the project](https://github.com/sponsors/staabm), so we can make this tool even better even faster for everyone. diff --git a/composer.json b/composer.json index 579b1b7..8920a7b 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "require-dev": { "phpstan/phpstan": "^1.10", - "phpunit/phpunit": "^10.5" + "phpunit/phpunit": "^9 || ^10.5" }, "autoload": { diff --git a/phpstan.neon b/phpstan.neon index 8596575..d2702ec 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,12 @@ +includes: + - extension.neon + parameters: level: 8 paths: - src - - tests \ No newline at end of file + - tests + + excludePaths: + - tests/data/* diff --git a/src/TodoByRule.php b/src/TodoByRule.php index 3506bd6..76ac716 100644 --- a/src/TodoByRule.php +++ b/src/TodoByRule.php @@ -7,6 +7,9 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; +/** + * @implements Rule + */ final class TodoByRule implements Rule { private const PATTERN = '/^TODO:?\s*([0-9]{4}-[0-9]{2}-[0-9]{2})(.*)$/'; @@ -18,7 +21,10 @@ public function getNodeType(): string public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array { - if ($node instanceof VirtualNode) { + if ( + $node instanceof VirtualNode + || $node instanceof Node\Expr + ) { // prevent duplicate errors return []; } diff --git a/tests/TodoByRuleTest.php b/tests/TodoByRuleTest.php index c59f11b..77428c0 100644 --- a/tests/TodoByRuleTest.php +++ b/tests/TodoByRuleTest.php @@ -6,6 +6,9 @@ use PHPStan\Testing\RuleTestCase; use staabm\PHPStanTodoBy\TodoByRule; +/** + * @extends RuleTestCase + */ final class TodoByRuleTest extends RuleTestCase { protected function getRule(): Rule @@ -49,9 +52,13 @@ public function testRule(): void 27, ], [ - "'in method comment' expired on 2023-12-14.", + "'in method comment1' expired on 2023-12-14.", 29, ], + [ + "'in method comment2' expired on 2023-12-14.", + 31, + ], ]); } } diff --git a/tests/data/example.php b/tests/data/example.php index 16d029e..6e729bc 100644 --- a/tests/data/example.php +++ b/tests/data/example.php @@ -2,7 +2,7 @@ namespace ExampleTest; -function doFoo() { +function doFoo():void { } @@ -19,14 +19,15 @@ class X {} //TODO: 2023-12-14 Expired commentX // TODO: 2023-12-14 -function doFooBar() { +function doFooBar():void { } class Z { // TODO: 2023-12-14 method comment - public function XY() { - // TODO: 2023-12-14 in method comment - + public function XY():void { + // TODO: 2023-12-14 in method comment1 + $x = 1; + // TODO: 2023-12-14 in method comment2 } }