cursorrules / rules
ivangrynenko/cursorrules/.cursor/rules/php-memory-optimisation.mdc
PHP memory optimisation standards and actionable checks
Cursor rule88 starsChanged 11 months ago
What's in it
- PHP Memory Optimisation Standards
---
description: PHP memory optimisation standards and actionable checks
globs: *.php, *.ini
---
# PHP Memory Optimisation Standards
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.
<rule>
name: php_memory_optimisation
description: Detect memory-heavy patterns and suggest streaming, generators, and better data handling
filters:
- type: file_extension
pattern: "\\.php$"
actions:
- type: enforce
conditions:
# Avoid loading entire DB result sets into memory.
- pattern: "->fetchAll\\("
message: "Avoid fetchAll() on large result sets; iterate with fetch() in a loop or wrap with a generator (yield)."
- pattern: "\\bmysqli_fetch_all\\("
message: "Avoid mysqli_fetch_all() for large queries; prefer streaming fetch (e.g., mysqli_fetch_assoc in a loop)."
# Avoid repeated full-file loads inside loops.
- pattern: "foreach\\s*\\([^)]*\\)\\s*\\{[^}]*file_get_contents\\("
message: "Avoid file_get_contents() inside loops; stream with SplFileObject or read once and reuse."
# Avoid array_merge in tight loops as it copies arrays.
- pattern: "foreach\\s*\\([^)]*\\)\\s*\\{[^}]*=\\s*array_merge\\("
message: "Avoid array_merge() inside loops; append elements directly or preallocate arrays."
# Use caution with range() on large ranges (allocates full array).
- pattern: "\\brange\\s*\\("
message: "range() allocates full arrays; for large ranges consider generators (yield) to avoid high memory."
- type: suggest
message: |
**PHP memory optimisation recommendations:**
- **Stream database results:** Prefer `$stmt->fetch(PDO::FETCH_ASSOC)` in a `while` loop or use generators instead of `fetchAll()`.
- **Use generators (yield):** Iterate large datasets without allocating full arrays.
- **Stream files:** Use `SplFileObject` or chunked reads instead of `file_get_contents()` for large files.
- **Minimise array copying:** Avoid `array_merge()` in loops; push items directly or pre-size with known capacity (e.g., `SplFixedArray`).
- **Free memory explicitly:** `unset($var)` after large data is no longer needed; consider `gc_collect_cycles()` for long-running scripts.
- **Profile memory:** Use `memory_get_usage()` and tools like Xdebug/Blackfire to spot peaks.
- **OPcache:** Ensure OPcache is enabled and sized appropriately in production.
- type: validate
conditions:
# Detect full-file reads that likely could be streamed.
- pattern: "\\bfile\\s*\\("
message: "file() reads entire files into memory; prefer SplFileObject for line-by-line streaming."
metadata:
priority: high
version: 1.0
</rule>
<rule>
name: php_ini_opcache_recommendations
description: Recommend enabling OPcache for lower memory and better performance when editing php.ini
filters:
- type: file_extension
pattern: "\\.ini$"
actions:
- type: suggest
message: |
**OPcache recommendations (php.ini):**
- Set `opcache.enable=1` and `opcache.enable_cli=1` for CLI scripts that process large datasets.
- Size memory pool appropriately, e.g., `opcache.memory_consumption=128` (adjust to your project).
- Consider `opcache.interned_strings_buffer` and `opcache.max_accelerated_files` for larger codebases.
- type: enforce
conditions:
- pattern: "(?mi)^opcache\\.enable\\s*=\\s*0"
message: "Enable OPcache in production (set opcache.enable=1) to reduce memory and CPU overhead."
metadata:
priority: medium
version: 1.0
</rule>
More agent context in ivangrynenko/cursorrules
70 other files this repository gives its agents, the first 60 shown.
AGENTS.md
CLAUDE.md
Cursor rule
- .cursor/rules/accessibility-standards.mdc
- .cursor/rules/api-standards.mdc
- .cursor/rules/behat-ai-guide.mdc
- .cursor/rules/behat-steps.mdc
- .cursor/rules/build-optimization.mdc
- .cursor/rules/code-generation-standards.mdc
- .cursor/rules/confluence-editing-standards.mdc
- .cursor/rules/cursor-rules.mdc
- .cursor/rules/debugging-standards.mdc
- .cursor/rules/docker-compose-standards.mdc
- .cursor/rules/drupal-authentication-failures.mdc
- .cursor/rules/drupal-broken-access-control.mdc
- .cursor/rules/drupal-cryptographic-failures.mdc
- .cursor/rules/drupal-database-standards.mdc
- .cursor/rules/drupal-file-permissions.mdc
- .cursor/rules/drupal-injection.mdc
- .cursor/rules/drupal-insecure-design.mdc
- .cursor/rules/drupal-integrity-failures.mdc
- .cursor/rules/drupal-logging-failures.mdc
- .cursor/rules/drupal-security-misconfiguration.mdc
- .cursor/rules/drupal-ssrf.mdc
- .cursor/rules/drupal-vulnerable-components.mdc
- .cursor/rules/generic_bash_style.mdc
- .cursor/rules/git-commit-standards.mdc
- .cursor/rules/github-actions-standards.mdc
- .cursor/rules/improve-cursorrules-efficiency.mdc
- .cursor/rules/javascript-broken-access-control.mdc
- .cursor/rules/javascript-cryptographic-failures.mdc
- .cursor/rules/javascript-identification-authentication-failures.mdc
- .cursor/rules/javascript-injection.mdc
- .cursor/rules/javascript-insecure-design.mdc
- .cursor/rules/javascript-performance.mdc
- .cursor/rules/javascript-security-logging-monitoring-failures.mdc
- .cursor/rules/javascript-security-misconfiguration.mdc
- .cursor/rules/javascript-server-side-request-forgery.mdc
- .cursor/rules/javascript-software-data-integrity-failures.mdc
- .cursor/rules/javascript-standards.mdc
- .cursor/rules/javascript-vulnerable-outdated-components.mdc
- .cursor/rules/lagoon-docker-compose-standards.mdc
- .cursor/rules/lagoon-yml-standards.mdc
- .cursor/rules/multi-agent-coordination.mdc
- .cursor/rules/new-pull-request.mdc
- .cursor/rules/node-dependencies.mdc
- .cursor/rules/php-drupal-best-practices.mdc
- .cursor/rules/php-drupal-development-standards.mdc
- .cursor/rules/project-definition-template.mdc
- .cursor/rules/pull-request-changelist-instructions.mdc
- .cursor/rules/python-authentication-failures.mdc
- .cursor/rules/python-broken-access-control.mdc
- .cursor/rules/python-cryptographic-failures.mdc
- .cursor/rules/python-injection.mdc
- .cursor/rules/python-insecure-design.mdc
- .cursor/rules/python-integrity-failures.mdc
- .cursor/rules/python-logging-monitoring-failures.mdc
- .cursor/rules/python-security-misconfiguration.mdc
- .cursor/rules/python-ssrf.mdc
- .cursor/rules/python-vulnerable-outdated-components.mdc
- .cursor/rules/react-patterns.mdc
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
No reports yet. Be the first to say whether it worked.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool public_context_discussion, action report. How to connect one.

