Cohesion
debt(d5/e5/b7/t5)
Closest to 'specialist tool catches' (d5). PHPStan and PHPMD (from detection_hints.tools) can flag some cohesion issues like god classes or excessive class size, but the detection_hints explicitly state automated=no — these tools catch symptoms (class size, method count) rather than true cohesion violations. Many low-cohesion designs pass static analysis and only surface through careful code review.
Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix suggests splitting a class 'along the and boundary' — this is rarely a one-line fix. Extracting responsibilities from a low-cohesion class typically requires creating new classes, moving methods, updating dependencies, and adjusting callers. For a god class, this can span multiple files.
Closest to 'strong gravitational pull' (b7). Low cohesion accumulates as a decay pattern — common_mistakes notes that god classes 'accumulate behaviour because they are already imported'. Once a low-cohesion class becomes central, every new feature gravitates toward it. The applies_to shows this affects all PHP contexts (web, cli, queue-worker), and the architectural/oop tags indicate this shapes system structure. Refactoring away from a god class often means reshaping how the entire codebase is organized.
Closest to 'notable trap' (t5). The misconception field explicitly states developers wrongly believe 'a class with many methods is low-cohesion' when actually cohesion measures purpose alignment, not size. Common_mistakes reinforces the trap: confusing cohesion with coupling, and using line count as the sole metric. These are documented gotchas most developers eventually learn, but the initial intuition is often wrong.
Also Known As
TL;DR
Explanation
Cohesion measures how focused a module's responsibilities are. A highly cohesive class has all its methods and properties working toward a single, well-defined purpose. Low cohesion — methods that have little relation to each other — indicates the class is doing too many things and should be split (see God Class, Single Responsibility). High cohesion is desirable: it makes classes easier to understand, test, reuse, and maintain. Cohesion and coupling are inversely related — increasing cohesion typically reduces unnecessary coupling.
Common Misconception
Why It Matters
Common Mistakes
- Adding helper methods to a class because it is convenient, not because they belong there — erodes cohesion over time.
- God classes that accumulate behaviour because they are already imported — a symptom of low cohesion.
- Confusing cohesion with coupling — they are related but distinct; a class can be highly cohesive and still tightly coupled.
- Not refactoring when a class exceeds 200-300 lines — size is often a cohesion warning sign.
Code Examples
// Low cohesion — unrelated responsibilities jammed together
class Utils {
public function formatDate(\DateTime $d): string { ... }
public function sendEmail(string $to, string $body): void { ... }
public function hashPassword(string $pw): string { ... }
public function resizeImage(string $path, int $w): void { ... }
}
// High cohesion — each class has one tight purpose
class DateFormatter { public function format(\DateTime $d): string { ... } }
class Mailer { public function send(string $to, string $body): void { ... } }
class PasswordHasher { public function hash(string $pw): string { ... } }
class ImageResizer { public function resize(string $path, int $w): void { ... } }