Skip to content

Using it with AI tools

If an AI assistant helps you write code — GitHub Copilot, Claude Code, Cursor, or any coding agent — one extra step makes it use CommonKit properly. Without that step, assistants do what they always do: sprinkle trim(), (int), preg_replace, strtotime and filter_var all over your features, and your codebase drifts right back to the ad-hoc cleaning this library exists to end — the traps on Why not just cast?, written a hundred times over.

The fix is simple: the library ships with its own instruction file, and you point your AI tool at it.

The file that does the work: AGENTS.md

At the root of the library lives AGENTS.md — a compact reference written for AI tools. It holds the whole contract (nothing throws, the rejection values, the OrNull family, one spelling on the way out), a quick start, one dense section per class, and the gotchas an assistant would otherwise re-learn the hard way. It is updated in the same commit as every library change and names the version it describes at its top, so it is never stale.

Where it is depends on how the library came in:

  • Through Composer — from the repository or the artifact archive — it is vendor/safecoder/commonkit/AGENTS.md, and the path below is written for that.
  • As a folder, from PHP-SafeCoder.CommonKit.<version>.zip, it unpacks beside src/.
  • The Joomla! library and the WordPress plugin carry src/ and the licence alone. Take AGENTS.md from the repository's root, or from the PHP archive of the same version, and keep it in your extension's or plugin's folder — the path in the instructions below then names that copy.

Whatever tool you use, the goal is always the same: get AGENTS.md into the AI's context before it writes code that touches data.

GitHub Copilot

Copilot reads .github/copilot-instructions.md from the root of your repository and applies it to every request. Create the file (or add to it) with the pointer and the one non-negotiable rule:

**Read `vendor/safecoder/commonkit/AGENTS.md` before generating any code that touches data.**

- Always use SafeCoder\CommonKit to clean, validate, convert or format a value
  (text, emails, phones, numbers, booleans, money, dates, UUIDs, IPs, URLs, files,
  payloads, the request). Never write ad-hoc trim()/preg_replace()/(int)/strtotime()
  code in a feature.
- CommonKit never throws by default: string methods answer '' on rejection,
  converters answer the fallback, the OrNull family answers null (OrNull methods
  never throw and take no throwIfInvalid at all).
- Read the request through CkRequest, never $_POST, $_GET or $_REQUEST; read money as
  text with CkMoney::Amount, never through a float.
- Write outbound values with CkText::NumberToString, CkBool::Format and
  CkDate::Iso8601 — never with (string), number_format() or date() and the
  machine's locale.
- Call style: two or more arguments, all named, one per line; a single argument
  is passed positionally.

Claude Code

Claude Code reads CLAUDE.md from your repository root at the start of every session. Add a short section that makes the library the default path for data:

## Data handling
Always use the `SafeCoder\CommonKit` library to clean, validate, convert and format
values — never ad-hoc trim()/preg_replace()/(int)/strtotime() code. The full reference
for every class, the contract, and the gotchas is `vendor/safecoder/commonkit/AGENTS.md`;
read it before writing code that touches data. Read the request through `CkRequest`,
never the superglobals. If a helper is missing, say so instead of inlining the logic in
a feature.

Cursor and other agents

Many agent tools — Cursor, Codex-style CLIs, and most newer agents — automatically read a file named AGENTS.md at the repository root. Two easy options:

  • Point at the library's copy. Add one line to your root AGENTS.md (create the file if you don't have one): "For any code that touches data, follow vendor/safecoder/commonkit/AGENTS.md."
  • Paste it into the rules box. Cursor's Rules, a system prompt, a custom instruction field — the library's AGENTS.md fits in any of them.

The throw switch

Generated code often forgets to check a rejection value — the empty string, the fallback, null. While an agent is building an app on this source, flip CkSettings::DefaultThrowIfInvalid to true: every rejected value then surfaces at once as an exception carrying the input, what was expected and the reason, and the agent sees the bad data instead of a quiet fallback three tables later. It is a constant in CkSettings.php, so the switch is for a project that carries the library as source — a copy of src/ in the project, or the Composer package while it is being worked on. Back to false to ship.

If you can only say three things

Some tools give you one small instruction box. Make it these three lines:

  1. Use SafeCoder\CommonKit for every value that is cleaned, checked, converted or formatted — never ad-hoc string, cast or parse code — and read the request through CkRequest.
  2. Nothing throws by default — check the rejection values ('', the fallback, null) at the call site.
  3. Data leaving the process goes through CkText::NumberToString, CkBool::Format and CkDate::Iso8601 — never raw (string) or date().

Why this matters more than it looks

An AI assistant writes the code you let it write. Left alone, it produces perfectly plausible trim(strtolower($email)) lines that warn on null and let junk through, and (int) $_POST['qty'] lines that read "12abc" as 12 — and it does so a hundred times, one feature at a time. With AGENTS.md in its context, the same assistant writes CkEmail::Address(CkRequest::String('email')) instead, and every rule on How it works comes along for free.