.DEFAULT_GOAL := help

.PHONY: help
help: ## Display this message help
	@make -v | head -n 1
	@awk '\
		BEGIN {\
			FS = ":.*##";\
			printf "\n\033[33mUsage:\033[0m\n  make [target]\n\n\033[33mAvailable targets:\033[0m\n" \
		} /^[a-zA-Z0-9_-]+:.*?##/ { \
			printf "  \033[32m%-18s\033[0m %s\n", $$1, $$2 \
		} /^##/ { \
			printf "\033[33m %s\033[0m\n", substr($$0, 4) \
		}' $(MAKEFILE_LIST)

## Checks

check: phpstan-check rector-check cs-check phpunit ## Run all checks

.PHONY: phpstan-check
phpstan-check: vendor/ ## Run PHP Static Analysis
	vendor/bin/phpstan analyse --verbose

.PHONY: rector-check
rector-check: vendor/ ## Check for Rector coding standards violations
	vendor/bin/rector process --dry-run --no-progress-bar

.PHONY: cs-check
cs-check: vendor/ ## Check for coding standards violations
	vendor/bin/php-cs-fixer fix --dry-run --verbose

.PHONY: phpunit
phpunit: vendor/ ## Run PHPUnit tests
	php -d pcov.enabled=1 ./vendor/bin/phpunit --testdox --coverage-text --coverage-html var/phpunit/code-coverage-html --verbose

## Fixers

.PHONY: fix
fix: rector-fix cs-fix ## Run all fixers

.PHONY: rector-fix
rector-fix: vendor/ ## Fix Rector coding standards violations
	vendor/bin/rector process --no-progress-bar

.PHONY: cs-fix
cs-fix: vendor/ ## Fix coding standards violations
	vendor/bin/php-cs-fixer fix --verbose

## Misc

.PHONY: clean
clean: ## Clean up workspace
	rm -rf composer.lock var/ vendor/

vendor/: var/ composer.json
	composer install

var/:
	mkdir -p var
