1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ use PHPUnit \Framework \TestCase ;
6+ use Smeghead \PhpVariableHardUsage \Analyze \VariableAnalyzer ;
7+ use Smeghead \PhpVariableHardUsage \Parse \Func ;
8+ use Smeghead \PhpVariableHardUsage \Parse \VarReference ;
9+
10+ class VariableAnalizerTest extends TestCase
11+ {
12+ public function testAnalyzeFunctionSimple (): void
13+ {
14+ $ func = new Func ('testFunction ' );
15+ $ func ->addVariable (new VarReference ('a ' , 1 ));
16+ $ func ->addVariable (new VarReference ('a ' , 2 ));
17+ $ func ->addVariable (new VarReference ('a ' , 3 ));
18+
19+ $ sut = new VariableAnalyzer ([$ func ]);
20+ $ result = $ sut ->analyze ();
21+ $ scopes = $ result ->scopes ;
22+
23+ $ this ->assertCount (1 , $ scopes );
24+ $ this ->assertSame ('testFunction ' , $ scopes [0 ]->getName ());
25+ $ this ->assertSame (2 , $ scopes [0 ]->getAnalyzedVariables ()[0 ]->variableHardUsage );
26+ }
27+
28+ public function testAnalyzeFunctionLong (): void
29+ {
30+ $ func = new Func ('testFunction ' );
31+ $ func ->addVariable (new VarReference ('a ' , 1 ));
32+ $ func ->addVariable (new VarReference ('a ' , 2 ));
33+ $ func ->addVariable (new VarReference ('a ' , 100 ));
34+
35+ $ sut = new VariableAnalyzer ([$ func ]);
36+ $ result = $ sut ->analyze ();
37+ $ scopes = $ result ->scopes ;
38+
39+ $ this ->assertCount (1 , $ scopes );
40+ $ this ->assertSame ('testFunction ' , $ scopes [0 ]->getName ());
41+ // (1 + 2 + 100) / 3 = 34
42+ // abs(34 - 1) + abs(34 - 2) + abs(34 - 100) = 33 + 32 + 66 = 131
43+ $ this ->assertSame (131 , $ scopes [0 ]->getAnalyzedVariables ()[0 ]->variableHardUsage );
44+ }
45+ }
0 commit comments