Skip to content

Commit 08aa3dc

Browse files
Refacto ComponentMapper
1 parent 8ff6a33 commit 08aa3dc

File tree

5 files changed

+87
-41
lines changed

5 files changed

+87
-41
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Presenter\Analyze\Component\Shared;
4+
5+
use App\Application\Analyze\AnalyzeMetric;
6+
7+
class Collector
8+
{
9+
public string $name;
10+
public float $totalInstability = 0;
11+
public int $countAbstractions = 0;
12+
public int $countClasses = 0;
13+
public array $dependencies = [];
14+
15+
public function setName(string $name): void
16+
{
17+
$this->name = $name;
18+
}
19+
20+
public function collect(AnalyzeMetric $metric): void
21+
{
22+
$this->totalInstability += $metric->instability();
23+
24+
if ($metric->abstract()) {
25+
$this->countAbstractions++;
26+
}
27+
28+
$this->countClasses++;
29+
}
30+
31+
public function addDependency(string $dependency): void
32+
{
33+
$this->dependencies[] = $dependency;
34+
}
35+
36+
public function hasDependency(string $dependency): bool
37+
{
38+
return in_array($dependency, $this->dependencies, true);
39+
}
40+
}
41+

app/Presenter/Analyze/Component/Shared/Component.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ class Component implements Networkable
99
public function __construct(
1010
public readonly string $name,
1111
public readonly int $countClasses,
12-
public readonly float $countAbstractions,
13-
public readonly float $averageAbstractness,
14-
public readonly float $averageInstability,
15-
public readonly array $innerDependencies,
12+
public readonly int $countAbstractions,
13+
public readonly float $totalInstability,
14+
public readonly array $dependencies,
1615
) {}
1716

1817
public function name(): string
@@ -32,16 +31,16 @@ public function countAbstractions(): int
3231

3332
public function abstractness(): float
3433
{
35-
return number_format($this->averageAbstractness, 2);
34+
return number_format($this->countAbstractions / $this->countClasses, 2);
3635
}
3736

3837
public function instability(): float
3938
{
40-
return number_format($this->averageInstability, 2);
39+
return number_format($this->totalInstability / $this->countClasses, 2);
4140
}
4241

4342
public function dependencies(): array
4443
{
45-
return $this->innerDependencies;
44+
return $this->dependencies;
4645
}
4746
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Presenter\Analyze\Component\Shared;
4+
5+
class ComponentFactory
6+
{
7+
public function make(Collector $collector): Component
8+
{
9+
return new Component(
10+
name: $collector->name,
11+
countClasses: $collector->countClasses,
12+
countAbstractions: $collector->countAbstractions,
13+
totalInstability: $collector->totalInstability,
14+
dependencies: $collector->dependencies,
15+
);
16+
}
17+
}

app/Presenter/Analyze/Component/Shared/ComponentMapper.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,28 @@
66

77
class ComponentMapper
88
{
9+
public function __construct(
10+
private readonly ComponentFactory $componentFactory,
11+
) {}
12+
913
/**
10-
* @todo : its work but it's not efficient
14+
* @var array<string, array<AnalyzeMetric>> $metrics
1115
*/
1216
public function from(array $metrics): array
1317
{
1418
$components = array_keys($metrics);
1519

16-
$tmp = [];
20+
$items = [];
1721

1822
foreach ($metrics as $component => $componentMetrics) {
1923

20-
$instability = 0;
21-
$abstract = 0;
24+
$collector = new Collector();
2225

23-
$innerDependencies = [];
26+
$collector->setName($component);
2427

2528
foreach ($componentMetrics as $metric) {
2629

27-
$instability += $metric->instability();
28-
$abstract += ($metric->abstract()) ? 1 : 0;
30+
$collector->collect($metric);
2931

3032
foreach ($metric->dependencies() as $dependency) {
3133

@@ -37,24 +39,17 @@ public function from(array $metrics): array
3739
continue;
3840
}
3941

40-
if (! in_array($otherComponent, $innerDependencies)) {
41-
$innerDependencies[] = $otherComponent;
42+
if (! $collector->hasDependency($otherComponent)) {
43+
$collector->addDependency($otherComponent);
4244
}
4345
}
4446
}
4547
}
4648
}
4749

48-
$tmp[] = new Component(
49-
$component,
50-
count($componentMetrics),
51-
$abstract,
52-
$abstract / (count($componentMetrics) ?: 1),
53-
$instability / (count($componentMetrics) ?: 1),
54-
$innerDependencies,
55-
);
50+
$items[] = $this->componentFactory->make($collector);
5651
}
5752

58-
return $tmp;
53+
return $items;
5954
}
6055
}

tests/Unit/Presenter/ComponentMapperTest.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
use App\Presenter\Analyze\Component\Shared\Component;
44
use App\Presenter\Analyze\Component\Shared\ComponentMapper;
5+
use App\Presenter\Analyze\Component\Shared\ComponentFactory;
6+
7+
beforeEach(function () {
8+
$this->componentMapper = new ComponentMapper(new ComponentFactory());
9+
});
510

611
it('should map metrics to components', function () {
712

8-
$mapper = new ComponentMapper();
9-
1013
$metrics = [
1114
'A' => [
1215
$this->oneAnalyzeMetric()->withName('A\Class1')->build(),
@@ -15,7 +18,7 @@
1518
]
1619
];
1720

18-
$components = $mapper->from($metrics);
21+
$components = $this->componentMapper->from($metrics);
1922

2023
expect($components)->toHaveCount(1);
2124
expect($components[0])->toBeInstanceOf(Component::class);
@@ -24,8 +27,6 @@
2427

2528
it('should map metrics to components with dependencies', function () {
2629

27-
$mapper = new ComponentMapper();
28-
2930
$metrics = [
3031
'A' => [
3132
$this->oneAnalyzeMetric()->withName('A\Class1')->withDependencies(['B\Class2'])->build(),
@@ -35,17 +36,14 @@
3536
]
3637
];
3738

38-
$components = $mapper->from($metrics);
39+
$components = $this->componentMapper->from($metrics);
3940

4041
expect($components)->toHaveCount(2);
4142
expect($components[0]->dependencies())->toBe(['B']);
42-
4343
});
4444

4545
it('should not keep dependencies from unwanted namespaces', function () {
4646

47-
$mapper = new ComponentMapper();
48-
4947
$metrics = [
5048
'A' => [
5149
/**
@@ -58,16 +56,14 @@
5856
]
5957
];
6058

61-
$components = $mapper->from($metrics);
59+
$components = $this->componentMapper->from($metrics);
6260

6361
expect($components)->toHaveCount(2);
6462
expect($components[0]->dependencies())->toBe([]);
6563
});
6664

6765
it('should calculate the average abstractness', function () {
6866

69-
$mapper = new ComponentMapper();
70-
7167
$metrics = [
7268
'A' => [
7369
$this->oneAnalyzeMetric()->build(),
@@ -77,7 +73,7 @@
7773
],
7874
];
7975

80-
$components = $mapper->from($metrics);
76+
$components = $this->componentMapper->from($metrics);
8177

8278
expect($components[0]->countClasses())->toBe(4);
8379
expect($components[0]->countAbstractions())->toBe(3);
@@ -86,8 +82,6 @@
8682

8783
it('should calculate the average instability', function () {
8884

89-
$mapper = new ComponentMapper();
90-
9185
$metrics = [
9286
'A' => [
9387
$this->oneAnalyzeMetric()->withInstability(0.3)->build(),
@@ -96,7 +90,7 @@
9690
],
9791
];
9892

99-
$components = $mapper->from($metrics);
93+
$components = $this->componentMapper->from($metrics);
10094

10195
expect($components[0]->instability())->toBe(0.67);
10296
});

0 commit comments

Comments
 (0)