Skip to content

Commit 25d6883

Browse files
phpstan-botclaude
andcommitted
Update CLAUDE.md with accurate repo structure and setup instructions
- Fix PHPStan level (8, not max) to match phpstan.neon - Fix code style description to reference phpstan/build-cs instead of PSR-12 - Add make cs-install prerequisite for code style checks - Add missing AST source files (AbstractNodeVisitor, Attribute, Comment, NodeAttributes) - Add Doctrine annotation AST namespace documentation - Add missing test files (TokenIteratorTest, DifferTest, IntegrationPrinterWithPhpParserTest) - Add Initial Setup section with composer install and make cs-install - Add format-preserving printer code example from README - Add API reference and documentation links from README - Add note about PHP 7.4+ support vs phpstan-src PHP 8.1+ - Remove emoji from section headers Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 29872e9 commit 25d6883

File tree

1 file changed

+71
-40
lines changed

1 file changed

+71
-40
lines changed

CLAUDE.md

Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ This file contains important instructions for working on the phpdoc-parser proje
44

55
## Project Overview
66

7-
**phpstan/phpdoc-parser** is a library that represents PHPDocs with an Abstract Syntax Tree (AST). It supports parsing and modifying PHPDocs, and is primarily used by PHPStan for static analysis.
7+
**phpstan/phpdoc-parser** is a library that represents PHPDocs with an AST (Abstract Syntax Tree). It supports parsing and modifying PHPDocs, and is primarily used by PHPStan for static analysis.
8+
9+
* [PHPDoc Basics](https://phpstan.org/writing-php-code/phpdocs-basics) (list of PHPDoc tags)
10+
* [PHPDoc Types](https://phpstan.org/writing-php-code/phpdoc-types) (list of PHPDoc types)
11+
* [phpdoc-parser API Reference](https://phpstan.github.io/phpdoc-parser/2.3.x/namespace-PHPStan.PhpDocParser.html) with all the AST node types
812

913
### Key Features
1014
- Parses PHPDoc comments into an AST representation
11-
- Supports all PHPDoc tags and types (see [PHPStan documentation](https://phpstan.org/writing-php-code/phpdocs-basics))
12-
- Format-preserving printer for modifying and printing AST nodes
15+
- Supports all PHPDoc tags and types
16+
- Format-preserving printer for modifying and printing AST nodes (inspired by [nikic/PHP-Parser](https://github.com/nikic/PHP-Parser))
1317
- Support for Doctrine Annotations parsing
1418
- Nullable, intersection, generic, and conditional types support
1519

1620
### Requirements
1721
- PHP ^7.4 || ^8.0
18-
- Platform target: PHP 7.4.6
22+
- Platform target: PHP 7.4.6 (set in composer.json config)
23+
24+
Note: While phpstan-src is on PHP 8.1+ thanks to PHAR build downgrade, this repository still supports PHP 7.4+.
1925

2026
## Project Structure
2127

@@ -24,32 +30,37 @@ This file contains important instructions for working on the phpdoc-parser proje
2430
The source code is organized into the following main components:
2531

2632
1. **Lexer** (`src/Lexer/`)
27-
- `Lexer.php` - Tokenizes PHPDoc strings
33+
- `Lexer.php` - Tokenizes PHPDoc strings into tokens
2834

2935
2. **Parser** (`src/Parser/`)
3036
- `PhpDocParser.php` - Main PHPDoc parser (parses tags and structure)
3137
- `TypeParser.php` - Parses PHPDoc type expressions
3238
- `ConstExprParser.php` - Parses constant expressions
33-
- `TokenIterator.php` - Iterator for tokens
39+
- `TokenIterator.php` - Iterator for consuming tokens
3440
- `StringUnescaper.php` - Handles string unescaping
35-
- `ParserException.php` - Exception handling
41+
- `ParserException.php` - Exception handling for parse errors
3642

3743
3. **AST** (`src/Ast/`)
3844
- `Node.php` - Base AST node interface
39-
- `NodeTraverser.php` - Traverses and transforms AST
40-
- `NodeVisitor.php` - Visitor pattern for AST traversal
41-
- `Type/` - Type nodes (GenericTypeNode, ArrayTypeNode, UnionTypeNode, etc.)
42-
- `PhpDoc/` - PHPDoc tag nodes (ParamTagValueNode, ReturnTagValueNode, etc.)
43-
- `ConstExpr/` - Constant expression nodes
44-
- `NodeVisitor/` - Built-in visitors (CloningVisitor, etc.)
45+
- `NodeAttributes.php` - Trait for node attributes (lines, indexes, comments)
46+
- `NodeTraverser.php` - Traverses and transforms AST trees
47+
- `NodeVisitor.php` - Visitor interface for AST traversal
48+
- `AbstractNodeVisitor.php` - Abstract base class for node visitors
49+
- `Attribute.php` - Attribute constants (START_LINE, END_LINE, START_INDEX, END_INDEX, ORIGINAL_NODE, COMMENTS)
50+
- `Comment.php` - Represents a comment within a PHPDoc
51+
- `Type/` - Type nodes (GenericTypeNode, ArrayTypeNode, UnionTypeNode, IntersectionTypeNode, CallableTypeNode, ConditionalTypeNode, ArrayShapeNode, ObjectShapeNode, OffsetAccessTypeNode, etc.)
52+
- `PhpDoc/` - PHPDoc tag nodes (ParamTagValueNode, ReturnTagValueNode, VarTagValueNode, ThrowsTagValueNode, MethodTagValueNode, PropertyTagValueNode, TemplateTagValueNode, ExtendsTagValueNode, ImplementsTagValueNode, AssertTagValueNode, TypeAliasTagValueNode, SealedTagValueNode, etc.)
53+
- `PhpDoc/Doctrine/` - Doctrine annotation AST nodes (DoctrineAnnotation, DoctrineArgument, DoctrineArray, DoctrineArrayItem, DoctrineTagValueNode)
54+
- `ConstExpr/` - Constant expression nodes (ConstExprIntegerNode, ConstExprStringNode, ConstExprArrayNode, ConstFetchNode, etc.)
55+
- `NodeVisitor/` - Built-in visitors (CloningVisitor)
4556

4657
4. **Printer** (`src/Printer/`)
47-
- `Printer.php` - Prints AST back to PHPDoc format
48-
- `Differ.php` - Computes differences between AST nodes
49-
- `DiffElem.php` - Represents diff elements
58+
- `Printer.php` - Prints AST back to PHPDoc format (supports format-preserving printing)
59+
- `Differ.php` - Computes differences between AST node lists
60+
- `DiffElem.php` - Represents diff elements (keep, remove, add, replace)
5061

5162
5. **Configuration**
52-
- `ParserConfig.php` - Parser configuration (attributes to use)
63+
- `ParserConfig.php` - Parser configuration (used attributes: lines, indexes, comments)
5364

5465
### Tests (`tests/PHPStan/`)
5566

@@ -59,24 +70,27 @@ Tests mirror the source structure and include:
5970
- `TypeParserTest.php` - Type parsing tests
6071
- `PhpDocParserTest.php` - PHPDoc parsing tests
6172
- `ConstExprParserTest.php` - Constant expression parsing tests
73+
- `TokenIteratorTest.php` - Token iterator tests
6274
- `FuzzyTest.php` - Fuzzy testing
6375
- `Doctrine/` - Doctrine annotation test fixtures
6476

6577
2. **AST Tests** (`tests/PHPStan/Ast/`)
6678
- `NodeTraverserTest.php` - Node traversal tests
67-
- `Attributes/AttributesTest.php` - AST attribute tests
79+
- `Attributes/` - AST attribute tests
6880
- `ToString/` - Tests for converting AST to string
6981
- `NodeVisitor/` - Visitor pattern tests
7082

7183
3. **Printer Tests** (`tests/PHPStan/Printer/`)
72-
- Tests for format-preserving printing functionality
84+
- `PrinterTest.php` - Printer unit tests
85+
- `DifferTest.php` - Differ algorithm tests
86+
- `IntegrationPrinterWithPhpParserTest.php` - Integration tests with nikic/php-parser
7387

7488
### Configuration Files
7589

7690
- `phpunit.xml` - PHPUnit test configuration
77-
- `phpstan.neon` - PHPStan static analysis configuration
91+
- `phpstan.neon` - PHPStan static analysis configuration (level 8)
7892
- `phpstan-baseline.neon` - PHPStan baseline (known issues)
79-
- `phpcs.xml` - PHP CodeSniffer configuration
93+
- `phpcs.xml` - PHP CodeSniffer configuration (references `build-cs/phpcs.xml`)
8094
- `composer.json` - Dependencies and autoloading
8195

8296
## How the Parser Works
@@ -108,6 +122,28 @@ For format-preserving printing (used when modifying existing PHPDocs), enable th
108122
- `indexes` - Preserve token indexes
109123
- `comments` - Preserve comments
110124

125+
```php
126+
$config = new ParserConfig(usedAttributes: ['lines' => true, 'indexes' => true, 'comments' => true]);
127+
// ... setup lexer, parsers as above ...
128+
129+
$cloningTraverser = new NodeTraverser([new CloningVisitor()]);
130+
[$newPhpDocNode] = $cloningTraverser->traverse([$phpDocNode]);
131+
132+
// modify $newPhpDocNode...
133+
134+
$printer = new Printer();
135+
$newPhpDoc = $printer->printFormatPreserving($newPhpDocNode, $phpDocNode, $tokens);
136+
```
137+
138+
## Initial Setup
139+
140+
```bash
141+
composer install
142+
make cs-install
143+
```
144+
145+
The `make cs-install` step clones the [phpstan/build-cs](https://github.com/phpstan/build-cs) repository and installs its dependencies. This is required before running code style checks (`make cs` or `make cs-fix`).
146+
111147
## Common Development Tasks
112148

113149
### Adding a New PHPDoc Tag
@@ -164,13 +200,13 @@ make check
164200

165201
This runs:
166202
- `lint` - PHP syntax checking with parallel-lint
167-
- `cs` - Code style checking with phpcs
203+
- `cs` - Code style checking with phpcs (requires `make cs-install` first)
168204
- `tests` - PHPUnit test suite
169205
- `phpstan` - Static analysis
170206

171207
## CRITICAL RULES
172208

173-
### ⚠️ MANDATORY: Run After Every Change
209+
### MANDATORY: Run After Every Change
174210

175211
**You MUST run both tests and PHPStan after every code change:**
176212

@@ -185,7 +221,7 @@ make check
185221

186222
**DO NOT** commit or consider work complete until both tests and PHPStan pass successfully.
187223

188-
### ⚠️ NEVER Delete Tests
224+
### NEVER Delete Tests
189225

190226
**NEVER delete any tests.** Tests are critical to the project's quality and regression prevention. If tests are failing:
191227
- Fix the implementation to make tests pass
@@ -194,32 +230,34 @@ make check
194230

195231
## Other Available Commands
196232

233+
- `make cs-install` - Clone and install phpstan/build-cs (required before `make cs` or `make cs-fix`)
197234
- `make cs-fix` - Automatically fix code style issues
198235
- `make lint` - Check PHP syntax only
199236
- `make cs` - Check code style only
200237
- `make phpstan-generate-baseline` - Generate PHPStan baseline (use sparingly)
201238

202239
## Workflow Summary
203240

204-
1. Make code changes
205-
2. Run `make tests` - ensure all tests pass
206-
3. Run `make phpstan` - ensure static analysis passes
207-
4. Fix any issues found
208-
5. Commit only when both pass
209-
6. Repeat as needed
241+
1. Run `composer install` (initial setup)
242+
2. Run `make cs-install` (initial setup, required for code style checks)
243+
3. Make code changes
244+
4. Run `make tests` - ensure all tests pass
245+
5. Run `make phpstan` - ensure static analysis passes
246+
6. Fix any issues found
247+
7. Commit only when both pass
210248

211249
**Remember: Tests and PHPStan MUST pass before any commit.**
212250

213251
## Coding Standards and Best Practices
214252

215253
### Code Style
216-
- Follow PSR-12 coding standards (enforced by phpcs)
217-
- Use tabs for indentation (project convention)
254+
- Code style is enforced by phpcs using rules from [phpstan/build-cs](https://github.com/phpstan/build-cs)
255+
- Uses tabs for indentation (tab-width 4)
218256
- Run `make cs-fix` to automatically fix code style issues
219257
- Always run `make cs` to verify code style before committing
220258

221259
### PHPStan Rules
222-
- Project uses strict PHPStan rules (level max)
260+
- Project uses PHPStan level 8
223261
- All code must pass static analysis
224262
- Avoid adding to phpstan-baseline.neon unless absolutely necessary
225263
- Type hints are required for all public APIs
@@ -235,7 +273,6 @@ make check
235273
- All AST nodes implement the `Node` interface
236274
- Nodes should be immutable where possible
237275
- Use `__toString()` for debugging output
238-
- Implement proper equality checks if needed
239276
- Follow the visitor pattern for AST traversal
240277

241278
### Parser Patterns
@@ -258,9 +295,3 @@ make check
258295
- Avoid unnecessary object allocations
259296
- Be careful with regex patterns
260297
- Consider memory usage in loops
261-
262-
### Documentation
263-
- Public APIs should have PHPDoc comments
264-
- Complex logic should include inline comments
265-
- Update README.md when adding major features
266-
- Reference PHPStan documentation for PHPDoc tag specifications

0 commit comments

Comments
 (0)