Coding conventions

From shopsys/shopsys · View interactive version

Coding conventions is an AI agent skill from the GitHub repository shopsys/shopsys. Coding principles and conventions for this Shopsys project — DRY/KISS/reuse, design conventions (data model, API, simplicity), comment and docblock quality, test-code rules, the project visibility/typing rules, and documentation best practices. Read it before planning or writing PHP so your design a

How to use this skill

Skill file

Monorepo note: if the repository has top-level packages/ and project-base/ directories (the shopsys/shopsys monorepo layout, not a standalone project), also read .agents/skills/monorepo-vs-project/SKILL.md and apply its delta (package-first + path/role remap) on top of this skill.

Shopsys coding conventions

Follow these whenever you write or modify code in this project.

Core principles

DRY (Don't Repeat Yourself)

KISS (Keep It Simple)

Reuse & composition

Validation & guard clauses

Maintainability first

Before writing new code, ask:

Comments — quality over quantity

// GOOD
// Handle edge case where role doesn't have FULL permission available
// Workaround for legacy database structure - remove after migration to v2.0
// Algorithm based on RFC 3986 specification

// BAD
// This method returns the name
public function getName(): string
// Set the user ID
$user->setId($id);

DocBlocks — always specify non-obvious types

Add docblocks for non-obvious parameters and return types (helps IDEs and static analysis). Always specify: array contents, class-string targets, callable signatures, iterable element types, complex/generic return types.

// GOOD
/**
 * @param class-string<\Shopsys\FrameworkBundle\Component\Context\AbstractContext> $context
 * @param array<string> $excludedRoles
 * @param array<int, \Shopsys\FrameworkBundle\Component\Security\Role\Role> $roles
 * @return array<string, mixed>
 */
public function buildGrid(string $context, array $excludedRoles, array $roles): array

/**
 * @param callable(string): bool $validator
 * @param iterable<\App\Entity\User> $users
 * @return \Generator<int, string>
 */
public function processUsers(callable $validator, iterable $users): \Generator

// BAD — what's in the array? which class? array of what?
/**
 * @param array $excludedRoles
 * @param string $context
 * @return array
 */
public function buildGrid(string $context, array $excludedRoles): array

`{@inheritdoc}` vs a detailed docblock

// Simple case → {@inheritdoc}
/**
 * {@inheritdoc}
 */
public function findById(int $id): ?User { /* ... */ }

// Adds value → specific docblock
/**
 * Transforms role identifiers array to multidimensional form structure
 *
 * @param array<string>|mixed $value Array of role identifiers (e.g., ['ROLE_ORDER_VIEW'])
 * @return array<string, array<string, bool>> [roleConstant][permission] = bool
 */
public function transform(mixed $value): array { /* ... */ }

Design conventions — apply while designing, not only in review

Decide these before the first line is written (when planning) and keep to them while implementing. They are the same rules the self-review skill checks afterwards — if you follow them from the start, the review has nothing to find.

The class and field names in the examples below (gift plans, CartRemovedProductGift, the cart watcher) come from one feature and only illustrate the rule — look for the equivalent in the code you are changing.

Data model

API (GraphQL Frontend API, public methods, mutation inputs)

Simplicity

Test-code rules

Entity property hooks

Sanitization, single-property validation and null fallbacks of an entity property live in a set property hook, never only in a setter (a constructor, edit() or a subclass can bypass a setter). Rules and examples: docs/model/entities.md#property-hooks.

Escaping dynamic values in administration HTML

Never put a stored value (entity name, label, e-mail, file name, domain name) into an HTML string through t() parameters, sprintf() or concatenation when the string is printed raw — flash messages, delete confirmations, Twig extension output. Pass the value to the helper that escapes it (add*FlashTwig() template parameters, ConfirmDeleteResponseFactory $messageParameters, EscapedMessageInterpolator::interpolate() parameters) or escape it yourself. Rules and examples: docs/security/output-escaping.md.

Visibility & typing

Your project code is a final version used as-is, with no downstream extension surface to preserve:

The framework you extend lives read-only in vendor/shopsys/ and follows different rules there (it's built to be extended) — but that's its code, not yours. For how to extend it correctly, see .agents/skills/shopsys-architecture/SKILL.md.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.