Stacks
debt(d7/e1/b3/t5)
Closest to 'only careful code review or runtime testing' (d7). The detection_hints indicate automated detection is 'no' and the only tool listed is Blackfire (a profiler). The common mistake of using array_shift instead of array_pop is a performance bug that produces correct output but degrades silently under load — only a profiler or careful code review would catch it. It won't surface as an error in normal testing.
Closest to 'one-line patch or single-call swap' (e1). The quick_fix is explicit: replace array_shift with array_pop — a single-call swap. The fix is mechanical and localized to a single usage site.
Closest to 'localised tax' (b3). The data structure choice is scoped to the algorithm or component that uses it. It applies across web/cli/queue contexts but the decision itself is local to wherever the stack is implemented — it doesn't impose a cross-codebase structural burden. Other parts of the codebase are unaffected.
Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field identifies the core trap: confusing stacks (LIFO) with queues (FIFO), and the common_mistakes list confirms developers frequently use array_shift (queue behavior) when array_pop (stack behavior) is needed. This is a well-documented gotcha that competent developers eventually learn but routinely get wrong initially.
Also Known As
TL;DR
Explanation
A stack supports three operations: push (add to top), pop (remove from top), peek (read top without removing). All are O(1). PHP's SplStack implements a doubly-linked-list-backed stack. The call stack is the most familiar stack — each function call pushes a frame, each return pops it. Stacks naturally model any 'reverse the order' or 'undo' problem. Bracket matching, postfix expression evaluation, and iterative DFS all use stacks explicitly.
Diagram
flowchart TD
subgraph Stack_LIFO
TOP[TOP] --> E3[Element C - last in]
E3 --> E2[Element B]
E2 --> E1[Element A - first in]
E1 --> BOTTOM[BOTTOM]
end
PUSH[push C] -->|O of 1| TOP
POP[pop] -->|O of 1 returns C| E3
subgraph Applications
CALLSTACK[Call stack<br/>function frames]
UNDO[Undo history]
DFS2[DFS traversal]
BRACKETS[Bracket matching]
end
style TOP fill:#238636,color:#fff
style E3 fill:#1f6feb,color:#fff
Common Misconception
Why It Matters
Common Mistakes
- Implementing a stack with array_push/array_shift — shift is O(n); use array_push/array_pop (both O(1)).
- Not checking isEmpty() before pop — popping an empty stack throws an exception in SplStack.
- Using a queue where a stack is needed — the order of processing is reversed.
- Confusing stack overflow (too many recursive calls) with heap overflow (memory exhaustion).
Code Examples
// Using array_shift as 'pop' — O(n) reindex:
$stack = [];
array_push($stack, 'a');
array_push($stack, 'b');
$top = array_shift($stack); // 'a' — wrong order AND O(n)
// array_pop is O(1) — correct LIFO:
$stack = [];
array_push($stack, 'a');
array_push($stack, 'b');
$top = array_pop($stack); // 'b' — correct LIFO, O(1)
// Or SplStack:
$stack = new SplStack();
$stack->push('a');
$stack->push('b');
$top = $stack->pop(); // 'b'