Magic Number
debt(d3/e1/b3/t3)
Closest to 'default linter catches the common case' (d3). The detection_hints list phpcs and phpstan, both widely used default-pipeline tools in PHP projects that flag numeric literals in business logic automatically. Not d1 because it's not a compiler/syntax error, and not d5 because these are standard, commonly configured tools rather than specialist SAST.
Closest to 'one-line patch or single-call swap' (e1). The quick_fix explicitly states: replace the literal with a named constant (e.g. const MAX_LOGIN_ATTEMPTS = 5). This is a single-declaration fix at the point of use, though the common_mistake about defining constants in multiple files hints at slight coordination overhead — still firmly e1 for the individual occurrence.
Closest to 'localised tax' (b3). Magic numbers are a code-smell with style/readability tags, and their impact is localised to the files and functions where they appear. They don't reshape the architecture or impose a persistent cross-cutting tax, but they do slow down readers and maintainers of the affected code. Not b1 because the confusion accumulates; not b5 because the rest of the codebase is largely unaffected.
Closest to 'minor surprise (one edge case)' (t3). The misconception field identifies a real but limited trap: developers believe magic numbers are only a problem when repeated, missing that a single unexplained literal (e.g. 86400) is equally problematic. This is a documented gotcha but not a deep behavioural contradiction — a competent developer will recognise it quickly once pointed out, placing it at t3 rather than t5.
Also Known As
TL;DR
Explanation
A magic number is a bare numeric literal (e.g. 86400, 1024, 42) embedded directly in code with no accompanying explanation of what it represents. If the value needs to change, every occurrence must be found and updated. Named constants (const MAX_UPLOAD_BYTES = 1024 * 1024 * 10) make the intent clear and create a single point of change. Accepted non-magic numbers: 0, 1, -1, and context-obvious values like array indices immediately after a count.
Common Misconception
Why It Matters
Common Mistakes
- Naming constants poorly — MAX = 86400 is no better than the literal; SECONDS_PER_DAY is.
- Defining the same constant in multiple files — a single constants file or class prevents drift.
- Treating 0 and 1 as never being magic — they often are (0 = disabled, 1 = admin role).
- Using class constants when an enum is more appropriate in PHP 8.1+.
Code Examples
if ($status === 3) {
$price *= 0.85;
}
const STATUS_APPROVED = 3;
const LOYALTY_DISCOUNT = 0.85;
if ($status === STATUS_APPROVED) {
$price *= LOYALTY_DISCOUNT;
}