Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
PHPStanのエラーをprettyにしようとしている
Search
Natsuki
November 22, 2025
39
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PHPStanのエラーをprettyにしようとしている
Natsuki
November 22, 2025
More Decks by Natsuki
See All by Natsuki
Reactのいいなと思ったところ
natsukiengr
1
1k
jsが型安全になったっていい
natsukiengr
0
220
PHPStanをチームに内緒で開発に取り入れる方法
natsukiengr
1
1.9k
Featured
See All Featured
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
GitHub's CSS Performance
jonrohan
1033
470k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7.1k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
720
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
Technical Leadership for Architectural Decision Making
baasie
3
610
Prompt Engineering for Job Search
mfonobong
0
530
Rails Girls Zürich Keynote
gr2m
96
14k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.7k
Transcript
PHPStanのエラーをprettyにしようとしている Natsuki
PHPStan使ってますか public ?int $age return $this->age + 1; <?php class
User { public function __construct( public string $name, ) {} public function nextAge(): int { } }
VSCodeのエラーは見づら過ぎる!! /** * @return array{ * id: int, * name:
string, * // ... * } */ function getProfile(): array { return [ 'id' => 123, 'name' => 'John Doe', // ... ]; }
pretty-ts-errorsみたいなのが欲しい
エラー文をパスーすればいいのでは(1) php-yacc 第179回 PHP勉強会@東京でのきんじょうさんの話 オフラインです
エラー文をパスーすればいいのでは(2) racc Umeda.rbにて わいださん のraccの話 ぺちこん関西2025でのトーク LRパーサーとか難しいのでなんとなくの理解
パーサーを使わないアプローチ パーサーは作らずエラーパターンからマッチさせる /Function 関数名 should return 型(array{...}) but returns 型(array{...})./
エラーの種類だけ、パターンマッチで拾う エラーパターンの変更を追従するのが大変 & プラグインの数だけ対応が必要 => 汎用的にエラー文をパースできた方がいい
ちなみにpretty-ts-errorsは正規表現 https://github.com/yoavbls/pretty-ts- errors/blob/8527285178e9a88c80ad63fa7f0129192b67bad2/packages/formatter/ TSはエラーパターンが決まってるからできる `${pre}${formatTypeBlock("", type, codeBlock)}: <ul>${post .split(", ")
.filter(Boolean) .map((prop: string) => `<li>${prop}</li>`) .join("")}</ul>` message .replaceAll( /(is missing the following properties from type\s?)'(.*)': ((?:#?\w+, )*(?:(?!and)\w+)?)/g, (_, pre, type, post) => )
Chevrotain TypeScript製のパーサージェネレーター マメジカ
例
例 this.RULE("object", () => { this.CONSUME(LCurly); this.MANY_SEP({ SEP: Comma, DEF:
() => { this.SUBRULE(this.objectItem); }, }); this.CONSUME(RCurly); }); class JsonParser extends CstParser { constructor() { this.RULE("json", () => { this.OR([ { ALT: () => this.SUBRULE(this.object) }, { ALT: () => this.SUBRULE(this.array) }, ]); }); this.RULE("objectItem", () => { this.CONSUME(StringLiteral); this.CONSUME(Colon); this.SUBRULE(this.value);
パーサーの定義の前に字句解析(Lexer) const True = createToken({ name: "True", pattern: /true/ });
const False = createToken({ name: "False", pattern: /false/ }); const Null = createToken({ name: "Null", pattern: /null/ }); const LCurly = createToken({ name: "LCurly", pattern: /{/ }); const RCurly = createToken({ name: "RCurly", pattern: /}/ });
phpstan-error-parserを作っています
使い方 import { parse } from 'phpstan-error-parser'; const result =
parse('PHPDoc tag @mixin contains unresolvable type.') [ { type: 'common_word', value: 'PHPDoc', location: { startColumn: 0, endColumn: 6, }, }, { type: 'common_word', value: 'tag', location: { startColumn: 7, endColumn: 10, }, }, { type: 'doc_tag', value: '@mixin',
VSCode拡張の組み込み アプローチは二つ pretty-ts-errorsのように別拡張として提供 phpstan-vscode自体に組み込む
pretty-ts-errorsとphpstan-vscodeがエラーを表示 している仕組み pretty-ts-errors ⇒ Diagnostic phpstan-vscode ⇒ HoverProvider
Diagnostic(診断) 場所とメッセージを渡すとエラー表示してくれる https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnostic phpstan-vscodeはPHPStanの結果をこれでVSCodeに渡している messageにMarkdownを渡せばリッチな表示ができる? ⇒ できない🙅 interface Diagnostic {
range: Range; severity?: DiagnosticSeverity; source?: string; message: string; }
HoverProvider ホバーイベントに応じてメッセージを返す https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover `Hover`ではcontentsにMarkdownを渡せばリッチな表示ができる❗️❗️ contents: MarkedString | MarkedString[] | MarkupContent;
interface Hover { /** * The hover's content */ /** * An optional range is a range inside a text document * that is used to visualize a hover, e.g. by changing the background color. */ range?: Range; }
pretty-ts-errorsでもそれによるDiscussionがある https://github.com/yoavbls/pretty-ts-errors/discussions/43
LSP 3.18 ならDiagnosticにもMarkdownが使える https://microsoft.github.io/language-server-protocol/specifications/lsp/3.18/specification/#diagnostic いつになるか分からないので 別拡張としてphpstan-error-parserでパースして、HoverProviderで実装する予 定 おしまい