Skip to content

Commit e794f6d

Browse files
committed
feat: Added PHP memory optimisation rule and indexed it in AGENTS.md
1 parent b117fe3 commit e794f6d

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Confluence Editing Standards (Markdown Authoring)
2+
3+
Purpose: Ensure Markdown renders cleanly in Confluence by applying spacing, nesting, and code formatting rules during page creation/updates.
4+
5+
## Lists & Spacing
6+
- Always insert a blank line between list items.
7+
8+
- Use nested sub-bullets for subpoints; never concatenate multiple points on a single line.
9+
10+
- For ordered lists, keep each step single-purpose; add an empty line between steps when a step has sub-bullets.
11+
12+
## Inline vs Code Blocks
13+
- Prefer inline code for short, single-line commands or file paths (e.g., `composer install`).
14+
15+
- Use fenced code blocks only when necessary (multi-line commands, config snippets). Verify Confluence renders the block without collapsing; otherwise, convert to inline steps.
16+
17+
- Do not mix bullets and code blocks without a blank line before and after the block.
18+
19+
## Headings & Anchors
20+
- Keep headings concise; avoid trailing punctuation.
21+
22+
- When using Markdown format, avoid enabling heading anchors if they introduce ID artifacts in rendered output.
23+
24+
## Tables
25+
- Keep tables simple (5–7 columns max). Prefer bullets when content wraps heavily.
26+
27+
- Add a brief sentence above a large table explaining what it captures.
28+
29+
## Nested Structure
30+
- Depth guidance: limit nesting to two levels (bullet → sub-bullet). If content requires more depth, split into a new subsection.
31+
32+
## Check Before Publish (Quicklist)
33+
- Bullets have blank lines between items.
34+
35+
- Sub-bullets are properly indented and grouped.
36+
37+
- Commands are inline where possible; multi-line blocks tested for rendering.
38+
39+
- No anchor artifacts in headings.
40+
41+
- Tables are readable; consider bullets if wide.
42+
43+
## Optional Page Label Cue
44+
When editing an existing Confluence page, if the page has label `format-spaced-lists`, apply these spacing rules strictly even if the original content is inconsistent.
45+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
description: PHP memory optimisation standards and actionable checks
3+
globs: *.php, *.ini
4+
---
5+
# PHP Memory Optimisation Standards
6+
7+
Guidance and automated checks to reduce peak memory usage in PHP applications. Based on widely accepted practices and the article "PHP Memory Optimization Tips" by Khouloud Haddad.
8+
9+
<rule>
10+
name: php_memory_optimisation
11+
description: Detect memory-heavy patterns and suggest streaming, generators, and better data handling
12+
filters:
13+
- type: file_extension
14+
pattern: "\\.php$"
15+
16+
actions:
17+
- type: enforce
18+
conditions:
19+
# Avoid loading entire DB result sets into memory.
20+
- pattern: "->fetchAll\\("
21+
message: "Avoid fetchAll() on large result sets; iterate with fetch() in a loop or wrap with a generator (yield)."
22+
23+
- pattern: "\\bmysqli_fetch_all\\("
24+
message: "Avoid mysqli_fetch_all() for large queries; prefer streaming fetch (e.g., mysqli_fetch_assoc in a loop)."
25+
26+
# Avoid repeated full-file loads inside loops.
27+
- pattern: "foreach\\s*\\([^)]*\\)\\s*\\{[^}]*file_get_contents\\("
28+
message: "Avoid file_get_contents() inside loops; stream with SplFileObject or read once and reuse."
29+
30+
# Avoid array_merge in tight loops as it copies arrays.
31+
- pattern: "foreach\\s*\\([^)]*\\)\\s*\\{[^}]*=\\s*array_merge\\("
32+
message: "Avoid array_merge() inside loops; append elements directly or preallocate arrays."
33+
34+
# Use caution with range() on large ranges (allocates full array).
35+
- pattern: "\\brange\\s*\\("
36+
message: "range() allocates full arrays; for large ranges consider generators (yield) to avoid high memory."
37+
38+
- type: suggest
39+
message: |
40+
**PHP memory optimisation recommendations:**
41+
42+
- **Stream database results:** Prefer `$stmt->fetch(PDO::FETCH_ASSOC)` in a `while` loop or use generators instead of `fetchAll()`.
43+
- **Use generators (yield):** Iterate large datasets without allocating full arrays.
44+
- **Stream files:** Use `SplFileObject` or chunked reads instead of `file_get_contents()` for large files.
45+
- **Minimise array copying:** Avoid `array_merge()` in loops; push items directly or pre-size with known capacity (e.g., `SplFixedArray`).
46+
- **Free memory explicitly:** `unset($var)` after large data is no longer needed; consider `gc_collect_cycles()` for long-running scripts.
47+
- **Profile memory:** Use `memory_get_usage()` and tools like Xdebug/Blackfire to spot peaks.
48+
- **OPcache:** Ensure OPcache is enabled and sized appropriately in production.
49+
50+
- type: validate
51+
conditions:
52+
# Detect full-file reads that likely could be streamed.
53+
- pattern: "\\bfile\\s*\\("
54+
message: "file() reads entire files into memory; prefer SplFileObject for line-by-line streaming."
55+
56+
metadata:
57+
priority: high
58+
version: 1.0
59+
</rule>
60+
61+
<rule>
62+
name: php_ini_opcache_recommendations
63+
description: Recommend enabling OPcache for lower memory and better performance when editing php.ini
64+
filters:
65+
- type: file_extension
66+
pattern: "\\.ini$"
67+
68+
actions:
69+
- type: suggest
70+
message: |
71+
**OPcache recommendations (php.ini):**
72+
- Set `opcache.enable=1` and `opcache.enable_cli=1` for CLI scripts that process large datasets.
73+
- Size memory pool appropriately, e.g., `opcache.memory_consumption=128` (adjust to your project).
74+
- Consider `opcache.interned_strings_buffer` and `opcache.max_accelerated_files` for larger codebases.
75+
76+
- type: enforce
77+
conditions:
78+
- pattern: "(?mi)^opcache\\.enable\\s*=\\s*0"
79+
message: "Enable OPcache in production (set opcache.enable=1) to reduce memory and CPU overhead."
80+
81+
metadata:
82+
priority: medium
83+
version: 1.0
84+
</rule>
85+

AGENTS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Below are the rule bundles and their rule files. Each item links directly to the
3232
- [.cursor/rules/pull-request-changelist-instructions.mdc](.cursor/rules/pull-request-changelist-instructions.mdc)
3333
- [.cursor/rules/readme-maintenance-standards.mdc](.cursor/rules/readme-maintenance-standards.mdc)
3434
- [.cursor/rules/testing-guidelines.mdc](.cursor/rules/testing-guidelines.mdc)
35+
- [.cursor/rules/confluence-editing-standards.mdc](.cursor/rules/confluence-editing-standards.mdc)
3536

3637
### Web Stack
3738
- [.cursor/rules/accessibility-standards.mdc](.cursor/rules/accessibility-standards.mdc)
@@ -63,6 +64,7 @@ Below are the rule bundles and their rule files. Each item links directly to the
6364
- [.cursor/rules/node-dependencies.mdc](.cursor/rules/node-dependencies.mdc)
6465
- [.cursor/rules/php-drupal-best-practices.mdc](.cursor/rules/php-drupal-best-practices.mdc)
6566
- [.cursor/rules/php-drupal-development-standards.mdc](.cursor/rules/php-drupal-development-standards.mdc)
67+
- [.cursor/rules/php-memory-optimisation.mdc](.cursor/rules/php-memory-optimisation.mdc)
6668
- [.cursor/rules/project-definition-template.mdc](.cursor/rules/project-definition-template.mdc)
6769
- [.cursor/rules/react-patterns.mdc](.cursor/rules/react-patterns.mdc)
6870
- [.cursor/rules/security-practices.mdc](.cursor/rules/security-practices.mdc)

0 commit comments

Comments
 (0)