]> Raphaƫl G. Git Repositories - airlibre/blob - AGENTS.md
Recipe symfony/routing update
[airlibre] / AGENTS.md
1 # AGENTS.md
2
3 This is a Symfony application. `composer.json` gives the versions of Symfony and
4 PHP. `symfony.lock` lists the recipes that ran. Check both before you assume
5 that a package is installed.
6
7 ## Installing packages
8
9 Before writing infrastructure code or adding a third-party library, check
10 whether a Symfony component does the job: Lock, Cache, HttpClient, Mailer,
11 Messenger, Scheduler, RateLimiter, Workflow... Usually there is one, and
12 `composer require` installs it.
13
14 ## Configuration
15
16 - Since Symfony 8.2, each component is configured at its own root key:
17 `messenger:`, `cache:`, `mailer:`, `validation:`... not under `framework:` as
18 in older versions.
19 - `config/reference.php` describes every option of the installed bundles, and
20 `config/schema.json` is their JSON schema, which editors use and `lint:yaml`
21 checks when `opis/json-schema` is installed. Both are regenerated when the
22 container is rebuilt in debug mode. Read them instead of guessing an option.
23 - Prefer attributes to configuration files: `#[Route]`, `#[IsGranted]`,
24 `#[AsCommand]`, `#[AsEventListener]`, `#[AsMessageHandler]`, `#[Autowire]`,
25 `#[Target]`... Services are autowired and autoconfigured, so a service
26 definition in YAML is the last resort.
27 - Map request data with `#[MapRequestPayload]` and `#[MapQueryString]` on the
28 arguments of an action. These attributes deserialize and validate the data,
29 unlike `json_decode()`.
30 - `.env` is committed and contains default values only. Secrets go in
31 `.env.local`, which is ignored by git, or in the vault
32 (`bin/console secrets:set`).
33
34 ## Running the app
35
36 - If `compose.yaml` runs services (database, mailer...), run commands through
37 the Symfony CLI: `symfony console ...`, `symfony php bin/phpunit`. The CLI
38 exposes the env vars of the running containers, and `bin/console` alone
39 doesn't see them. `symfony serve -d` starts a web server.
40 - There's no need to clear the cache in dev: the kernel rebuilds what changed.
41 - When something fails, read `var/log/dev.log` and the profiler (`/_profiler`
42 lists the last requests) before changing code.
43 - Makers (`bin/console make:*`) ask for missing arguments, and a
44 non-interactive shell then waits forever. Pass all the arguments, with
45 `--no-interaction`.
46
47 ## Tests
48
49 `symfony/test-pack` provides PHPUnit: run `php bin/phpunit`. Extend
50 `WebTestCase` to test HTTP requests and `KernelTestCase` to test services. These
51 helpers replace a lot of setup code:
52
53 - `$client->loginUser($user)` instead of submitting a login form;
54 - `MessengerAssertionsTrait`: a message sent to an async transport isn't handled
55 during a test. Call `consumeQueuedMessages()` and check what the handler did,
56 such as the email it sent, or the handler stays untested;
57 - `#[UseRecord]` to record HTTP exchanges once and replay them, instead of
58 calling real APIs;
59 - `ClockSensitiveTrait::mockTime()` to control the time.
60
61 ## Writing
62
63 These rules apply to comments, commit messages, docs and anything written for
64 the team:
65
66 - Write plain, factual English, because many readers aren't native speakers.
67 Write one idea per sentence and use verbs in their everyday sense. Repeat the
68 noun instead of using a pronoun that makes the reader look back.
69 - State facts, not how good they are. Avoid words like "robust", "seamless",
70 "powerful" or "simply", exclamation marks and emoji.
71 - Start with what changes for the reader, then explain why. Don't repeat what
72 the code or the diff already shows.
73 - Use commas, colons and parentheses, not em-dashes.
74 - A commit message says what changed in one line, plus a sentence or two when
75 the reason isn't obvious.
76
77 ## Code
78
79 - Write a comment only for what the code can't say, such as a reason, a
80 reference to a spec or a non-obvious constraint. Don't narrate the code.
81 - Don't set an option to its default value. `config/reference.php` shows the
82 defaults.
83 - Fix a bug by writing a test that reproduces it first.
84 - Keep the diff to what the task needs: no unrelated renames, reformatting or
85 refactoring.
86
87 ## When a package gets in the way
88
89 These rules apply to bugs, missing features, slow code paths and APIs that make
90 the app write boilerplate, in Symfony or in any other package:
91
92 - Before you write a workaround, check that the problem is real. Reproduce it in
93 a few lines, compare the installed version with the latest release, and
94 search the issues and pull requests of the project.
95 - For a performance issue, measure it first, with the profiler or a benchmark,
96 and keep the numbers.
97 - Keep the workaround small. Link the workaround to the upstream issue or pull
98 request in a comment, so that it can be removed after the fix is released.
99 - Offer to report it upstream. Draft the issue with the reproduction or the
100 numbers, or a pull request with a test when the fix is clear. Ask before
101 posting anything, since it is published under the name of the person you
102 work for.
103 - Report security issues privately, as the security policy of the project says,
104 never in a public issue. For Symfony, see
105 https://symfony.com/doc/current/contributing/code/security.html.
106
107 ## Look it up
108
109 Symfony's APIs change between versions, and what you remember may be outdated.
110 The application can tell what exists:
111
112 - `bin/console debug:autowiring <keyword>`: the services you can type-hint;
113 - `bin/console debug:autoconfiguration <keyword>`: the attributes and interfaces
114 that make a class a command, a listener, a voter, a message handler...;
115 - `bin/console debug:config <root key>`: the configuration as resolved;
116 - `bin/console debug:router`, `debug:container --env-vars`, `debug:messenger`,
117 `debug:event-dispatcher`...;
118 - `bin/console lint:container`, `lint:yaml config/`, `lint:twig templates/`: to
119 catch errors before running;
120 - `vendor/symfony/<package>/CHANGELOG.md`: what each version added, deprecated
121 or removed. Read the sections of the recent versions before you use a
122 component;
123 - the source and the docblocks in `vendor/`;
124 - https://symfony.com/doc/current/, switched to the version of `composer.json`
125 when it differs.