File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed
tests/PHPStan/Rules/Methods Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change @@ -1270,4 +1270,10 @@ public function testDeepDimFetch(): void
12701270 $ this ->analyse ([__DIR__ . '/data/deep-dim-fetch.php ' ], []);
12711271 }
12721272
1273+ #[RequiresPhp('>= 8.0 ' )]
1274+ public function testBug9494 (): void
1275+ {
1276+ $ this ->analyse ([__DIR__ . '/data/bug-9494.php ' ], []);
1277+ }
1278+
12731279}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Bug9494 ;
4+
5+ class Fib
6+ {
7+ /** @var array<int, ?int> 0-indexed memoization */
8+ protected $ mem = [];
9+
10+ public function __construct (public int $ limit ) {
11+ $ this ->mem = array_fill (2 , $ limit , null );
12+ }
13+
14+ /**
15+ * Calculate fib, 1-indexed
16+ */
17+ public function fib (int $ n ): int
18+ {
19+ if ($ n < 1 || $ n > $ this ->limit ) {
20+ throw new \RangeException ();
21+ }
22+
23+ if ($ n == 1 || $ n == 2 ) {
24+ return 1 ;
25+ }
26+
27+ if (is_null ($ this ->mem [$ n - 1 ])) {
28+ $ this ->mem [$ n - 1 ] = $ this ->fib ($ n - 1 ) + $ this ->fib ($ n - 2 );
29+ }
30+
31+ return $ this ->mem [$ n - 1 ]; // Is always an int at this stage
32+ }
33+
34+ /**
35+ * Calculate fib, 0-indexed
36+ */
37+ public function fib0 (int $ n0 ): int
38+ {
39+ if ($ n0 < 0 || $ n0 >= $ this ->limit ) {
40+ throw new \RangeException ();
41+ }
42+
43+ if ($ n0 == 0 || $ n0 == 1 ) {
44+ return 1 ;
45+ }
46+
47+ if (is_null ($ this ->mem [$ n0 ])) {
48+ $ this ->mem [$ n0 ] = $ this ->fib0 ($ n0 - 1 ) + $ this ->fib0 ($ n0 - 2 );
49+ }
50+
51+ return $ this ->mem [$ n0 ];
52+ }
53+ }
You can’t perform that action at this time.
0 commit comments