Skip to content

入門編の増補と、本文とPHPファイルの整合性チェック - #2

Merged
zonuexe merged 6 commits into
masterfrom
update/beginner-2
Aug 19, 2026
Merged

入門編の増補と、本文とPHPファイルの整合性チェック#2
zonuexe merged 6 commits into
masterfrom
update/beginner-2

Conversation

@zonuexe

@zonuexe zonuexe commented Aug 18, 2026

Copy link
Copy Markdown
Owner

概要

入門編 (beginner) を増補し、あわせて Markdown 本文と PHP ファイル・PHPStan の実際の出力が矛盾しないようにする仕組みを追加しました。

入門編の増補

  • 1.5. 「型は追跡され、追跡できなくなると広がる」を独立した節・ファイル (beginner/1.5.php) として追加。定数型 (5, 6) と整数範囲型 (int<0, max>, positive-int) を dumpType() で体感する
  • 各スクリプトと解答例に use function PHPStan\dumpType; / assertType; を追加。§1 の TIP で「dumpType() とも書けるが、本文は \PHPStan\dumpType() で統一する」と明記
  • 2. 「エラーメッセージを読む」小節を追加。初期状態の4エラーを1つずつ読み下し、mixed の意味、レベル (Playground の Level 10)、エラー識別子を紹介
  • 3. Author/Book クラスの掲載と TIP (コンストラクタプロモーション / readonly class / non-empty-array)、「ほかの絞り込み方」小節 (比較演算子 → int<1, 100>is_string()mixed から string)、!$wordnon-falsy-string になる注意
  • 各演習の IMPORTANT に解答例へのリンクを追加、修了時のまとめを更新

整合性チェックの仕組み (composer check)

tools/check-docs.php が README のコードブロックを検査します。詳細は CONTRIBUTING.md

  • ```php file=3.php — ブロックが演習ファイルの連続する行と一致すること (タブ/スペース差は無視、// ... で省略可)
  • ```php phpstan — ブロック単体を PHPStan で解析
  • // DumpedType: X / // Error: message 注釈 (行末または直前行) が実際の出力と過不足なく一致すること
  • answers/**/*.php が単体でエラーなしであること
  • README 内の相対リンク先が存在すること

.github/workflows/check.yml で PR/push 時に実行します。

解答例 answers/

answers/beginner/2.php のように配置しました。演習ファイルと同じディレクトリに置くと phpstan analyse の一括実行で label()Book の定義が衝突して誤検出が出るため (excludePaths で除くと今度は明示指定でも解析されない)、paths 外に置いています。

Playground リンクの検査・更新 (tools/playground.php)

Playground の Share ボタンと同じ API (POST https://api.phpstan.org/analyse + saveResult: true) を叩き、GET /sample?id= で保存内容を照合します。

  • composer check-playground — リンク先サンプルのコード・設定がローカルの演習ファイルと一致するか (読み取りのみ、CI で実行)
  • composer update-playground — 不一致/TODO のリンクを新規発行して README を書き換え (--dry-run あり)

全節のリンクを再生成済みです (Level 10, bleedingEdge オン, strictRules オフ = ローカル設定と同じ)。

TODO (Draft の理由)

  • 各節の PHPStan Playground リンクを再生成する
  • 文言のレビュー

動作確認

$ composer check
[OK] No errors        # tools/ を level max で解析
[OK] 33 checks passed # 本文と PHP ファイルの整合性

- ```php file=N.php``` blocks must match the file contents
- ```php phpstan``` blocks are analysed standalone
- // DumpedType: / // Error: annotations are verified against real PHPStan output
- answers/ are analysed one by one and must be error-free
- composer check runs everything; GitHub Actions runs it on push/PR
Placed outside phpstan.dist.neon paths because they redeclare the same
symbols as the exercise files and would collide in a whole-project run.
- 1: constant types, integer range types (int<0, max>), tracking vs widening
- 2: how to read PHPStan error messages, mixed, levels, error identifiers
- 3: show Author/Book, comparison-operator and is_*() narrowing, !$word caveat
- link answer files from each exercise
- tag code blocks with file=/phpstan and DumpedType/Error annotations
- Move the constant/integer-range examples out of 1.php into a new
  section 1.5 with its own file, so section 1 stays focused
- Add `use function PHPStan\dumpType;` / `assertType;` to every
  script and answer; explain that dumpType() may be written unqualified
  while the text keeps \PHPStan\dumpType() for consistency
- Playground links now need regeneration (TODO comments per section)
The Playground's Share button POSTs to https://api.phpstan.org/analyse
with saveResult: true and gets back an id for https://phpstan.org/r/<id>.

- check: fetch each linked sample (GET /sample?id=) and compare its code
  and config with the local file (read-only; runs in CI)
- update: publish stale/missing samples, rewrite the README link and drop
  the <!-- TODO --> marker (--dry-run to preview)
- config mirrors phpstan.dist.neon: level 10, bleedingEdge on, strictRules off
- Read the HTTP status from stream_get_meta_data() instead of the
  $http_response_header variable, which is deprecated in PHP 8.5
- Iterate the live lines array by index; foreach iterated a copy, so
  after removing a TODO line the indices no longer matched and the
  **File** line was not found for the following sections
- Publish new samples for all sections (1, 1.5, 2, 3, 4, basic 1) and
  drop the TODO markers
@zonuexe
zonuexe force-pushed the update/beginner-2 branch from c3adbfb to 1fb39dc Compare August 19, 2026 00:28
@zonuexe
zonuexe marked this pull request as ready for review August 19, 2026 06:38
@zonuexe
zonuexe merged commit 925ab54 into master Aug 19, 2026
1 check passed
@zonuexe
zonuexe deleted the update/beginner-2 branch August 19, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant