typo3-simplify
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTYPO3 Code Simplifier
TYPO3代码简化工具
Adapted from Boris Cherny's (Anthropic) code-simplifier agent for TYPO3 contexts. Target: TYPO3 v14 (primary), v13, v12.4 LTS (fallbacks noted).
Simplify and refine recently changed TYPO3 code. Preserve all functionality. Focus on
clarity over cleverness, TYPO3 API usage over custom implementations, and v14 patterns
over deprecated approaches.
改编自Boris Cherny(Anthropic)为TYPO3场景打造的代码简化Agent。 目标版本: TYPO3 v14(优先)、v13、v12.4 LTS(标注兼容回退方案)。
在保留所有功能的前提下,简化和优化近期修改的TYPO3代码。优先考虑代码清晰度而非技巧性,优先使用TYPO3 API而非自定义实现,优先采用v14模式而非已废弃的方案。
Process
流程
- Identify recently modified files (use or staged changes)
git diff --name-only HEAD~1 - Run three parallel review passes: Reuse, Quality, Efficiency
- Aggregate findings, deduplicate, sort by impact
- Apply fixes, verify no behavior change
- 识别近期修改的文件(使用或暂存区变更)
git diff --name-only HEAD~1 - 并行执行三轮审核:复用性、代码质量、执行效率
- 汇总发现的问题,去重后按影响程度排序
- 应用修复,验证功能无变化
Pass 1: TYPO3 API Reuse
第一轮:TYPO3 API复用
Find custom implementations that duplicate what TYPO3 already provides.
找出重复实现TYPO3已有功能的自定义代码。
Replace Custom Code with Core APIs
用核心API替换自定义代码
| Custom Pattern | TYPO3 API Replacement |
|---|---|
Manual DB queries ( | |
| Inject |
| |
| Manual JSON response construction | |
| Constructor injection via |
| |
| Inject |
| Constructor DI |
| Manual file path resolution | |
| Custom caching with globals | |
| |
| Manual page tree traversal | |
| |
| PSR-14 events or |
| Manual link generation | |
| 自定义模式 | TYPO3 API替代方案 |
|---|---|
手动数据库查询( | 带命名参数的 |
| 通过中间件/控制器注入 |
| 通过依赖注入使用 |
| 手动构造JSON响应 | 使用PSR-7的 |
| 通过 |
| |
| 注入 |
| 构造函数依赖注入 |
| 手动解析文件路径 | |
| 使用全局变量的自定义缓存 | 通过依赖注入配合缓存配置使用 |
使用 | 使用 |
| 手动遍历页面树 | |
| |
写入 | PSR-14事件或 |
| 手动生成链接 | |
Replace Deprecated Patterns (v14)
替换已废弃的模式(v14)
| Deprecated | v14 Replacement |
|---|---|
| |
| PSR-14 events |
| |
| PSR-14 events or DI decoration |
| Separate plugins per action |
| Set in controller, not repository |
| Signal/Slot | PSR-14 events |
| Extbase controller or middleware |
TCA | Dedicated TCA keys: |
| |
| |
| |
| 已废弃模式 | v14替代方案 |
|---|---|
| 在PSR-14事件上使用 |
| PSR-14事件 |
| |
| PSR-14事件或依赖注入装饰器 |
| 每个动作对应独立插件 |
在仓库中调用 | 在控制器中设置,而非仓库 |
| Signal/Slot | PSR-14事件 |
| Extbase控制器或中间件 |
TCA中用 | 使用专用TCA键: |
| |
| |
带数字数组键的 | 带 |
Pass 2: Code Quality
第二轮:代码质量
PHP Classes
PHP类
- One class per file, PSR-4 autoloading
- on every PHP file
declare(strict_types=1) - on classes not designed for inheritance
final - on immutable properties
readonly - Constructor promotion for DI dependencies
- Explicit return types on all methods
- No unused imports
use - No suppressed errors ()
@ - Guard clauses over deep nesting (early returns)
- No mixed types where specific types exist
- Replace typehints with typed arrays or DTOs
array
php
// Before
class MyService
{
private ConnectionPool $connectionPool;
private Context $context;
public function __construct(ConnectionPool $connectionPool, Context $context)
{
$this->connectionPool = $connectionPool;
$this->context = $context;
}
public function getData($id)
{
// ...
}
}
// After
final class MyService
{
public function __construct(
private readonly ConnectionPool $connectionPool,
private readonly Context $context,
) {}
public function getData(int $id): array
{
// ...
}
}- 一个文件对应一个类,遵循PSR-4自动加载规范
- 每个PHP文件开头添加
declare(strict_types=1) - 非为继承设计的类添加修饰符
final - 不可变属性添加修饰符
readonly - 依赖注入的参数使用构造函数提升
- 所有方法添加显式返回类型
- 移除未使用的导入
use - 禁止使用错误抑制符()
@ - 使用卫语句替代深层嵌套(提前返回)
- 已有特定类型时禁止使用类型
mixed - 用类型化数组或DTO替换类型提示
array
php
// Before
class MyService
{
private ConnectionPool $connectionPool;
private Context $context;
public function __construct(ConnectionPool $connectionPool, Context $context)
{
$this->connectionPool = $connectionPool;
$this->context = $context;
}
public function getData($id)
{
// ...
}
}
// After
final class MyService
{
public function __construct(
private readonly ConnectionPool $connectionPool,
private readonly Context $context,
) {}
public function getData(int $id): array
{
// ...
}
}Fluid Templates
Fluid模板
- No inline PHP or complex ViewHelper chains
- Use instead of hardcoded strings
<f:translate> - Use /
<f:link.page>instead of manual<f:link.typolink><a href> - Use instead of manual
<f:image>tags<img> - Partials for repeated markup (DRY)
- Sections for layout slots, not for reuse (use Partials)
- No unless absolutely necessary (XSS risk)
{variable -> f:format.raw()} - Variables use camelCase
- Remove empty blocks
<f:section> - Simplify nested to
<f:if>or ternary where clearer<f:switch>
- 禁止内联PHP或复杂的ViewHelper链式调用
- 使用替代硬编码字符串
<f:translate> - 使用/
<f:link.page>替代手动<f:link.typolink><a href> - 使用替代手动
<f:image>标签<img> - 重复标记使用Partials(遵循DRY原则)
- Sections仅用于布局插槽,不可复用(复用请用Partials)
- 非必要情况禁止使用(存在XSS风险)
{variable -> f:format.raw()} - 变量采用小驼峰命名法
- 移除空的块
<f:section> - 将嵌套的简化为
<f:if>或三元表达式(更清晰时)<f:switch>
TCA
TCA
- Use v14 format with
items/labelkeysvalue - Remove redundant on fields already restricted
'exclude' => true - Use dedicated types: ,
'type' => 'email','type' => 'datetime','type' => 'number','type' => 'link','type' => 'color''type' => 'json' - Use instead of
'required' => true'eval' => 'required' - Use instead of
'nullable' => true'eval' => 'null' - Remove on string fields (already default)
'default' => '' - Consolidate palette definitions (remove single-field palettes)
- Remove boilerplate definitions auto-created from
columns(v13.3+):ctrl,hidden,starttime,endtime,fe_group,sys_language_uid,l10n_parentl10n_diffsource - Use palettes for enablecolumns: for
visibility,hiddenforaccessstarttime, endtime - Remove convention fields from (auto-added to database)
ext_tables.sql - Remove unused fields from types
showitem
- 使用v14的格式,采用
items/label键value - 移除已受限制字段上多余的配置
'exclude' => true - 使用专用类型:、
'type' => 'email'、'type' => 'datetime'、'type' => 'number'、'type' => 'link'、'type' => 'color''type' => 'json' - 使用替代
'required' => true'eval' => 'required' - 使用替代
'nullable' => true'eval' => 'null' - 移除字符串字段上多余的配置(已为默认值)
'default' => '' - 合并调色板定义(移除单字段调色板)
- 移除自动生成的冗余
ctrl定义(v13.3+):columns、hidden、starttime、endtime、fe_group、sys_language_uid、l10n_parentl10n_diffsource - 为启用列使用调色板:对应
visibility,hidden对应accessstarttime, endtime - 从中移除约定字段(会自动添加到数据库)
ext_tables.sql - 移除类型中未使用的字段
showitem
Services.yaml
Services.yaml
- Use autowiring (remove explicit argument definitions when type-hintable)
- Use
_defaults: autowire: true, autoconfigure: true, public: false - Remove manual service definitions for classes that autowiring handles
- Replace definitions with
factorywhere possible#[Autoconfigure] - Remove unless needed for
public: trueGeneralUtility::makeInstance()
- 使用自动注入(类型可提示时移除显式参数定义)
- 使用
_defaults: autowire: true, autoconfigure: true, public: false - 移除自动注入可处理的类的手动服务定义
- 尽可能用替代
#[Autoconfigure]定义factory - 除非需要,否则移除
GeneralUtility::makeInstance()public: true
ext_localconf.php / ext_tables.php
ext_localconf.php / ext_tables.php
- Minimize code — move to files where possible
Configuration/ - Plugin registration only (no business logic)
- Use (not
ExtensionUtility::configurePlugin()for frontend)registerPlugin - No — use
addPageTSConfigConfiguration/page.tsconfig - No — use
addUserTSConfigConfiguration/user.tsconfig - No — use
addTypoScriptConfiguration/TypoScript/setup.typoscript
- 最小化代码 — 尽可能迁移到目录下的文件
Configuration/ - 仅保留插件注册(无业务逻辑)
- 使用(前端插件不使用
ExtensionUtility::configurePlugin())registerPlugin - 禁止使用— 改用
addPageTSConfigConfiguration/page.tsconfig - 禁止使用— 改用
addUserTSConfigConfiguration/user.tsconfig - 禁止使用— 改用
addTypoScriptConfiguration/TypoScript/setup.typoscript
Pass 3: Efficiency
第三轮:执行效率
- QueryBuilder: select only needed columns, not
* - QueryBuilder: add when expecting single row
setMaxResults() - Use queries instead of fetching all rows to count
count() - Cache expensive operations with TYPO3 Caching Framework
- Avoid N+1 queries in Extbase repositories (use or batch loading)
JOIN - Use not re-processing on every request
TYPO3\CMS\Core\Resource\ProcessedFileRepository - Remove calls without pagination
findAll() - Lazy-load file references with
@TYPO3\CMS\Extbase\Annotation\ORM\Lazy - Replace + manual
foreachfiltering with QueryBuilderin_array()WHERE IN - Remove redundant calls in CLI commands
cache:flush
- QueryBuilder:仅选择所需列,而非
* - QueryBuilder:预期单行结果时添加
setMaxResults() - 使用查询替代获取所有行后再计数
count() - 使用TYPO3缓存框架缓存耗时操作
- 避免Extbase仓库中的N+1查询(使用或批量加载)
JOIN - 使用而非每次请求都重新处理
TYPO3\CMS\Core\Resource\ProcessedFileRepository - 移除无分页的调用
findAll() - 使用懒加载文件引用
@TYPO3\CMS\Extbase\Annotation\ORM\Lazy - 用QueryBuilder的替代
WHERE IN+ 手动foreach过滤in_array() - 移除CLI命令中冗余的调用
cache:flush
Output Format
输出格式
After analysis, report findings grouped by file:
text
undefined分析完成后,按文件分组报告发现的问题:
text
undefinedClasses/Controller/MyController.php
Classes/Controller/MyController.php
:42 — replace GeneralUtility::makeInstance(MyService::class) → constructor injection
:18 — add return type
:55 — deprecated: $GLOBALS['TSFE']->id → $request routing attribute
:67 — guard clause: invert condition, return early, reduce nesting
: ResponseInterface:42 — replace GeneralUtility::makeInstance(MyService::class) → constructor injection
:18 — add return type
:55 — deprecated: $GLOBALS['TSFE']->id → $request routing attribute
:67 — guard clause: invert condition, return early, reduce nesting
: ResponseInterfaceResources/Private/Templates/List.html
Resources/Private/Templates/List.html
:12 — hardcoded string "No items found" → f:translate
:34 — manual <a href> → f:link.page
:12 — hardcoded string "No items found" → f:translate
:34 — manual <a href> → f:link.page
Configuration/TCA/Overrides/tt_content.php
Configuration/TCA/Overrides/tt_content.php
:8 — v12 items format ['Label', 'value'] → ['label' => 'Label', 'value' => 'value']
Applied 7 fixes. No behavior changes. Run tests to verify.
undefined:8 — v12 items format ['Label', 'value'] → ['label' => 'Label', 'value' => 'value']
Applied 7 fixes. No behavior changes. Run tests to verify.
undefinedVersion Fallbacks
版本兼容回退方案
When simplifying for v12/v13 compatibility:
- Keep alongside
Configuration/RequestMiddlewares.php#[AsMiddleware] - Keep event listener config alongside
Services.yaml#[AsEventListener] - Keep numeric TCA arrays alongside
items/labelformatvalue - Use only on v14+ (not available in v12)
#[Autoconfigure]
为v12/v13兼容进行简化时:
- 保留与
Configuration/RequestMiddlewares.php并存#[AsMiddleware] - 保留事件监听器配置与
Services.yaml并存#[AsEventListener] - 保留数字键的TCA 数组与
items/label格式并存value - 仅在v14+版本使用(v12不支持)
#[Autoconfigure]
v14-Only Simplification Targets
仅适用于v14的简化目标
The following simplification opportunities are v14-specific.
以下简化优化仅适用于v14版本。
v14 Simplification Patterns [v14 only]
v14简化模式 [仅v14]
| Pattern | Simplification |
|---|---|
| Replace with |
Extbase annotations ( | Replace with |
| Replace with |
| Replace with |
| Bootstrap Modal JS | Replace with native |
TCA | Replace with per-column |
| Custom localization parsers | Remove, use Symfony Translation Component |
| Remove, use System Resource API |
| 现有模式 | 简化方案 |
|---|---|
访问 | 替换为 |
Extbase注解( | 替换为PHP属性 |
| 替换为 |
使用 | 替换为 |
| Bootstrap Modal JS | 替换为原生 |
TCA | 替换为按列配置 |
| 自定义本地化解析器 | 移除,使用Symfony Translation Component |
| 移除,使用System Resource API |
Credits & Attribution
致谢与归属
Adapted from Boris Cherny's (Anthropic) code-simplifier
agent and Claude Code's bundled skill for TYPO3 contexts.
/simplifyCopyright (c) Anthropic — Code simplification patterns (MIT License)
Thanks to Netresearch DTT GmbH for their contributions to the TYPO3 community.
改编自Boris Cherny(Anthropic)的code-simplifier Agent,以及Claude Code为TYPO3场景提供的内置技能。
/simplify版权所有 (c) Anthropic — 代码简化模式(MIT许可证)
感谢Netresearch DTT GmbH为TYPO3社区做出的贡献。