Skip to content

Commit

Permalink
ci: add psalm static analysis
Browse files Browse the repository at this point in the history
test: separate phpunit and pest tests

test: add arch pest tests

deps: add support for rector
  • Loading branch information
lotyp committed Jun 5, 2024
1 parent 78b8f41 commit e7a0ab1
Show file tree
Hide file tree
Showing 17 changed files with 480 additions and 135 deletions.
63 changes: 59 additions & 4 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@ on: # yamllint disable-line rule:truthy
branches:
- master
paths:
- 'phpstan*'
- 'psalm*'
- 'composer.*'
- 'src/**'
- 'tests/**'
- 'bin/trap'
- '.php-cs-fixer.dist.php'
pull_request:
branches:
- master
paths:
- 'phpstan*'
- 'psalm*'
- 'composer.*'
- 'src/**'
- 'tests/**'
- 'bin/trap'
- '.php-cs-fixer.dist.php'

name: 🔍 Static analysis

jobs:
static-analysis:
phpstan:
timeout-minutes: 4
runs-on: ${{ matrix.os }}
concurrency:
cancel-in-progress: true
group: static-analysis-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
group: phpstan-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
strategy:
fail-fast: true
matrix:
Expand Down Expand Up @@ -71,3 +75,54 @@ jobs:

- name: 🔍 Run static analysis using phpstan/phpstan
run: composer stan:ci

psalm:
timeout-minutes: 4
runs-on: ${{ matrix.os }}
concurrency:
cancel-in-progress: true
group: psalm-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
strategy:
fail-fast: true
matrix:
os:
- ubuntu-latest
php-version:
- '8.1'
dependencies:
- locked
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4.1.6

- name: 🛠️ Setup PHP
uses: shivammathur/setup-php@2.30.4
with:
php-version: ${{ matrix.php-version }}
extensions: none, ctype, curl, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter, opcache, pcntl, posix
ini-values: error_reporting=E_ALL
coverage: xdebug

- name: 🛠️ Setup problem matchers
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

- name: 🤖 Validate composer.json and composer.lock
run: composer validate --ansi --strict

- name: 🔍 Get composer cache directory
uses: wayofdev/gh-actions/actions/composer/get-cache-directory@v3.1.0

- name: ♻️ Restore cached dependencies installed with composer
uses: actions/cache@v4.0.2
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}
restore-keys: php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-

- name: 📥 Install "${{ matrix.dependencies }}" dependencies
uses: wayofdev/gh-actions/actions/composer/install@v3.1.0
with:
dependencies: ${{ matrix.dependencies }}

- name: 🔍 Run static analysis using vimeo/psalm
run: composer psalm:ci
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="composer-normalize" version="^2.42.0" installed="2.42.0" location="./.phive/composer-normalize" copy="false"/>
<phar name="composer-require-checker" version="^4.10.0" installed="4.10.0" location="./.phive/composer-require-checker" copy="false"/>
<phar name="composer-require-checker" version="^4.11.0" installed="4.11.0" location="./.phive/composer-require-checker" copy="false"/>
</phive>
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ repos:
description: Run markdownlint-cli2 on your Markdown files using the docker image
language: docker_image
types: [markdown]
entry: davidanson/markdownlint-cli2-rules:latest
entry: 'davidanson/markdownlint-cli2-rules:latest'
args: ['--config', '.github/.markdownlint.json']

...
23 changes: 18 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ENVSUBST ?= $(BUILDER) envsubst
YAML_LINT_RUNNER ?= $(DOCKER) run --rm $$(tty -s && echo "-it" || echo) \
-v $(PWD):/data \
cytopia/yamllint:latest \
-c ./.github/.yamllint.yaml \
-f colored .

ACTION_LINT_RUNNER ?= $(DOCKER) run --rm $$(tty -s && echo "-it" || echo) \
Expand All @@ -47,7 +48,8 @@ ACTION_LINT_RUNNER ?= $(DOCKER) run --rm $$(tty -s && echo "-it" || echo) \
MARKDOWN_LINT_RUNNER ?= $(DOCKER) run --rm $$(tty -s && echo "-it" || echo) \
-v $(shell pwd):/app \
--workdir /app \
davidanson/markdownlint-cli2-rules:latest
davidanson/markdownlint-cli2-rules:latest \
--config ".github/.markdownlint.json"

PHIVE_RUNNER ?= $(DOCKER_COMPOSE) run --rm --no-deps app

Expand Down Expand Up @@ -94,9 +96,9 @@ help: ## Show this menu
@echo
@echo ' 📑 Logs are stored in $(MAKE_LOGFILE)'
@echo
@echo ' 📦 Package php-cs-fixer-config (github.com/wayofdev/php-cs-fixer-config)'
@echo ' 🤠 Makefile Author Andrij Orlenko (github.com/lotyp)'
@echo ' 🏢 ${YELLOW}Org wayofdev (github.com/wayofdev)${RST}'
@echo ' 📦 Package php-cs-fixer-config (https://github.com/wayofdev/php-cs-fixer-config)'
@echo ' 🤠 Makefile Author Andrij Orlenko (https://github.com/lotyp)'
@echo ' 🏢 ${YELLOW}Org wayofdev (https://github.com/wayofdev)${RST}'
@echo
.PHONY: help

Expand Down Expand Up @@ -140,6 +142,10 @@ down: # Stops and removes containers of this project
$(DOCKER_COMPOSE) down --remove-orphans --volumes
.PHONY: down

stop: ## Stops all containers, without removing them
$(DOCKER_COMPOSE) stop
.PHONY: stop

restart: down up ## Runs down and up commands
.PHONY: restart

Expand Down Expand Up @@ -267,10 +273,17 @@ infect-ci: ## Runs infection – mutation testing framework with github output (
$(APP_COMPOSER) infect:ci
.PHONY: lint-infect-ci

test: ## Run project php-unit and pest tests
test: test-unit test-arch ## Run project php-unit and pest tests
.PHONY: test

test-unit: ## Run project php-unit tests
$(APP_COMPOSER) test
.PHONY: test

test-arch: ## Run project pest tests with architecture checks
$(APP_COMPOSER) test:arch
.PHONY: test-arch

test-cc: ## Run project php-unit and pest tests in coverage mode and build report
$(APP_COMPOSER) test:cc
.PHONY: test-cc
Expand Down
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,30 +73,30 @@ composer req --dev wayofdev/cs-fixer-config

```php
<?php

declare(strict_types=1);

use WayOfDev\PhpCsFixer\Config\ConfigBuilder;
use WayOfDev\PhpCsFixer\Config\RuleSets\DefaultSet;

require_once 'vendor/autoload.php';

$config = ConfigBuilder::createFromRuleSet(new DefaultSet())
->inDir(__DIR__ . '/src')
->inDir(__DIR__ . '/tests')
->addFiles([__FILE__])
->getConfig()
;

$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/php-cs-fixer.cache');

return $config;
```

### → Composer Script

* Add `scripts` section to `composer.json`:

```diff
{
"scripts": {
Expand Down Expand Up @@ -126,11 +126,11 @@ composer req --dev wayofdev/cs-fixer-config
+prepare:
+ mkdir -p .build/php-cs-fixer
+.PHONY: prepare

+lint-php: prepare ## Fixes code to follow coding standards using php-cs-fixer
+ $(APP_COMPOSER) cs:fix
+.PHONY: lint-php

+lint-diff: prepare ## Runs php-cs-fixer in dry-run mode and shows diff which will by applied
+ $(APP_COMPOSER) cs:diff
+.PHONY: lint-diff
Expand All @@ -148,17 +148,17 @@ composer req --dev wayofdev/cs-fixer-config

```yaml
---

on: # yamllint disable-line rule:truthy
pull_request:
branches:
- master
push:
branches:
- master

name: 🧹 Fix PHP coding standards

jobs:
coding-standards:
timeout-minutes: 4
Expand All @@ -181,48 +181,48 @@ composer req --dev wayofdev/cs-fixer-config
run: |
git config --global core.autocrlf false
git config --global core.eol lf

- name: 🛠️ Setup PHP
uses: shivammathur/setup-php@2.30.4
with:
php-version: ${{ matrix.php-version }}
extensions: none, ctype, dom, json, mbstring, phar, simplexml, tokenizer, xml, xmlwriter
ini-values: error_reporting=E_ALL
coverage: none

- name: 📦 Check out the codebase
uses: actions/checkout@v4.1.5

- name: 🛠️ Setup problem matchers
run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"

- name: 🤖 Validate composer.json and composer.lock
run: composer validate --ansi --strict

- name: 🔍 Get composer cache directory
uses: wayofdev/gh-actions/actions/composer/get-cache-directory@v3.1.0

- name: ♻️ Restore cached dependencies installed with composer
uses: actions/cache@v4.0.2
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}
restore-keys: php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-

- name: 📥 Install "${{ matrix.dependencies }}" dependencies with composer
uses: wayofdev/gh-actions/actions/composer/install@v3.1.0
with:
dependencies: ${{ matrix.dependencies }}

- name: 🛠️ Prepare environment
run: make prepare

- name: 🚨 Run coding standards task
run: composer cs:fix
env:
PHP_CS_FIXER_IGNORE_ENV: true

- name: 📤 Commit and push changed files back to GitHub
uses: stefanzweifel/git-auto-commit-action@v5.0.1
with:
Expand Down Expand Up @@ -278,7 +278,7 @@ To use via composer script commands:
* Runs php-cs-fixer in dry-run mode and shows diff which will by applied:

```bash
make lint-diff
make lint-diff
```

<br>
Expand Down Expand Up @@ -320,7 +320,7 @@ You are more than welcome. Before contributing, kindly check our [contribution g
* **Discord:** Join our community on [Discord](https://discord.gg/CE3TcCC5vr).

<p align="left">
<a href="https://discord.gg/CE3TcCC5vr" target="_blank"><img alt="Codecov" src="https://img.shields.io/discord/1228506758562058391?style=for-the-badge&logo=discord&labelColor=7289d9&logoColor=white&color=39456d"></a>
<a href="https://discord.gg/CE3TcCC5vr" target="_blank"><img alt="Discord Link" src="https://img.shields.io/discord/1228506758562058391?style=for-the-badge&logo=discord&labelColor=7289d9&logoColor=white&color=39456d"></a>
<a href="https://x.com/intent/follow?screen_name=wayofdev" target="_blank"><img alt="Follow on Twitter (X)" src="https://img.shields.io/badge/-Follow-black?style=for-the-badge&logo=X"></a>
</p>

Expand Down
47 changes: 32 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"phpstan/phpstan-strict-rules": "^1.6",
"phpunit/phpunit": "^10.5",
"psalm/plugin-phpunit": "~0.19.0",
"rector/rector": "^1.1",
"roave/infection-static-analysis-plugin": "^1.35",
"vimeo/psalm": "^5.23.1"
},
Expand All @@ -49,7 +50,7 @@
},
"autoload-dev": {
"psr-4": {
"WayOfDev\\PhpCsFixer\\Config\\Tests\\": "tests/"
"WayOfDev\\Tests\\": "tests/"
}
},
"config": {
Expand All @@ -74,19 +75,35 @@
}
},
"scripts": {
"cs:diff": "php vendor/bin/php-cs-fixer fix --dry-run -v --diff",
"cs:fix": "php vendor/bin/php-cs-fixer fix -v",
"infect": "XDEBUG_MODE=coverage php vendor/bin/roave-infection-static-analysis-plugin --configuration=infection.json.dist",
"infect:ci": "XDEBUG_MODE=coverage php vendor/bin/roave-infection-static-analysis-plugin --ansi --configuration=infection.json.dist --logger-github --ignore-msi-with-no-mutations --only-covered",
"psalm": "php vendor/bin/psalm --show-info=true",
"psalm:baseline": "php vendor/bin/psalm --set-baseline=psalm-baseline.xml",
"psalm:ci": "php vendor/bin/psalm --output-format=github --shepherd --show-info=false --stats --threads=4",
"refactor": "php vendor/bin/rector process --config=rector.php",
"refactor:ci": "php vendor/bin/rector process --config=rector.php --dry-run --ansi",
"stan": "php vendor/bin/phpstan analyse --memory-limit=2G",
"stan:baseline": "php vendor/bin/phpstan analyse --generate-baseline --memory-limit=2G --allow-empty-baseline",
"stan:ci": "php vendor/bin/phpstan analyse --memory-limit=2G --error-format=github",
"test": "XDEBUG_MODE=coverage php vendor/bin/pest",
"test:cc": "XDEBUG_MODE=coverage php vendor/bin/pest --coverage --coverage-clover=.build/phpunit/logs/clover.xml"
"cs:diff": "php-cs-fixer fix --dry-run -v --diff",
"cs:fix": "php-cs-fixer fix -v",
"infect": [
"@putenv XDEBUG_MODE=coverage",
"roave-infection-static-analysis-plugin"
],
"infect:ci": [
"@putenv XDEBUG_MODE=coverage",
"roave-infection-static-analysis-plugin --ansi --logger-github --ignore-msi-with-no-mutations --only-covered"
],
"psalm": "psalm --show-info=true",
"psalm:baseline": "psalm --set-baseline=psalm-baseline.xml",
"psalm:ci": "psalm --output-format=github --shepherd --show-info=false --stats --threads=4",
"refactor": "rector process --config=rector.php",
"refactor:ci": "rector process --config=rector.php --dry-run --ansi",
"stan": "phpstan analyse --memory-limit=2G",
"stan:baseline": "phpstan analyse --generate-baseline --memory-limit=2G --allow-empty-baseline",
"stan:ci": "phpstan analyse --memory-limit=2G --error-format=github",
"test": [
"@putenv XDEBUG_MODE=coverage",
"pest --color=always"
],
"test:arch": [
"@putenv XDEBUG_MODE=coverage",
"pest --color=always --configuration pest.xml.dist"
],
"test:cc": [
"@putenv XDEBUG_MODE=coverage",
"pest --coverage --coverage-clover=.build/phpunit/logs/clover.xml"
]
}
}
Loading

0 comments on commit e7a0ab1

Please sign in to comment.