Skip to content

Instantly share code, notes, and snippets.

@gnugat
Created October 24, 2025 09:19
Show Gist options
  • Save gnugat/437d6f6a2b1e254358f099ebcb569055 to your computer and use it in GitHub Desktop.
Save gnugat/437d6f6a2b1e254358f099ebcb569055 to your computer and use it in GitHub Desktop.
How to check and fix namespace / filename / PSR-4 mistakes
# Parameters (optional)
# * `arg`: arbitrary arguments to pass to rules (default: none)
# * `env`: used to set APP_ENV (default: test)
arg ?=
env ?= test
# Docker containers
PHP_SERVICE = app
# Executables, from the container
# FAST: disable "slow" XDebug when not needed
# FASTER: use local executable when the container is not needed
PHP = docker compose exec $(PHP_SERVICE) php
PHP_FAST = $(PHP) -dxdebug.mode=off
PHP_FASTER = php -d memory_limit=1G
COMPOSER = docker compose exec $(PHP_SERVICE) composer
COMPOSER_FASTER = composer
CONSOLE = $(PHP) bin/console
PHPUNIT = $(PHP) bin/phpunit
PHP_CS_FIXER = $(PHP_FASTER) bin/php-cs-fixer
PHPSTAN = $(PHP_FASTER) bin/phpstan
SWISS_KNIFE = $(PHP_FASTER) bin/swiss-knife
test: ## Runs tests using PHPUnit (arg, eg `arg='--testdox --filter Controller'`)
@$(PHPUNIT) $(arg)
cs-check: ## Checks CS with PHP CS Fixer (arg, eg `arg='./src'`)
@$(PHP_CS_FIXER) check --verbose $(arg)
swiss-knife: ## Runs Swiss Knife (arg, eg `arg='namespace-to-psr-4 src --namespace-root "App\\\\"'`)
@$(SWISS_KNIFE) $(arg)
php-cs-fixer: ## Coding Standards with PHP CS Fixer (arg, eg `arg='fix ./src/'`)
@$(PHP_CS_FIXER) $(arg)
phpstan: ## Static Analysis with phpstan (arg, eg `arg='analyze ./src/'`)
@$(PHPSTAN) $(arg)
static-analysis: ## phpstan + psalm
@$(MAKE) phpstan
@$(MAKE) psalm
qa: ## cs-check + static-analysis + test
@$(COMPOSER_FASTER) dump-autoload --optimize --strict-psr --strict-ambiguous
@$(MAKE) cs-check
@$(MAKE) static-analysis
@$(MAKE) test
cs-fix: ## Fixes CS with PHP CS Fixer and PSR-4 namespaces (arg, eg `arg='./src'`)
@$(SWISS_KNIFE) namespace-to-psr-4 src --namespace-root 'App\\'
@$(SWISS_KNIFE) namespace-to-psr-4 tests --namespace-root 'Tests\\'
@$(PHP_CS_FIXER) fix --verbose $(arg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment