🍰
CakePHP で Lefthook を使ってみる
はじめに
Docker で開発している CakePHP プロジェクトで Lefthook 経由で PHPStan と PHP_CodeSniffer 動作させてみたので備忘録。
ディレクトリ構成
以下のようなディレクトリ構成を想定。
.
├── .git
├── docker
│ └── compose.yaml
├── lefthook.yml
└── src
├── cake
│ ├── README.md
│ ├── bin
│ ├── composer.json
│ ├── composer.lock
│ ├── config
│ ├── index.php
│ ├── phpcs.xml
│ ├── phpstan.neon
│ ├── phpunit.xml.dist
│ ├── plugins
│ ├── psalm.xml
│ ├── resources
│ ├── src
│ ├── templates
│ ├── tests
│ ├── vendor
│ └── webroot
└── webroot
└── index.php
src/cake
ディレクトリは以下のコマンドで作成したもの。
% composer create-project --prefer-dist cakephp/app:5 cake
セットアップ
compose.yaml
コンテナ内の Web サーバのドキュメントルートは /srv/httpd/webroot
の想定。
services:
web:
container_name: hogehoge
volumes:
- ../src:/srv/httpd
# 後はよしなに
db:
# よしなに
db-test:
# よしなに
lefthook.yml
pre-commit:
commands:
1_phpstan:
tags: [dev, scan]
root: src/cake
glob: "*.php"
run: docker exec -t hogehoge
bash -c "cd /srv/httpd/cake && ./vendor/bin/phpstan analyse {staged_files}"
2_phpcs:
tags: [dev, check]
root: src/cake
glob: "*.php"
run: docker exec -t hogehoge
bash -c "cd /srv/httpd/cake && ./vendor/bin/phpcs --colors -p {staged_files}"
動作確認
以下のようなファイルを作成してコミットしてみる。
cake/src/Hoge.php
<?php
class Hoge {
public function __construct() {
echo 'Hello, World!';
}
public function hello() {
return 'Hello, World!';
}
}
% git commit
╭────────────────────────────────────────╮
│ 🥊 lefthook v1.10.10 hook: pre-commit │
╰────────────────────────────────────────╯
┃ 1_phpstan ❯
Note: Using configuration file /srv/httpd/cake/phpstan.neon.
1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
------ ----------------------------------------------------
Line Hoge.php
------ ----------------------------------------------------
6 Method Hoge::hello() has no return type specified.
🪪 missingType.return
------ ----------------------------------------------------
[ERROR] Found 1 error
┃ 2_phpcs ❯
E 1 / 1 (100%)
FILE: /srv/httpd/cake/src/Hoge.php
----------------------------------------------------------------------
FOUND 9 ERRORS AFFECTING 5 LINES
----------------------------------------------------------------------
1 | ERROR | [x] Missing declare(strict_types=1).
2 | ERROR | [ ] Each class must be in a namespace of at least one
| | level (a top-level vendor name)
2 | ERROR | [x] Opening brace of a class must be on the line after
| | the definition
3 | ERROR | [ ] Missing doc comment for function __construct()
3 | ERROR | [x] Opening brace should be on a new line
5 | ERROR | [x] Every function/method needs a newline afterwards
6 | ERROR | [ ] Missing doc comment for function hello()
6 | ERROR | [ ] Method \Hoge::hello() does not have return type hint
| | nor @return annotation for its return value.
6 | ERROR | [x] Opening brace should be on a new line
----------------------------------------------------------------------
PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Time: 145ms; Memory: 6MB
────────────────────────────────────
summary: (done in 2.56 seconds)
🥊 1_phpstan
🥊 2_phpcs
環境
% sw_vers
ProductName: macOS
ProductVersion: 15.2
BuildVersion: 24C101
% docker --version
Docker version 27.4.0, build bde2b89
% lefthook version
1.10.10
Discussion