Skip to content
acc-check-encapsulation logo

Encapsulation Analyzer

acc-check-encapsulation

Analyzes PHP code for encapsulation violations. Detects public mutable state, exposed internals, Tell Don't Ask violations, getter/setter abuse, and information hiding breaches.

SKILL.md

Full skill instructions

Encapsulation Analyzer

Overview

This skill analyzes PHP codebases for encapsulation violations — situations where internal state is exposed, getters/​setters replace behavior, or the "Tell, Don't Ask" principle is violated.

Encapsulation Principles

PrincipleDescriptionViolation Indicator
Information HidingInternal state not exposedPublic properties, many getters
Tell Don't AskObjects perform actions, not expose dataGetter chains, external decisions
Behavioral RichnessObjects have behavior, not just dataAnemic domain model
Invariant ProtectionState changes validate constraintsPublic setters without validation

Detection Patterns

Phase 1: Public Mutable State

# Public properties (non-readonly)
Grep: "public string|public int|public float|public bool|public array|public \?" --glob "**/​Domain/​**/​*.php"

# Public properties in entities
Grep: "public \$|public string \$|public int \$" --glob "**/​Entity/​**/​*.php"

# Expected: private/​protected or public readonly
Grep: "public readonly|private readonly|protected readonly" --glob "**/​Domain/​**/​*.php"

Violations:

// BAD: Public mutable state
class User
{
    public string $email;      // Can be modified externally!
    public int $age;           // No validation!
    public array $permissions; // Collection exposed!
}

// GOOD: Encapsulated state
final class User
{
    private Email $email;
    private Age $age;
    private PermissionCollection $permissions;

    public function changeEmail(Email $newEmail): void { /​* ... */ }
    public function grantPermission(Permission $permission): void { /​* ... */ }
}

Phase 2: Getter/​Setter Abuse

# Getter/​setter pairs (anemic model indicator)
Grep: "public function get[A-Z][a-z]+\(\)" --glob "**/​Domain/​**/​*.php"
Grep: "public function set[A-Z][a-z]+\(" --glob "**/​Domain/​**/​*.php"

# Count getters vs behavior methods
# High getter ratio = anemic model

# Setters in entities (should be behavior methods)
Grep: "public function set[A-Z]" --glob "**/​Entity/​**/​*.php"
Grep: "public function set[A-Z]" --glob "**/​Aggregate/​**/​*.php"

# Direct property setters
Grep: "\$this->[a-z]+ = \$" --glob "**/​Domain/​**/​*.php"
# Check if inside validated method or public setter

Getter/​Setter Anti-pattern:

// BAD: Anemic entity
class Order
{
    public function getStatus(): string { return $this->status; }
    public function setStatus(string $status): void { $this->status = $status; }
}

// External code makes decisions
if ($order->getStatus() === 'pending') {
    $order->setStatus('confirmed');
}

// GOOD: Rich entity
final class Order
{
    public function confirm(): void
    {
        if ($this->status !== OrderStatus::PENDING) {
            throw new CannotConfirmOrderException();
        }
        $this->status = OrderStatus::CONFIRMED;
        $this->recordEvent(new OrderConfirmedEvent($this->id));
    }
}

Phase 3: Tell Don't Ask Violations

# Getter chains (asking for data to make decisions)
Grep: "->get[A-Z][a-z]+\(\)->get[A-Z][a-z]+\(\)" --glob "**/​*.php"
Grep: "if \(\$.*->get[A-Z].*->get[A-Z]" --glob "**/​*.php"

# External conditionals on object state
Grep: "if \(\$[a-z]+->get[A-Z][a-z]+\(\) ===" --glob "**/​*.php"
Grep: "if \(\$[a-z]+->is[A-Z][a-z]+\(\))" --glob "**/​*.php"

# Switch on object state
Grep: "switch \(\$.*->get[A-Z]|match \(\$.*->get[A-Z]" --glob "**/​*.php"

Tell Don't Ask Pattern:

// BAD: Ask then act
if ($user->getBalance()->getAmount() >= $payment->getAmount()) {
    $user->setBalance(
        $user->getBalance()->subtract($payment->getAmount())
    );
}

// GOOD: Tell
$user->pay($payment);

// Inside User
public function pay(Payment $payment): void
{
    if (!$this->balance->canAfford($payment->amount())) {
        throw new InsufficientBalanceException();
    }
    $this->balance = $this->balance->subtract($payment->amount());
    $this->recordEvent(new PaymentMadeEvent($this->id, $payment->id()));
}

Phase 4: Collection Exposure

# Returning mutable collections
Grep: "public function get[A-Z][a-z]+\(\): array" --glob "**/​Entity/​**/​*.php" -A 3
# Check if returns internal array directly

# Doctrine collections exposed
Grep: "public function get[A-Z][a-z]+\(\): Collection" --glob "**/​Domain/​**/​*.php"

# Array modifications outside entity
Grep: "\$.*->get[A-Z][a-z]+\(\)\[\]|array_push\(\$.*->get" --glob "**/​*.php"

Collection Encapsulation:

// BAD: Collection exposed
class Order
{
    /​** @return OrderItem[] */
    public function getItems(): array
    {
        return $this->items; // Internal array exposed!
    }
}

// External modification
$order->getItems()[] = $newItem; // Bypasses validation!

// GOOD: Collection encapsulated
final class Order
{
    public function addItem(Product $product, Quantity $quantity): void
    {
        $this->validateCanAddItem($product);
        $this->items[] = new OrderItem($product, $quantity);
        $this->recalculateTotal();
    }

    /​** @return OrderItem[] */
    public function items(): array
    {
        return [...$this->items]; // Return copy
    }
}

Phase 5: Exposed Internals

# Internal state returned
Grep: "return \$this->[a-z]+;" --glob "**/​Domain/​**/​*.php"
# Check if returning mutable objects

# Private field via reflection
Grep: "ReflectionClass|ReflectionProperty|setAccessible" --glob "**/​*.php"

# Debug/​dump methods exposing state
Grep: "public function toArray\(\)|public function dump\(\)|public function debug\(\)" --glob "**/​Domain/​**/​*.php"

# Serialization exposing internals
Grep: "__serialize|__sleep|jsonSerialize" --glob "**/​Domain/​**/​*.php"

Phase 6: Constructor Injection Issues

# Too many dependencies (SRP violation indicator)
Grep: "__construct\(" --glob "**/​Domain/​**/​*.php" -A 15
# Count parameters

# Public constructor with complex setup
Grep: "public function __construct" --glob "**/​Domain/​**/​*.php" -A 20
# Check for business logic in constructor

# Missing factory for complex construction
Grep: "new [A-Z][a-z]+Entity\(|new [A-Z][a-z]+Aggregate\(" --glob "**/​Application/​**/​*.php"
# Complex instantiation outside factory

Report Format

# Encapsulation Analysis Report

## Summary

| Issue Type | Critical | Warning | Info |
|------------|----------|---------|------|
| Public Mutable State | 3 | 5 | - |
| Getter/​Setter Abuse | 2 | 8 | 12 |
| Tell Don't Ask | 4 | 15 | - |
| Collection Exposure | 2 | 6 | - |
| Exposed Internals | 1 | 3 | 4 |

**Encapsulation Score: 68%**

## Critical Issues

### ENC-001: Public Mutable Properties
- **File:** `src/​Domain/​User/​Entity/​User.php:12`
- **Issue:** Public properties allow external modification
- **Code:**
  ```php
  public string $email;
  public string $name;
  public array $roles;
  • Expected:
    private Email $email;
    private Name $name;
    private RoleCollection $roles;
    
    public function changeEmail(Email $email): void { /​* validate */ }
    
  • Skills: acc-create-entity, acc-create-value-object

ENC-002: Anemic Entity

  • File: src/​Domain/​Order/​Entity/​Order.php
  • Issue: 15 getters, 12 setters, 0 behavior methods
  • Code:
    public function getStatus(): string { ... }
    public function setStatus(string $status): void { ... }
    
  • Expected: Replace setters with behavior methods
    public function confirm(): void { /​* validate and transition */ }
    public function ship(TrackingNumber $tracking): void { /​* ... */ }
    public function cancel(CancellationReason $reason): void { /​* ... */ }
    
  • Skills: acc-create-entity

ENC-003: Collection Mutated Externally

  • File: src/​Application/​Service/​OrderService.php:45
  • Issue: Adding items bypasses entity validation
  • Code:
    $order->getItems()[] = $newItem;
    
  • Expected:
    $order->addItem($product, $quantity);
    

Warning Issues

ENC-004: Tell Don't Ask Violation

  • File: src/​Application/​Handler/​ConfirmOrderHandler.php:34
  • Issue: External logic should be in entity
  • Code:
    if ($order->getStatus() === 'pending' &&
        $order->getPayment()->getStatus() === 'completed') {
        $order->setStatus('confirmed');
    }
    
  • Expected:
    $order->confirm(); // Validation inside entity
    

ENC-005: Getter Chain

  • File: src/​Application/​Service/​ReportService.php:78
  • Issue: Law of Demeter violation
  • Code:
    $country = $user->getAddress()->getCity()->getCountry()->getName();
    
  • Refactoring Options:
    1. Add shortcut: $user->countryName()
    2. Pass needed data: new Report($user->address()->countryName())

ENC-006: Internal Array Returned

  • File: src/​Domain/​Order/​Entity/​Order.php:89
  • Issue: Internal array returned by reference
  • Code:
    public function getItems(): array
    {
        return $this->items;
    }
    
  • Expected:
    /​** @return OrderItem[] */
    public function items(): array
    {
        return [...$this->items]; // Return copy
    }
    

Metrics

Getter/​Behavior Ratio

EntityGettersSettersBehaviorRatioStatus
User8534.3⚠️ Poor
Order1512213.5❌ Anemic
Product6051.2✅ Good
Payment4241.5✅ Good

Target: Ratio < 2.0 (behaviors should outnumber getters)

Public State Exposure

LayerPublic PropsReadonly PropsPrivate Props
Domain12 ❌845
Application02315

Refactoring Recommendations

Immediate

  1. Make all entity properties private
  2. Replace setters with behavior methods
  3. Return collection copies, not references

Short-term

  1. Extract Value Objects for validated data
  2. Add factory methods for complex construction
  3. Remove getter chains (add shortcut methods)

Long-term

  1. Review anemic entities for missing behavior
  2. Consider CQRS to separate read/​write models

## Encapsulation Patterns

### Rich Entity Example

```php
final class Order
{
    private OrderId $id;
    private CustomerId $customerId;
    private OrderStatus $status;
    private OrderItemCollection $items;
    private Money $total;
    private array $events = [];

    public static function create(CustomerId $customerId): self
    {
        $order = new self();
        $order->id = OrderId::generate();
        $order->customerId = $customerId;
        $order->status = OrderStatus::DRAFT;
        $order->items = new OrderItemCollection();
        $order->total = Money::zero();

        $order->recordEvent(new OrderCreatedEvent($order->id));

        return $order;
    }

    public function addItem(Product $product, Quantity $quantity): void
    {
        $this->assertDraft();

        $item = OrderItem::create($product, $quantity);
        $this->items = $this->items->add($item);
        $this->recalculateTotal();
    }

    public function submit(): void
    {
        $this->assertDraft();
        $this->assertHasItems();

        $this->status = OrderStatus::SUBMITTED;
        $this->recordEvent(new OrderSubmittedEvent($this->id));
    }

    // Query methods (no state exposure)
    public function id(): OrderId { return $this->id; }
    public function total(): Money { return $this->total; }
    public function isSubmitted(): bool { return $this->status->equals(OrderStatus::SUBMITTED); }

    private function assertDraft(): void
    {
        if (!$this->status->equals(OrderStatus::DRAFT)) {
            throw new OrderNotDraftException($this->id);
        }
    }
}

Quick Analysis Commands

# Check encapsulation
echo "=== Public Properties ===" && \
grep -rn "public string\|public int\|public array" --include="*.php" src/​Domain/ | grep -v "readonly" && \
echo "=== Setter Methods ===" && \
grep -rn "public function set[A-Z]" --include="*.php" src/​Domain/ && \
echo "=== Getter Chains ===" && \
grep -rn "->get[A-Z].*->get[A-Z].*->get[A-Z]" --include="*.php" src/ && \
echo "=== Tell Don't Ask ===" && \
grep -rn "if (\$.*->get[A-Z].*===\|switch (\$.*->get[A-Z]" --include="*.php" src/​Application/

Integration

Works with:

  • acc-detect-code-smells — Feature Envy, Anemic Model
  • acc-structural-auditor — DDD compliance
  • acc-create-entity — Generate rich entities
  • acc-create-value-object — Encapsulated value types

References

  • "Tell, Don't Ask" — Martin Fowler
  • "Anemic Domain Model" — Martin Fowler
  • "Object-Oriented Software Construction" (Bertrand Meyer)
  • "Elegant Objects" (Yegor Bugayenko)

More skills from diegosouzapw

accessibility logo
diegosouzapw/awesome-omni-skill

accessibility

Accessibility guidelines for VS Code features — covers accessibility help dialogs, accessible views, verbosity settings, accessibility signals, ARIA alerts/status announcements, keyboard navigation, and ARIA labels/roles. Applies to both new interactive UI surfaces and updates to existing feature...

62 1
View
mermaid-diagrams logo
diegosouzapw/awesome-omni-skill

mermaid-diagrams

Mermaid diagram creation for flowcharts, sequences, ERDs, and more. Generate diagrams from text in markdown files. Use for documentation, architecture diagrams, and visual representations. Triggers on mermaid, flowchart, sequence diagram, ERD, entity relationship, gantt chart, pie chart, class di...

62 1
View
tauri2-react-rust logo
diegosouzapw/awesome-omni-skill

tauri2-react-rust

Guides development of cross-platform desktop apps with Tauri 2, TypeScript, React, and Rust. Use when building Tauri apps, implementing IPC, designing Rust backend or TypeScript/React frontend, when researching or cloning a website (open site, snapshot elements), when verifying local dev or built...

62 1
View
fp-ts-errors logo
diegosouzapw/awesome-omni-skill

fp-ts-errors

Handle errors as values using fp-ts Either and TaskEither for cleaner, more predictable TypeScript code. Use when implementing error handling patterns with fp-ts.

62 0
View
ring:pre-dev-research logo
diegosouzapw/awesome-omni-skill

ring:pre-dev-research

Gate 0 research phase for pre-dev workflow. Dispatches 3 parallel research agents to gather codebase patterns, external best practices, and framework documentation BEFORE creating PRD/TRD. Outputs research.md with file:line references.

62 0
View
create-assistant logo
diegosouzapw/awesome-omni-skill

create-assistant

Create and configure Vapi voice AI assistants with models, voices, transcribers, tools, hooks, and advanced settings. Use when building voice agents, phone bots, customer support assistants, or any conversational AI that handles phone or web calls.

62 0
View
codex-headless logo
diegosouzapw/awesome-omni-skill

codex-headless

Delegiere Aufgaben an OpenAI Codex CLI im Headless-Mode. Nutzt ChatGPT Subscription (KEIN API Key). Nur Workspace-Zugriff, KEIN System-Zugriff. Auto-Accept mit --full-auto. Web-Recherche mit --search.

62 0
View
social-media-scheduler logo
diegosouzapw/awesome-omni-skill

social-media-scheduler

Generate a full week of social media content for any topic. Outputs platform-optimized posts for Twitter/X, LinkedIn, and Instagram with hashtags and posting times.

62 0
View
Data Privacy Compliance logo
diegosouzapw/awesome-omni-skill

Data Privacy Compliance

Data privacy and regulatory compliance specialist for GDPR, CCPA, HIPAA, and international data protection laws. Use when implementing privacy controls, conducting data protection impact assessments, ensuring regulatory compliance, or managing data subject rights. Expert in consent management, da...

62 0
View
triple-review logo
diegosouzapw/awesome-omni-skill

triple-review

Reviews a task using the TRIPLE protocol, as the review agent.

62 0
View
Tech Stack & Architecture Decision logo
diegosouzapw/awesome-omni-skill

Tech Stack & Architecture Decision

Define and document the technology stack and architecture decisions for a project. Use when the user needs to choose a tech stack, make architecture decisions, define infrastructure choices, or document technology selections. Triggers on requests like "define the tech stack", "choose technologies...

62 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View