99
1010namespace PHPCfg \ParserHandler \Stmt ;
1111
12+ use PHPCfg \Block ;
1213use PHPCfg \Op ;
14+ use PHPCfg \Operand ;
1315use PHPCfg \ParserHandler ;
1416use PHPCfg \ParserHandler \Stmt ;
1517use PhpParser \Node ;
@@ -22,15 +24,50 @@ public function handleStmt(Node\Stmt $node): void
2224 $ old = $ this ->parser ->currentClass ;
2325 $ this ->parser ->currentClass = $ name ;
2426
25- $ this -> addOp ( new Op \Stmt \Class_ (
27+ $ class = new Op \Stmt \Class_ (
2628 $ name ,
2729 $ node ->flags ,
2830 $ node ->extends ? $ this ->parser ->parseTypeNode ($ node ->extends ) : null ,
2931 $ this ->parser ->parseTypeList (...$ node ->implements ),
3032 $ this ->parser ->parseNodes ($ node ->stmts , $ this ->createBlock ()),
3133 $ this ->parser ->parseAttributeGroups (...$ node ->attrGroups ),
3234 $ this ->mapAttributes ($ node ),
33- ));
35+ );
36+
37+ $ this ->addScope ($ class , $ name );
38+ $ this ->addOp ($ class );
3439 $ this ->parser ->currentClass = $ old ;
3540 }
41+
42+ public static function addScope (Op \Stmt \ClassLike $ class , Op \Type $ name ): void
43+ {
44+ $ toprocess = new \SplObjectStorage ;
45+ $ processed = new \SplObjectStorage ;
46+ $ toprocess ->attach ($ class ->stmts );
47+ while ($ toprocess ->count () > 0 ) {
48+ $ block = $ toprocess ->current ();
49+ $ toprocess ->detach ($ block );
50+ $ processed ->attach ($ block );
51+ foreach ($ block ->children as $ op ) {
52+ $ op ->scope = $ name ;
53+ if ($ op instanceof Op \CallableOp) {
54+ if ($ op ->func ->cfg && !$ processed ->contains ($ op ->func ->cfg )) {
55+ $ toprocess ->attach ($ op ->func ->cfg );
56+ }
57+ }
58+ foreach ($ op ->getSubBlocks () as $ sub ) {
59+ if (is_array ($ sub )) {
60+ foreach ($ sub as $ s ) {
61+ if ($ s && !$ processed ->contains ($ s )) {
62+ $ toprocess ->attach ($ s );
63+ }
64+ }
65+ } elseif ($ sub && !$ processed ->contains ($ sub )) {
66+ $ toprocess ->attach ($ sub );
67+ }
68+ }
69+ }
70+ $ toprocess ->rewind ();
71+ }
72+ }
3673}
0 commit comments