Drupal Coding Standards: Install, Source and Security

Drupal Coding Standards

Published by gkastanis in drupal-workflow

No known issues50 installs

What this skill does

PHPCS, PHPStan, naming conventions, and code style enforcement for Drupal 10/11. Use when checking coding standards, running static analysis, or enforcing code quality.

Add Drupal Coding Standards to your agent

Review the source and files first. When you are ready, copy the prompt instruction or use the CLI command supported by your environment.

Install with a prompt

Paste this into a compatible coding agent:

add this skill "drupal-coding-standards" from https://github.com/gkastanis/drupal-workflow

Install with the CLI

Run this command in a controlled environment after reviewing the repository:

npx skills add https://github.com/gkastanis/drupal-workflow --skill drupal-coding-standards

Skill instructions

Drupal Coding Standards

Always run PHPCS before committing. Never skip static analysis. Use declare(strict_types=1) in every PHP file. Prefer final classes unless explicitly designed for extension. Avoid deep nesting — use guard clauses and early returns.

Validation Commands

# Check coding standards.
./vendor/bin/phpcs --standard=Drupal,DrupalPractice web/modules/custom/my_module/

# Auto-fix standards issues.
./vendor/bin/phpcbf --standard=Drupal web/modules/custom/my_module/

# Static analysis.
./vendor/bin/phpstan analyse web/modules/custom/my_module/

# Deprecation check.
drupal-check web/modules/custom/my_module/

# Security audit.
composer audit

Required Code Patterns

File Headers

<?php

declare(strict_types=1);

namespace Drupal\my_module;

Class Declaration

/**
 * Manages content operations.
 */
final class ContentManager implements ContentManagerInterface {

Constructor

public function __construct(
  private readonly EntityTypeManagerInterface $entityTypeManager,
  private readonly LoggerInterface $logger,
) {}

Naming Conventions

ElementConventionExample
Local variables$snake_case$user_name
Class properties$lowerCamelCase$this->entityManager
ClassesPascalCaseContentManager
InterfacesPascalCase + InterfaceContentManagerInterface
ConstantsUPPER_SNAKE_CASEMAX_RETRY_COUNT
Servicesmodule.service_namemy_module.content_manager
Hooksmodule_hook_namemy_module_form_alter

Anti-Patterns to Report

Anti-PatternFix
foreach with nested if/break/continuearray_filter/array_map/array_reduce
Deep nesting (3+ levels)Guard clauses, early returns
Non-final classes without reasonDeclare final by default
Getters/setters where public readonly worksUse public readonly
\Drupal:: calls in classesConstructor dependency injection
PHP json_encode/decode\GuzzleHttp\Utils::jsonDecode/jsonEncode
"Service" namespaceUse logical groupings
Catching \ExceptionCatch narrowest exception type

PHPDoc Standards

/**
 * Loads articles by category.
 *
 * @param int $category_id
 *   The taxonomy term ID.
 * @param int $limit
 *   Maximum number of results.
 *
 * @return \Drupal\node\NodeInterface[]
 *   Array of article nodes.
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 */
public function loadByCategory(int $category_id, int $limit = 10): array {

Module Structure

modules/custom/my_module/
  my_module.info.yml
  my_module.module
  my_module.services.yml
  my_module.routing.yml
  my_module.permissions.yml
  config/
    install/
    schema/
  src/
    Controller/
    Form/
    Plugin/
      Block/
    EventSubscriber/
  tests/
    src/
      Unit/
      Kernel/
      Functional/

Files included

  • SKILL.md

More skills