一键导入
standards-coding
Follow Magento 2 coding standards — service contracts, strict types, PHPCS rules, repository pattern, and extension attribute conventions
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Follow Magento 2 coding standards — service contracts, strict types, PHPCS rules, repository pattern, and extension attribute conventions
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Build Magento 2 admin grids using UI Component listing XML, data providers, and admin controllers with proper ACL integration
Implement Magento 2 GraphQL resolvers — define schema.graphqls types, write query and mutation resolvers, and handle authorization
Run Magento 2 CLI commands through Warden's Docker environment: warden shell, bin/magento, composer, Redis/Valkey, Varnish, OpenSearch, RabbitMQ, n98-magerun2, mutagen sync, and env lifecycle.
Adapt Luma-oriented Magento 2 modules to work with Hyva themes — replace RequireJS and Knockout with Alpine.js, Tailwind CSS, and ViewModels
Configure Magento 2 dependency injection — preferences, virtual types, constructor arguments, and proxy generation via di.xml
Implement Magento 2 plugins (interceptors) following best practices for before, after, and around method interception
基于 SOC 职业分类
| name | standards-coding |
| description | Follow Magento 2 coding standards — service contracts, strict types, PHPCS rules, repository pattern, and extension attribute conventions |
| installed_version | 1.0.0 |
| magehub_version | 0.1.13 |
Magento enforces coding standards through the magento/magento-coding-standard
PHPCS ruleset. All custom code must pass this ruleset before merge. Beyond
automated checks, Magento's architecture relies on structural conventions —
service contracts, repository patterns, and strict type declarations — that
determine how modules interoperate.
Add declare(strict_types=1); to every PHP file. Strict typing catches
type coercion bugs at the exact call site rather than producing silent data
corruption downstream. This is mandatory for all new Magento 2 code and
enforced by the coding standard.
Define public APIs in Api/ interfaces. Data interfaces go in Api/Data/.
Repository interfaces follow the pattern: getById(), save(), delete(),
getList(). Other modules must depend on these interfaces, never on concrete
model classes. Service contracts are the only stable API — Magento may change
internal implementations between patch releases.
Implement repositories in Model/ that fulfill the Api/ interface contract.
Use resource models for persistence and collection factories for getList().
Bind the interface to the implementation via di.xml preference. Never
expose the resource model or collection directly — always route through the
repository.
Use extension_attributes.xml to add custom fields to existing data
interfaces without modifying core code. Extension attributes are type-safe,
auto-generated by Magento, and compatible with REST/GraphQL serialization.
Prefer extension attributes over EAV attributes when the data is module-
specific and not user-editable.
Run vendor/bin/phpcs --standard=Magento2 <path> on every module. Address
all errors — the Magento Marketplace and many CI pipelines reject submissions
with PHPCS violations. Key rules: method visibility must be explicit, method
length under 20 lines preferred, cyclomatic complexity under 10.
Always include the xsi:noNamespaceSchemaLocation attribute in XML config
files. This enables IDE validation and matches the coding standard. Use
lowercase underscore-separated names for XML node identifiers.
Repository interface following the Magento service contract pattern with full PHPDoc for web API compatibility
<?php
declare(strict_types=1);
namespace Vendor\Module\Api;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Vendor\Module\Api\Data\EntityInterface;
use Vendor\Module\Api\Data\EntitySearchResultsInterface;
interface EntityRepositoryInterface
{
/**
* @param int $entityId
* @return EntityInterface
* @throws NoSuchEntityException
*/
public function getById(int $entityId): EntityInterface;
/**
* @param EntityInterface $entity
* @return EntityInterface
* @throws CouldNotSaveException
*/
public function save(EntityInterface $entity): EntityInterface;
/**
* @param EntityInterface $entity
* @return bool
*/
public function delete(EntityInterface $entity): bool;
/**
* @param SearchCriteriaInterface $searchCriteria
* @return EntitySearchResultsInterface
*/
public function getList(SearchCriteriaInterface $searchCriteria): EntitySearchResultsInterface;
}
Concrete repository that implements the service contract using resource models for persistence and collection for list queries
<?php
declare(strict_types=1);
namespace Vendor\Module\Model;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Vendor\Module\Api\Data\EntityInterface;
use Vendor\Module\Api\Data\EntitySearchResultsInterface;
use Vendor\Module\Api\Data\EntitySearchResultsInterfaceFactory;
use Vendor\Module\Api\EntityRepositoryInterface;
use Vendor\Module\Model\ResourceModel\Entity as EntityResource;
use Vendor\Module\Model\ResourceModel\Entity\CollectionFactory;
class EntityRepository implements EntityRepositoryInterface
{
public function __construct(
private readonly EntityResource $resource,
private readonly EntityFactory $entityFactory,
private readonly CollectionFactory $collectionFactory,
private readonly CollectionProcessorInterface $collectionProcessor,
private readonly EntitySearchResultsInterfaceFactory $searchResultsFactory
) {
}
public function getById(int $entityId): EntityInterface
{
$entity = $this->entityFactory->create();
$this->resource->load($entity, $entityId);
if (!$entity->getId()) {
throw new NoSuchEntityException(
__('Entity with ID "%1" does not exist.', $entityId)
);
}
return $entity;
}
public function save(EntityInterface $entity): EntityInterface
{
try {
$this->resource->save($entity);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__('Could not save entity: %1', $e->getMessage()),
$e
);
}
return $entity;
}
public function delete(EntityInterface $entity): bool
{
$this->resource->delete($entity);
return true;
}
public function getList(SearchCriteriaInterface $searchCriteria): EntitySearchResultsInterface
{
$collection = $this->collectionFactory->create();
$this->collectionProcessor->process($searchCriteria, $collection);
$searchResults = $this->searchResultsFactory->create();
$searchResults->setSearchCriteria($searchCriteria);
$searchResults->setItems($collection->getItems());
$searchResults->setTotalCount($collection->getSize());
return $searchResults;
}
}
Composer script and phpcs.xml configuration to run the Magento 2 coding standard in a CI pipeline
<?xml version="1.0"?>
<ruleset name="Vendor Module">
<description>Magento 2 coding standard for Vendor Module</description>
<rule ref="Magento2"/>
<file>.</file>
<exclude-pattern>*/Test/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<arg name="extensions" value="php"/>
<arg name="colors"/>
<arg value="sp"/>
</ruleset>
Path template:
Api/{{interfaceName}}.php
Service contract repository interface with standard CRUD methods
<?php
declare(strict_types=1);
namespace {{vendor}}\{{module}}\Api;
use Magento\Framework\Exception\NoSuchEntityException;
use {{vendor}}\{{module}}\Api\Data\{{dataInterface}};
interface {{interfaceName}}
{
/**
* @param int $id
* @return {{dataInterface}}
* @throws NoSuchEntityException
*/
public function getById(int $id): {{dataInterface}};
}
Path template:
phpcs.xml
PHPCS configuration applying the Magento 2 coding standard to the module
<?xml version="1.0"?>
<ruleset name="{{vendor}} {{module}}">
<description>Magento 2 coding standard for {{vendor}} {{module}}</description>
<rule ref="Magento2"/>
<file>.</file>
<exclude-pattern>*/Test/*</exclude-pattern>
<arg name="extensions" value="php"/>
<arg name="colors"/>
</ruleset>