summaryrefslogtreecommitdiffstats
path: root/Makefile
blob: bec4ef6cda555879b930448aec07751b645ef121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Development tasks.
#
#   make            list the targets
#   make test       run the suite with PHPUnit
#   make lint       syntax and style checks, no files touched
#   make build      validate the package and its PSR-4 autoloading
#
# The library itself has no Composer dependencies; PHPUnit is needed only to
# run the suite, and `make test` installs it for you.

PHP ?= php
COMPOSER ?= composer

SOURCES = $(shell find src tests -name '*.php')

# php-cs-fixer and phpcs are not dependencies of this package. Either one is
# used when it happens to be installed, from vendor/bin or from PATH.
FIXER = $(shell if [ -x vendor/bin/php-cs-fixer ]; then echo vendor/bin/php-cs-fixer; \
	else command -v php-cs-fixer 2>/dev/null; fi)
PHPCS = $(shell if [ -x vendor/bin/phpcs ]; then echo vendor/bin/phpcs; \
	else command -v phpcs 2>/dev/null; fi)
PHPCBF = $(shell if [ -x vendor/bin/phpcbf ]; then echo vendor/bin/phpcbf; \
	else command -v phpcbf 2>/dev/null; fi)

.DEFAULT_GOAL := help
.PHONY: help test lint format build check clean

help:
	@echo 'Targets:'
	@echo '  test       run the test suite'
	@echo '  lint       php -l over the tree, plus php-cs-fixer or phpcs when installed'
	@echo '  format     apply php-cs-fixer or phpcbf, when installed (writes)'
	@echo '  build      validate composer.json and the PSR-4 autoloading'
	@echo '  check      lint + test + build, in that order'
	@echo '  clean      remove installed dependencies and test artefacts'

vendor: composer.json
	$(COMPOSER) install --no-interaction
	@touch vendor

test: vendor
	$(PHP) vendor/bin/phpunit

# Read-only by design, so it is safe in CI and in a pre-commit hook: it reports
# what `make format` would change without changing anything.
lint:
	@set -e; for f in $(SOURCES); do $(PHP) -l $$f >/dev/null; done
	@echo 'syntax OK: $(words $(SOURCES)) files'
ifneq ($(FIXER),)
	$(FIXER) fix --dry-run --diff
else ifneq ($(PHPCS),)
	$(PHPCS) --standard=PSR12 src tests
else
	@echo 'php-cs-fixer and phpcs not installed: syntax checks only'
endif

format:
ifneq ($(FIXER),)
	$(FIXER) fix
else ifneq ($(PHPCBF),)
	$(PHPCBF) --standard=PSR12 src tests
else
	@echo 'php-cs-fixer and phpcbf not installed: nothing to format with'
endif

# PHP compiles nothing, so the equivalent of a build is proving the package
# metadata is valid and that every class is where PSR-4 says it is.
build:
	$(COMPOSER) validate --strict
	$(COMPOSER) dump-autoload --optimize --strict-psr

# The order matters: lint is the fastest to fail, so a broken run reports the
# cheapest problem first.
check: lint test build

clean:
	rm -rf vendor .phpunit.cache .php-cs-fixer.cache