---
title: "Rules"
url: https://festi.dev/8.0/Packages/CodingStandard/docs/rules/
---

# Rules

[Back to the README](https://festi.dev/8.0/Packages/CodingStandard/index.md)

What each rule asks for. To change a limit or switch a rule off, see
[Configuration](https://festi.dev/8.0/Packages/CodingStandard/docs/configuration/index.md).

- [The `Festi` standard (phpcs)](#the-festi-standard-phpcs)
- [Class-design limits (PHPMD)](#class-design-limits-phpmd)
- [Phan plugins](#phan-plugins)

## The `Festi` standard (phpcs)

### Layout and syntax

Taken from the Generic, PEAR and Squiz standards:

- Allman braces for functions and classes; PEAR control-structure and
  function-call signatures.
- No tab indent, no short open tag, Unix line endings.
- No global functions.
- Lines of at most 120 characters.
- Short array syntax: `[]`, not `array()` (auto-fixable).

### Naming

| Rule | Asks for | Good | Bad |
|---|---|---|---|
| `Festi.NamingConventions.PrivateMemberUnderscore` | a `private` property starts with `_`, promoted constructor parameters included | `private string $_name;` | `private string $name;` |
| `Festi.NamingConventions.PrivateMethodUnderscore` | a `private` method starts with `_` | `private function _load()` | `private function load()` |
| `Festi.NamingConventions.MethodNameConnectiveWord` | no `Or` or `And` as a word of a method name; a method does one thing. `One` is a number and is allowed: `getOne()` | `findUser()` | `findOrCreateUser()` |
| `Festi.NamingConventions.EntityIdNaming` | entity ids named by one convention, below | | |

Only `private` takes the underscore. `protected` is part of a class's
contract with its subclasses, so like `public` it stays unprefixed. Test
methods are exempt from the connective-word rule.

#### Entity ids

| Kind | Form | Good | Bad |
|---|---|---|---|
| Method | `ID` suffix | `getProjectID()` | `getIdProject()`, `getUserId()` |
| Constant | `_ID` suffix | `FIELD_AGENT_ID` | `FIELD_ID_AGENT`, `ID_PROJECT` |
| Constant for the array key of an id | `_ID_KEY` suffix | `TARGET_USER_ID_KEY` | `TARGET_ID_USER_KEY` |
| Variable holding an id | `$id` prefix | `$idProject`, `$_idProject` | `$projectId`, `$projectID` |
| Variable mentioning an id | `ID`, ending the name | `$keysByID`, `$cacheByUserID` | `$keysById`, `$userIdCache` |

So a DTO pairs the property `$_idProject` with the getter `getProjectID()`.
Only a constant's name is checked, never its value:
`const FIELD_AGENT_ID = 'id_agent'` is fine. `getIdentifier()`, `$identity`
and `isValid()` are not ids and are left alone, as is the bare `FIELD_ID`.

`Id` is never written in a variable name. What the name becomes depends on
what the variable holds:

| Written | Holds | Becomes |
|---|---|---|
| `$projectId`, `$userID` | an id | `$idProject`, `$idUser` |
| `$resolvedByUserId` | an id: the user who resolved it | `$idResolvedByUser` |
| `$keysById`, `$uidByCallId` | a map keyed by id | `$keysByID`, `$uidByCallID` |
| `$postWithoutAnId`, `$byToolId` | something related to an id | `$postWithoutAnID`, `$byToolID` |
| `$hasParentId` | a yes-or-no | `$hasParentID` |
| `$lastEventId = [...]` | an array | `$lastEventID` |
| `$userIdCache`, `$rowProjectIdLookup` | a cache, a lookup | reorder it: `$cacheByUserID` |

The message carries the exact new name, except for the last row. There the
words have to be reordered so that what the variable holds comes first and
`ID` ends the name, which depends on what the variable holds. That case is
reported as `VariableIdNamingUnclear` with the pattern to follow, and
[asks for a judge](https://festi.dev/8.0/Packages/CodingStandard/docs/extending/index.md#asking-for-a-judge).

Every use of a badly named variable is reported, so one wrong name in a
long method is several errors.

### Documentation

**`Festi.Commenting.PublicMethodDocBlock`** — every `public` method has a
docblock with an `@param` for each parameter and an `@return`.

```php
/**
 * @param int $idProject Project to load.
 *
 * @return Project
 */
public function getProject(int $idProject): Project
```

`@return` is not needed for a `void` or `never` method. `{@inheritDoc}` is
accepted. `private`, `protected`, magic (`__*`) and `test*` methods are not
checked, and neither is any file under `tests/`.

When a method has attributes, write the docblock above the attributes:

```php
/**
 * @param int $idProject Project to load.
 *
 * @return Project
 * @throws StoreException
 */
#[Route('/project')]
#[Deprecated]
public function getProject(int $idProject): Project
```

The rule reads the docblock across each attribute group and across the
blank lines between them. A comment between the docblock and the method
separates them, and the rule then reports `MissingDocBlock`.

The rule also accepts a docblock between the attributes and the method.
Do not use that form: Phan with `--allow-polyfill-parser` does not read
such a docblock, and reports `PhanUnreferencedUseNormal` for a class that
only its `@throws` tag names.

This is about documentation being present, which a type checker does not
ask for: a fully typed `getProjectID(): int` satisfies PHPStan and Phan
with no docblock at all.

### Arrays

| Rule | Asks for | Good | Bad |
|---|---|---|---|
| `Festi.Arrays.MultiLineArrayElements` | an array with more than one element is written one element per line | see below | `['a' => 1, 'b' => 2]` |
| `Festi.Arrays.EmptyArrayComparison` | check emptiness with `empty()` | `if (empty($rows))` | `if ($rows === [])` |
| `Festi.Functions.NoArrayArgument` | no array literal as a call argument: name it first, or pass a value object | see below | `send(['to' => $user])` |

```php
// MultiLineArrayElements, with ArrayIndent and the Squiz newline rules
$options = [
    'to' => $user,
    'subject' => $subject,
];

// NoArrayArgument: the array gets a name before it is passed
$mailer->send($options);
```

A destructuring target (`[$a, $b] = f();`) is not an array literal and is
left alone. A comparison against a non-empty literal (`$a === [1, 2]`) is a
real value check and is left alone too.

`empty()` is broader than `=== []`: it is also true for `0`, `'0'`, `''`,
`null` and `false`. On a value that is always an array the two agree. Where
a scalar can legitimately reach the comparison, keep the strict form and
add `// phpcs:ignore` with a one-line reason.

### Strings and conditions

**`Festi.Strings.ConcatenationSpacing`** — no spaces around `.`:
`$first.' '.$last`. A wrapped expression keeps the `.` at the end of the
line it continues from.

**`Festi.ControlStructures.TrailingBooleanOperator`** — a condition that
wraps keeps `&&` / `||` at the end of the line it continues from
(auto-fixable):

```php
// bad                      // good
if ($isEnabled              if ($isEnabled &&
    && $hasPermission           $hasPermission
) {                         ) {
```

It applies wherever the operator appears: `if`, `while`, ternaries,
assignments, `return`. The fix lengthens the previous line, so run the
standard again after a bulk `phpcbf`.

### Routing methods

**`Festi.Functions.RoutingMethodTypeHint`** — every parameter of a routing
method on a `*Plugin` class has a type declaration, because the router
casts URL captures to the declared type:

```php
// bad: URL captures arrive untyped and are cast by hand
public function onDisplayAgentSkills(Response &$response, $idProject, $idAgent): bool
{
    $idProject = (int) $idProject;

// good
public function onDisplayAgentSkills(Response &$response, int $idProject, int $idAgent): bool
```

A routing method is a `public` method whose name starts with `onDisplay`,
`onUpdate`, `onAjax`, `onJson`, `doAjaxResponse` or `doJsonResponse`.

### Types

Every file checks its types strictly, and every parameter, return value
and property says what it is.

| Rule | Asks for | Good | Bad |
|---|---|---|---|
| `SlevomatCodingStandard.TypeHints.DeclareStrictTypes` | the file opens with `declare(strict_types=1);` | | a file without it |
| `SlevomatCodingStandard.TypeHints.ParameterTypeHint` | a type on every parameter | `function load(string $name)` | `function load($name)` |
| `SlevomatCodingStandard.TypeHints.ReturnTypeHint` | a return type on every function | `function load(): Project` | `function load()` |
| `SlevomatCodingStandard.TypeHints.PropertyTypeHint` | a type on every property | `private int $_weight;` | `private $_weight;` |

The type is native where PHP can express it and in the docblock where it
cannot:

```php
/** @var resource */
private $_handle;

/**
 * @param string[] $tags What an array holds is said in the docblock.
 *
 * @return array<string, int>
 */
public function count(array $tags): array
```

An `array` whose items are not described is reported
(`MissingTraversableTypeHintSpecification`). A docblock tag that repeats a
native type is **not** asked to go: `PublicMethodDocBlock` asks for it.

**Overriding an untyped method.** PHP refuses to add a parameter type the
parent method does not have. A method that overrides one, a framework hook
for example, says so and is left alone:

```php
/**
 * {@inheritDoc}
 */
public function process(File $phpcsFile, $stackPtr): void
```

`phpcbf` adds a native type wherever the docblock already names one, and
adds the `declare`. Review what it did to a public method: a subclass
elsewhere that overrides it has to follow.

### Architecture

**`Festi.Architecture.DomainLayerDependency`** — the domain depends on
nothing around it. A class whose namespace has a `Domain` segment must not
name another layer of its own context, nor reach into the application
through a service locator.

```php
namespace Plugins\Agents\Domain\Service;

// bad: the domain names the layer that stores it
use Plugins\Agents\Store\MySqlTaskStore;

// bad: the constructor no longer says what the class needs
$db = Core::getInstance()->db;

// good: the domain declares a port, the outer layer implements it
use Plugins\Agents\Domain\Port\ITaskStore;

public function __construct(ITaskStore $tasks)
```

| Code | Reported when domain code |
|---|---|
| `OutwardDependency` | names a class of its own context outside `Domain` and `Contracts`, or any class in an `Application`, `Infrastructure`, `Persistence` or `Adapter(s)` namespace |
| `ServiceLocator` | calls `Core::getInstance()` |

Left alone: its own model, ports and exceptions, the contracts of its
context, **the domain and contracts of another context**, framework base
classes (`ValuesObject`, `Entity`), libraries, and all test code. A file
with no namespace is not checked.

### Complexity of one function

Three measures, all reported as **errors**:

| Sniff | Measures | Reports above |
|---|---|---|
| `Generic.Metrics.CyclomaticComplexity` | decision points | 10 |
| `Generic.Metrics.NestingLevel` | depth of nested control structures | 3 |
| `SlevomatCodingStandard.Complexity.Cognitive` | cognitive complexity, the measure SonarQube reports as `php:S3776` | 15 |

A function over a limit fails a whole-tree run. For a change, the function
it edits was usually over the limit before it, so both `festi-phpcs-diff`
and `festi-quality --diff` measure the same function at the base revision
and report only what the change added or raised
([Usage](https://festi.dev/8.0/Packages/CodingStandard/docs/usage/index.md#did-the-change-make-it-worse)). A fix made inside an old,
complex function is therefore not blocked; making that function more
complex is. [Configuration](https://festi.dev/8.0/Packages/CodingStandard/docs/configuration/index.md#turning-an-error-into-a-warning)
shows how a project turns one back into a warning.

## Class-design limits (PHPMD)

phpcs looks at one function at a time. PHPMD measures the class.

The limit is the highest value that the rule accepts.

| Rule | Measures | Limit |
|---|---|---|
| `ExcessiveClassComplexity` | the cyclomatic complexity of every method, summed | 49 |
| `CouplingBetweenObjects` | other types the class depends on | 12 |
| `NPathComplexity` | execution paths through one method | 199 |
| `TooManyMethods` | methods, accessors not counted | 20 |
| `TooManyFields` | fields | 10 |
| `ParameterCount` | parameters of a method or function | 4 |
| `ParameterCount` | parameters of a constructor | 5 |

Each limit stands for a design principle:

- **A long parameter list is a value object nobody named.** Past four, some
  of the parameters belong together.
- **A long constructor is a class with more than one job.** Its parameters
  are collaborators, so one more is allowed than for a method.
- **Many fields is an aggregate that has grown.** An entity over ten
  usually holds a value object.
- **Many dependencies is dependency inversion not happening.** A
  composition root is exempt, because knowing many classes is its job:
  classes named `*Plugin` or `*Factory` are not measured for coupling.

`tests/` is never measured: a scenario class is long by design. Under
`festi-quality --diff` these are compared with the base revision in the
same way as the complexity rules.

## Phan plugins

Enabled through the project's `.phan/config.php`; the template lists all of
them.

### `PlaneSeparationPlugin`

| Issue | Reported when | Fix |
|---|---|---|
| `FestiPlaneSeparation` | `getPluginInstance()` is called outside a composition root | resolve the plugin in the facade and pass what is needed down |
| `FestiCrossPluginDao` | a DAO is constructed by hand, `new RunnersObject($db)` | use `$this->object`, a linked DAO, or `DataAccessObject::getInstance` |

Resolving another plugin from inside a domain service couples the planes
of an application (`admin`, `api`, `workers`) and hides a global dependency
from the service's constructor.

Composition roots may resolve plugins: file scope (`index.php`, cron entry
points), and classes named `*Plugin`, `*Worker`, `*Tool` or `*Mode`.
Facades and DAOs may construct DAOs. `new AgentValuesObject($row)` is a
value object, not a DAO, and is fine.

A test may resolve a plugin and construct a DAO: it must build the plugin
and the DAO that it tests. The plugin finds a test by its class hierarchy,
not by its path. A test is a class that extends
`PHPUnit\Framework\TestCase`, directly or through parent classes. Each
parent class except `TestCase` must be in the analysed code (for example
a base test class in `tests/`). PHPUnit does not have to be installed. A
helper class of a test suite that does not extend `TestCase` is reported
like production code.

```php
// fine: a test builds what it tests
class JimboObjectTest extends \PHPUnit\Framework\TestCase
{
    public function testItReadsAUser(): void
    {
        $object = new JimboObject($this->connection);
    }
}

// reported: the same line in a service
class JimboService
{
    public function load(): void
    {
        $object = new JimboObject($this->connection);
    }
}
```

### `SqlInjectionConcatPlugin`

**`PhanPluginSqlInjectionValueConcat`** — SQL built by concatenating a
value straight after a comparison operator:

```php
// reported
$sql = 'SELECT * FROM users WHERE '.$column.'='.$value;

// fine: the value goes through a sanitiser
$sql = 'SELECT * FROM users WHERE '.$column.'='.$this->quote($value);
```

`quote()`, `getSqlCondition()` and an `(int)` or `(float)` cast count as
sanitisers. The rule looks only inside functions that build SQL: ones that
call a query method or contain a SQL keyword.

Quotes written around a value do not make it safe, because the value can
close them. These are reported too:

```php
$sql = "SELECT * FROM users WHERE name = '".$name."'";
$sql = "SELECT * FROM users WHERE name LIKE '%".$name."%'";
$sql = "SELECT * FROM users WHERE name = '$name'";
```

### `SecurityGuardArgumentPlugin`

| Issue | Reported when |
|---|---|
| `PhanPluginSecurityGuardDisabled` | a call passes a literal `false` to a parameter named like an authorisation guard: `$store->loadRowByPrimaryKey($id, false)` |
| `PhanPluginSecurityGuardDefaultsOff` | a method declares such a parameter defaulting to `false`, so every caller opts out unless it remembers to opt in |

### `CorePlugin`

Reports nothing. It tells Phan that `getPluginInstance('Jimbo')` returns a
`JimboPlugin`, so calls on the result are type-checked.
