Skip to content

Commit 65a184f

Browse files
committed
added CLAUDE.md
1 parent d519f36 commit 65a184f

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.gitattributes export-ignore
22
.github/ export-ignore
33
.gitignore export-ignore
4+
CLAUDE.md export-ignore
45
ncs.* export-ignore
56
phpstan*.neon export-ignore
67
tests/ export-ignore

CLAUDE.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
Nette Code Checker is a command-line tool that checks and optionally repairs formal errors in source code. It's distributed as a standalone tool (not a library dependency) and validates multiple file formats including PHP, Latte templates, NEON, JSON, and YAML.
8+
9+
## Essential Commands
10+
11+
### Running Code Checker
12+
13+
```bash
14+
# Check current directory (read-only mode)
15+
php code-checker
16+
17+
# Check and fix files
18+
php code-checker --fix
19+
20+
# Check specific directory
21+
php code-checker -d path/to/check
22+
23+
# Check with strict_types validation
24+
php code-checker --strict-types
25+
26+
# Normalize line endings
27+
php code-checker --eol
28+
29+
# Syntax-only checks (faster)
30+
php code-checker --only-syntax
31+
```
32+
33+
### Development Commands
34+
35+
```bash
36+
# Run all tests
37+
composer run tester
38+
# or
39+
vendor/bin/tester tests -s -C
40+
41+
# Run single test file
42+
vendor/bin/tester tests/Tasks.latteSyntaxChecker.phpt -s -C
43+
44+
# Run PHPStan static analysis
45+
composer run phpstan
46+
# or
47+
phpstan analyse
48+
```
49+
50+
## Architecture Overview
51+
52+
### Core Components
53+
54+
The codebase has a simple, task-based architecture:
55+
56+
1. **Checker** (`src/Checker.php`): Main orchestrator that:
57+
- Scans directories using file patterns
58+
- Processes each file through registered tasks
59+
- Handles read-only vs fix mode
60+
- Reports errors/warnings/fixes with color-coded output
61+
62+
2. **Tasks** (`src/Tasks.php`): Collection of static validation/fixing methods:
63+
- Each task is a static method with signature: `(string &$contents, Result $result): void`
64+
- Tasks can modify `$contents` (for fixers) or just add messages to `$result`
65+
- Tasks are pattern-matched to file types (e.g., `*.php`, `*.latte`)
66+
67+
3. **Result** (`src/Result.php`): Accumulates messages from tasks:
68+
- `error()`: Critical issues that prevent acceptance
69+
- `warning()`: Non-critical issues
70+
- `fix()`: Issues that were/can be automatically fixed
71+
72+
4. **bootstrap.php** (`src/bootstrap.php`): Entry point that:
73+
- Parses command-line arguments
74+
- Registers tasks based on options
75+
- Runs the checker
76+
- Returns appropriate exit codes
77+
78+
### Task Registration Pattern
79+
80+
Tasks are registered in `bootstrap.php` with optional file patterns:
81+
82+
```php
83+
// Apply to all files
84+
$checker->addTask([$tasks, 'controlCharactersChecker']);
85+
86+
// Apply only to specific file types
87+
$checker->addTask([$tasks, 'phpSyntaxChecker'], '*.php,*.phpt');
88+
89+
// Conditional registration
90+
if (isset($options['--strict-types'])) {
91+
$checker->addTask([$tasks, 'strictTypesDeclarationChecker'], '*.php,*.phpt');
92+
}
93+
```
94+
95+
### Task Implementation Patterns
96+
97+
**Checker tasks** (read-only validation):
98+
```php
99+
public static function taskName(string $contents, Result $result): void
100+
{
101+
if (/* check fails */) {
102+
$result->error('Error message', $lineNumber);
103+
}
104+
}
105+
```
106+
107+
**Fixer tasks** (modify contents):
108+
```php
109+
public static function taskName(string &$contents, Result $result): void
110+
{
111+
$new = /* transform contents */;
112+
if ($new !== $contents) {
113+
$result->fix('description of fix', $lineNumber);
114+
$contents = $new;
115+
}
116+
}
117+
```
118+
119+
### Latte Syntax Checker Integration
120+
121+
The `latteSyntaxChecker` task demonstrates integration with Nette ecosystem:
122+
- Creates a full Latte engine with all standard extensions (UI, Forms, Cache, Assets)
123+
- Compiles templates to PHP code
124+
- Validates generated PHP using `phpSyntaxChecker`
125+
- Downgrades "Unexpected tag" errors to warnings (for extensibility)
126+
127+
This is important when modifying: always ensure Latte checker has all necessary extensions registered.
128+
129+
## Testing Strategy
130+
131+
Tests use Nette Tester with `.phpt` files:
132+
133+
- Each task typically has its own test file: `Tasks.{taskName}.phpt`
134+
- Use the `test()` function wrapper defined in `tests/bootstrap.php`
135+
- Tests create `Result` objects and verify messages/fixes
136+
- No test descriptions needed - the test file name indicates what's tested
137+
138+
Example test pattern:
139+
```php
140+
test(function () {
141+
$result = new Result;
142+
Tasks::someChecker('input content', $result);
143+
Assert::same([[Result::Error, 'Expected message', 1]], $result->getMessages());
144+
});
145+
```
146+
147+
## Key Implementation Details
148+
149+
### File Pattern Matching
150+
151+
The `Checker::matchFileName()` method supports:
152+
- Comma-separated patterns: `*.php,*.phpt`
153+
- Negative patterns with `!` prefix: `!*.sh`
154+
- Case-insensitive matching via `FNM_CASEFOLD`
155+
156+
### Line Number Calculation
157+
158+
Tasks use `Tasks::offsetToLine()` to convert byte offsets to line numbers for error reporting. This is critical for accurate error messages.
159+
160+
### Read-Only vs Fix Mode
161+
162+
In read-only mode:
163+
- Fix-type results are reported as "FOUND" instead of "FIX"
164+
- Files are never modified
165+
- Exit code reflects whether fixes are needed
166+
- Used for CI/validation pipelines
167+
168+
### Token-Based PHP Analysis
169+
170+
Many PHP tasks use `token_get_all()` for parsing:
171+
- Wrapped in `@` to suppress parse errors (checked separately)
172+
- Allows pattern detection without full AST parsing
173+
- Used for: phpDoc validation, escape sequence checking, tab detection
174+
175+
### Exit Codes
176+
177+
- `0`: Success (no errors)
178+
- `1`: Errors found or fixes needed (in read-only mode)
179+
- `2`: Exception/fatal error
180+
181+
## Dependencies
182+
183+
Core dependencies (all from Nette ecosystem):
184+
- `nette/command-line`: CLI argument parsing
185+
- `nette/utils`: String manipulation, file finding
186+
- `latte/latte`: Template syntax validation
187+
- `nette/neon`: NEON format validation
188+
- `nette/application`, `nette/forms`, `nette/caching`, `nette/assets`: Latte extensions

0 commit comments

Comments
 (0)