File tree Expand file tree Collapse file tree 2 files changed +102
-0
lines changed
tests/PHPStan/Analyser/nsrt Expand file tree Collapse file tree 2 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace Bug13385c ;
4+
5+ use function PHPStan \Testing \assertType ;
6+
7+ interface Operator {
8+ public function priority (): int ;
9+ public function calculate (int $ a , int $ b ): int ;
10+ }
11+
12+ class HelloWorld
13+ {
14+ /**
15+ * @param list<Operator|int> $children
16+ */
17+ public function calculate (array $ children ): int {
18+ $ operands = [];
19+ $ operators = [];
20+
21+ foreach ($ children as $ child ) {
22+ if ($ child instanceof Operator) {
23+ for (;$ operators !== [] && end ($ operators )->priority () >= $ child ->priority ();) {
24+ $ op = array_pop ($ operators );
25+ $ left = array_pop ($ operands ) ?? 0 ;
26+ $ right = array_pop ($ operands ) ?? 0 ;
27+
28+ assert (is_int ($ left ));
29+ assert (is_int ($ right ));
30+
31+ $ value = $ op ->calculate ($ left , $ right );
32+
33+ assertType (Operator::class, $ op );
34+ assertType ('int ' , $ left );
35+ assertType ('int ' , $ right );
36+ assertType ('int ' , $ value );
37+
38+ $ operands [] = $ value ;
39+
40+ assertType ('non-empty-list<int> ' , $ operands );
41+ }
42+
43+ $ operators [] = $ child ;
44+ } else {
45+ $ operands [] = $ child ;
46+ }
47+ }
48+
49+ return count ($ operands ) === 1 ? reset ($ operands ) : 0 ;
50+ }
51+ }
Original file line number Diff line number Diff line change 1+ <?php declare (strict_types = 1 );
2+
3+ namespace Bug13385d ;
4+
5+ use function PHPStan \Testing \assertType ;
6+
7+ interface Operator {
8+ public function priority (): int ;
9+ public function calculate (int $ a , int $ b ): int ;
10+ }
11+
12+ class HelloWorld
13+ {
14+ /**
15+ * @param list<Operator|int> $children
16+ */
17+ public function calculate (array $ children ): int {
18+ $ operands = [];
19+ $ operators = [];
20+
21+ for ($ i = 0 ; $ i < count ($ children ); $ i ++) {
22+ if ($ children [$ i ] instanceof Operator) {
23+ while ($ operators !== [] && end ($ operators )->priority () >= $ children [$ i ]->priority ()) {
24+ $ op = array_pop ($ operators );
25+ $ left = array_pop ($ operands ) ?? 0 ;
26+ $ right = array_pop ($ operands ) ?? 0 ;
27+
28+ assert (is_int ($ left ));
29+ assert (is_int ($ right ));
30+
31+ $ value = $ op ->calculate ($ left , $ right );
32+
33+ assertType (Operator::class, $ op );
34+ assertType ('int ' , $ left );
35+ assertType ('int ' , $ right );
36+ assertType ('int ' , $ value );
37+
38+ $ operands [] = $ value ;
39+
40+ assertType ('non-empty-list<int> ' , $ operands );
41+ }
42+
43+ $ operators [] = $ children [$ i ];
44+ } else {
45+ $ operands [] = $ children [$ i ];
46+ }
47+ }
48+
49+ return count ($ operands ) === 1 ? reset ($ operands ) : 0 ;
50+ }
51+ }
You can’t perform that action at this time.
0 commit comments