Table of Contents
5. How to Review AI‑Generated Code — A Calm, Structured Approach
AI can generate PHP quickly,
but it cannot guarantee clarity, safety, or architectural coherence.
Reviewing AI‑generated code is not about fixing mistakes
— it’s about restoring shape, intent, and predictability.
This page offers a calm, structured approach to reviewing AI’s output with confidence.
It may be one of the most important pages in the entire primer
1. Start With the Shape, Not the Details
Before reading a single line, we should ask:
- What is this code trying to be?
- What is the shape of the data?
- What is the lifecycle of the object?
- What is the boundary of the function?
- What is the contract of the method?
If the shape is wrong, the details don’t matter.
Example signals of wrong shape:
- arrays where objects should exist
- static methods instead of instance methods
- DTOs mixed with associative arrays
- inconsistent return types
- unclear boundaries
Fix the shape first.
Everything else follows.
2. Check for Mixed Eras of PHP
AI often blends:
- PHP 5 patterns
- PHP 7 type hints
- PHP 8 features
- docblocks that duplicate types
- magic methods mixed with modern constructs
We should ask:
- Does this code belong to PHP 8+?
- Is it mixing old and new idioms?
- Are modern features used with purpose?
If the era is inconsistent,
we should ask AI to rewrite it in a single, modern style.
3. Look for Over‑Engineering
AI loves ceremony:
- factories for trivial objects
- service locators
- unnecessary interfaces
- abstract classes with one implementation
- dependency injection containers inside functions
We should ask:
- Is this complexity necessary?
- Does this abstraction express meaning?
- Can this be simpler?
Modern PHP favors clarity over cleverness.
4. Inspect Data Shape Carefully
AI often drifts when handling data:
- inconsistent array keys
- mixing associative and numeric arrays
- changing return shapes between calls
- deeply nested arrays
- missing validation
- missing null handling
We should ask:
- Is the data shape predictable?
- Is the return type stable?
- Should this be a DTO instead of an array?
- Data shape is the backbone of modern PHP.
5. Check Feature Usage for Intent
AI uses modern features decoratively:
- readonly on objects that mutate
- enums used as fancy constants
- attributes with no system behind them
- union types where a clear object is better
We should ask:
- Why is this feature here?
- Does it express meaning?
- Does it clarify or obscure intent?
Modern PHP features are tools of expression,
not decoration.
6. Review Error Handling and Edge Cases
AI often:
- forgets to validate input
- assumes strict typing where none exists
- mishandles null
- ignores exceptions
- uses error suppression
- invents nonexistent functions
We should ask:
- What happens when the input is invalid?
- What happens when the dependency fails?
- What happens when the data is missing?
Modern PHP is explicit about failure.
7. Check for Patterns Imported From Other Languages
AI frequently imports:
- async/await
- decorators
- dataclasses
- pattern matching
- Java‑style interfaces
- TypeScript‑style type inference
We should ask:
- Does this pattern exist in PHP?
- Is this a hallucination?
- Is this a misapplied idea from another ecosystem?
If it feels foreign, it probably is.
8. Ask AI to Explain Its Choices
This is one of the most powerful review tools.
We should ask AI:
- “Explain why you structured it this way.”
AI will reveal:
- assumptions
- borrowed patterns
- misunderstandings
- unnecessary abstractions
- incorrect mental models
Once we see the reasoning,
we can guide it back into clarity.
9. Iterate With Calm, Precise Corrections
When something is off, don’t rewrite it yourself.
Guide AI with small, intentional adjustments:
"Use a readonly DTO instead of an array."
"Remove unnecessary abstractions."
"Rewrite this in PHP 8+ style only."
"Avoid static methods here."
"Use enums only when they express meaning."
AI responds best to direction, not patches.
Summary
Reviewing AI‑generated code means:
- checking shape before details
- ensuring a consistent era of PHP
- removing unnecessary complexity
- enforcing predictable data shape
- using modern features with purpose
- validating edge cases
- avoiding patterns from other languages
- asking AI to explain its choices
- iterating calmly
AI can draft code quickly.
We ensure the architecture stays clear, modern, and intentional.
This page describes common behaviors observed in AI models when generating PHP code.
These patterns are not guarantees, but they are consistent enough to be useful when reviewing or guiding AI output.
