Skip to content

Commit c78e1d7

Browse files
authored
Merge pull request #6 from smeghead/feature/change-variable-hard-usage-algorithm
feat: The base of deviation of local variable abuse has been changed …
2 parents 4e6daf7 + 1891b7f commit c78e1d7

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
### Features
4+
5+
* The base of deviation of local variable abuse has been changed from the average number of rows to the first number of rows.
6+
7+
38
## v0.0.2 (2025-03-19)
49

510
### Features

README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ $ vendor/bin/php-variable-hard-usage somewhere/your-php-file.php
8484
}
8585
```
8686

87-
8887
## How to calculate VariableHardUsage
8988

9089
VariableHardUsage is an index used to evaluate the frequency of use and scope of local variables within a function. This measure is calculated based on the variance of the line number of references to the same variable and the frequency with which the variable is assigned.
@@ -95,31 +94,30 @@ VariableHardUsage is an index used to evaluate the frequency of use and scope of
9594

9695
* For each variable used in the function, retrieve all line numbers where the variable is referenced.
9796

98-
2. Calculate the average of the line numbers.
99-
100-
* Calculates the average of the line numbers obtained. This is obtained by dividing the sum of the line numbers by the number of references.
97+
2. Calculate deviation based on first occurrence.
10198

102-
3. Calculate VariableHardUsage.
99+
* For each reference, computes the difference between the line number and the line number of the first occurrence of the variable.
103100

104-
* Calculates the absolute difference between the line number and the average line number for each reference.
105-
* If a variable is assigned, the difference is multiplied by a factor (2 by default).
106-
* Sum all these values to obtain VariableHardUsage.
101+
3. Applying coefficients by assignment.
102+
* When a variable is assigned, the difference is multiplied by a coefficient (2 by default). This is to account for the effect of the assignment on the frequency of use of the variable.
103+
* Calculation of VariableHardUsage:.
104+
* The VariableHardUsage is obtained by summing all these deviation values.
107105

108106
### Example
109107

110-
For example, suppose there are three reference points in a function, each with line numbers 10, 20, and 30, where some assignments are made and some are not made. In this case, the average row number is 20.
108+
Suppose, for example, that there are three reference points in a function, each with line numbers 10, 20, and 30, and that some assignments are made and some are not made. In this case, the line number of the first occurrence is 10.
111109

112-
* Reference A: Row 10, with assignment
113-
* Reference B: Row 20, no assignment
114-
* Reference C: Row 30, with assignment
110+
* Reference A: line 10, with assignment
111+
* Reference B: line 20, no assignment
112+
* Reference C: line 30, with assignment
115113

116114
In this case, VariableHardUsage is calculated as follows
117115

118-
* Reference A: |10 - 20| * 2 = 20
119-
* Reference B: |20 - 20| * 1 = 0
120-
* Reference C: |30 - 20| * 2 = 20
116+
* Reference A: (10 - 10) * 2 = 0
117+
* Reference B: (20 - 10) * 1 = 10
118+
* Reference C: (30 - 10) * 2 = 40
121119

122-
Summing these, VariableHardUsage is 20 + 0 + 20 = 40.
120+
Summing these, VariableHardUsage is 0 + 10 + 40 = 50.
123121

124122
VariableHardUsage is thus calculated as a measure of the frequency of use and scope of a variable. This metric can be used to quantitatively evaluate the usage of local variables within a function and help improve code readability and maintainability.
125123

src/Analyze/VariableAnalyzer.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ private function analyzeFunction(Func $function): Scope
5050
*/
5151
private function calcVariableHardUsage(array $vars): int
5252
{
53-
$lineNumbers = array_map(fn($var) => $var->lineNumber, $vars);
54-
$avarageLinuNumber = intval(array_sum($lineNumbers) / count($lineNumbers));
55-
$variableHardUsage = array_sum(array_map(fn(VarReference $var) => abs($var->lineNumber - $avarageLinuNumber) * ($var->assigned ? self::ASSIGNED_VARIABLE_COEFFICIENT : 1), $vars));
56-
return $variableHardUsage;
53+
$firstLineNumber = $vars[0]->lineNumber;
54+
return array_sum(array_map(fn(VarReference $var) => ($var->lineNumber - $firstLineNumber) * ($var->assigned ? self::ASSIGNED_VARIABLE_COEFFICIENT : 1), $vars));
5755
}
5856
}

test/VariableAnalizerTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testAnalyzeFunctionSimple(): void
2222

2323
$this->assertCount(1, $scopes);
2424
$this->assertSame('testFunction', $scopes[0]->name);
25-
$this->assertSame(2, $scopes[0]->getAnalyzedVariables()[0]->variableHardUsage);
25+
$this->assertSame(3, $scopes[0]->getAnalyzedVariables()[0]->variableHardUsage, '0 + 1 + 2');
2626
}
2727

2828
public function testAnalyzeFunctionLong(): void
@@ -38,9 +38,7 @@ public function testAnalyzeFunctionLong(): void
3838

3939
$this->assertCount(1, $scopes);
4040
$this->assertSame('testFunction', $scopes[0]->name);
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);
41+
$this->assertSame(100, $scopes[0]->getAnalyzedVariables()[0]->variableHardUsage, '(1 - 1) + (2 - 1) + (100 - 1)');
4442
}
4543

4644
public function testAnalyzeFunctionLongAssignedVariable(): void
@@ -56,8 +54,22 @@ public function testAnalyzeFunctionLongAssignedVariable(): void
5654

5755
$this->assertCount(1, $scopes);
5856
$this->assertSame('testFunction', $scopes[0]->name);
59-
// (1 + 2 + 100) / 3 = 34
60-
// abs(34 - 1) * 2 + abs(34 - 2) + abs(34 - 100) = 66 + 32 + 66 = 164
61-
$this->assertSame(164, $scopes[0]->getAnalyzedVariables()[0]->variableHardUsage);
57+
$this->assertSame(100, $scopes[0]->getAnalyzedVariables()[0]->variableHardUsage, '(1 - 1) * 2 + (2 - 1) + (100 - 1)');
58+
}
59+
60+
public function testAnalyzeFunctionLongMultipleAssignedVariable(): void
61+
{
62+
$func = new Func(null, 'testFunction');
63+
$func->addVariable(new VarReference('a', 1, true));
64+
$func->addVariable(new VarReference('a', 2));
65+
$func->addVariable(new VarReference('a', 100, true));
66+
67+
$sut = new VariableAnalyzer([$func]);
68+
$result = $sut->analyze();
69+
$scopes = $result->scopes;
70+
71+
$this->assertCount(1, $scopes);
72+
$this->assertSame('testFunction', $scopes[0]->name);
73+
$this->assertSame(199, $scopes[0]->getAnalyzedVariables()[0]->variableHardUsage, '(1 - 1) * 2 + (2 - 1) + (100 - 1) * 2');
6274
}
6375
}

0 commit comments

Comments
 (0)