|
| 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 | +PHP 8.1+ SDK client for the Astrology API v3. Package: `procoders/astrology-api-php`. Uses Guzzle 7 for HTTP transport. This is a standalone library (no framework, no database). |
| 8 | + |
| 9 | +## Common Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Install dependencies |
| 13 | +composer install |
| 14 | + |
| 15 | +# Run tests |
| 16 | +composer test # Full PHPUnit suite |
| 17 | +vendor/bin/phpunit tests/Unit/Categories/ChartsClientTest.php # Single test file |
| 18 | +vendor/bin/phpunit --filter testMethodName # Single test method |
| 19 | + |
| 20 | +# Code coverage |
| 21 | +composer coverage # HTML report in coverage/ + text output |
| 22 | + |
| 23 | +# Code style |
| 24 | +composer lint # Check only (dry-run) |
| 25 | +composer lint:fix # Auto-fix violations |
| 26 | + |
| 27 | +# Documentation |
| 28 | +composer docs # Generate PHPDoc in docs/ |
| 29 | +``` |
| 30 | + |
| 31 | +## Architecture |
| 32 | + |
| 33 | +**Root Client:** `AstrologyClient` is the entry point. It creates a Guzzle client with middleware (retry, API key injection, logging) and exposes 26 readonly category client properties. |
| 34 | + |
| 35 | +**Category Clients:** Each in `src/Categories/`, extending `BaseCategoryClient`. Methods validate inputs via `Validators` then delegate HTTP calls through `HttpHelper`. The `InsightsClient` is special — it contains 5 nested sub-clients (relationship, pet, wellness, financial, business). |
| 36 | + |
| 37 | +**HTTP Layer:** `HttpHelper` interface with `GuzzleHttpHelper` implementation. Responses are normalized by extracting `data` or `result` fields from JSON. |
| 38 | + |
| 39 | +**Validation:** All in `Utils/Validators.php` with static methods. Validation runs before every HTTP call. Throws `AstrologyError` on failure. |
| 40 | + |
| 41 | +**Error Handling:** Single exception type `AstrologyError` with factory methods `fromGuzzleException()` and `normalize()`. Exposes `statusCode`, `errorCode`, `details` plus `isClientError()`/`isServerError()` helpers. |
| 42 | + |
| 43 | +**Configuration:** `AstrologyClientConfig` (readonly) accepts array or object. Reads env vars: `ASTROLOGY_API_KEY`, `ASTROLOGY_API_BASE_URL`, `ASTROLOGY_DEBUG`. Retry config: `RetryConfig` (readonly). |
| 44 | + |
| 45 | +## Code Conventions |
| 46 | + |
| 47 | +- Every file uses `declare(strict_types=1)` |
| 48 | +- PSR-12 style enforced via PHP-CS-Fixer |
| 49 | +- Classes are `final` unless inheritance is needed |
| 50 | +- Heavy use of PHP 8.1+ `readonly` properties |
| 51 | +- PSR-4 autoloading: `Procoders\AstrologyApi\` → `src/`, `Procoders\AstrologyApi\Tests\` → `tests/` |
| 52 | + |
| 53 | +## Testing Patterns |
| 54 | + |
| 55 | +**Unit tests** (`tests/Unit/`): Use `SpyHttpHelper` and Guzzle's `MockHandler` — no real HTTP calls. Tests cover URL construction, validation, retry behavior, and error normalization. Always run. |
| 56 | + |
| 57 | +**Integration tests** (`tests/Integration/`): Make real HTTP requests to the API. Automatically skipped when `ASTROLOGY_API_KEY` env var is not set. Each category has a test file with 1-3 representative endpoint calls. |
| 58 | + |
| 59 | +```bash |
| 60 | +# Run only unit tests |
| 61 | +composer test:unit |
| 62 | + |
| 63 | +# Run only integration tests (requires API key) |
| 64 | +ASTROLOGY_API_KEY=your_key composer test:integration |
| 65 | + |
| 66 | +# Run both (integration tests skip without key) |
| 67 | +composer test |
| 68 | +``` |
| 69 | + |
| 70 | +PHPUnit 10 with random execution order and strict mode (fail on risky/warnings). |
| 71 | + |
| 72 | +## Adding a New Endpoint |
| 73 | + |
| 74 | +1. Add/update category client method in `src/Categories/` (extend `BaseCategoryClient`) |
| 75 | +2. Add validation logic to `Utils/Validators.php` if needed |
| 76 | +3. Call validation before the HTTP request in the method |
| 77 | +4. Write tests in `tests/Unit/Categories/` using `MockHandler` |
| 78 | +5. Run `composer lint:fix && composer test` |
0 commit comments