Skip to content

Commit f2468b5

Browse files
committed
refactor
1 parent b88f135 commit f2468b5

18 files changed

+210
-106
lines changed

src/Command/DiffCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,11 @@ protected function handle(InputInterface $input, OutputInterface $output)
147147
$this->gitlabDomains = array_merge($this->gitlabDomains, $input->getOption('gitlab-domains'));
148148

149149
$urlGenerators = new GeneratorContainer($this->gitlabDomains);
150-
$formatters = new FormatterContainer($output, $urlGenerators);
150+
$formatters = new FormatterContainer($output);
151151
$formatter = $formatters->getFormatter($input->getOption('format'));
152152

153+
$this->packageDiff->setUrlGenerator($urlGenerators);
154+
153155
$prodOperations = new DiffEntries(array());
154156
$devOperations = new DiffEntries(array());
155157

src/Diff/DiffEntry.php

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,25 @@ class DiffEntry
2727
/** @var string */
2828
private $type;
2929

30+
/** @var string|null */
31+
private $compareUrl;
32+
33+
/** @var string|null */
34+
private $projectUrl;
35+
3036
/**
31-
* @param bool $direct
37+
* @param UrlGenerator|null $urlGenerator
38+
* @param bool $direct
3239
*/
33-
public function __construct(OperationInterface $operation, $direct = false)
40+
public function __construct(OperationInterface $operation, $urlGenerator = null, $direct = false)
3441
{
3542
$this->operation = $operation;
3643
$this->direct = $direct;
3744
$this->type = $this->determineType();
45+
46+
if ($urlGenerator instanceof UrlGenerator) {
47+
$this->setUrls($urlGenerator);
48+
}
3849
}
3950

4051
/**
@@ -134,19 +145,35 @@ public function getLicenses()
134145
}
135146

136147
/**
137-
* @return array<string, string|bool|null|string[]>
148+
* @return array{
149+
* name: string,
150+
* direct: bool,
151+
* operation: string,
152+
* version_base: string|null,
153+
* version_target: string|null,
154+
* licenses: string[],
155+
* compare: string|null,
156+
* link: string|null,
157+
* }
138158
*/
139-
public function toArray(UrlGenerator $generator)
159+
public function toArray()
140160
{
141161
$array = $this->toBaseArray();
142-
$array['compare'] = $this->getUrl($generator);
143-
$array['link'] = $this->getProjectUrl($generator);
162+
$array['compare'] = $this->getUrl();
163+
$array['link'] = $this->getProjectUrl();
144164

145165
return $array;
146166
}
147167

148168
/**
149-
* @return array<string, string|bool|null|string[]>
169+
* @return array{
170+
* name: string,
171+
* direct: bool,
172+
* operation: string,
173+
* version_base: string|null,
174+
* version_target: string|null,
175+
* licenses: string[],
176+
* }
150177
*/
151178
public function toBaseArray()
152179
{
@@ -191,30 +218,41 @@ public function toBaseArray()
191218
/**
192219
* @return string|null
193220
*/
194-
public function getUrl(UrlGenerator $generator)
221+
public function getUrl()
195222
{
196-
$operation = $this->getOperation();
197-
198-
if ($operation instanceof UpdateOperation) {
199-
return $generator->getCompareUrl($operation->getInitialPackage(), $operation->getTargetPackage());
200-
}
201-
202-
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
203-
return $generator->getReleaseUrl($operation->getPackage());
204-
}
223+
return $this->compareUrl;
224+
}
205225

206-
return null;
226+
/**
227+
* @return string|null
228+
*/
229+
public function getProjectUrl()
230+
{
231+
return $this->projectUrl;
207232
}
208233

209-
public function getProjectUrl(UrlGenerator $generator)
234+
/**
235+
* @return void
236+
*/
237+
private function setUrls(UrlGenerator $generator)
210238
{
211239
$package = $this->getPackage();
212240

213-
if (!isset($package)) {
214-
return null;
241+
if (isset($package)) {
242+
$this->projectUrl = $generator->getProjectUrl($package);
215243
}
216244

217-
return $generator->getProjectUrl($package);
245+
$operation = $this->getOperation();
246+
247+
if ($operation instanceof UpdateOperation) {
248+
$this->compareUrl = $generator->getCompareUrl($operation->getInitialPackage(), $operation->getTargetPackage());
249+
250+
return;
251+
}
252+
253+
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
254+
$this->compareUrl = $generator->getReleaseUrl($operation->getPackage());
255+
}
218256
}
219257

220258
/**

src/Formatter/AbstractFormatter.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace IonBazan\ComposerDiff\Formatter;
44

55
use IonBazan\ComposerDiff\Diff\DiffEntry;
6-
use IonBazan\ComposerDiff\Url\GeneratorContainer;
76
use Symfony\Component\Console\Output\OutputInterface;
87

98
abstract class AbstractFormatter implements Formatter
@@ -13,15 +12,9 @@ abstract class AbstractFormatter implements Formatter
1312
*/
1413
protected $output;
1514

16-
/**
17-
* @var GeneratorContainer
18-
*/
19-
protected $generators;
20-
21-
public function __construct(OutputInterface $output, GeneratorContainer $generators)
15+
public function __construct(OutputInterface $output)
2216
{
2317
$this->output = $output;
24-
$this->generators = $generators;
2518
}
2619

2720
/**
@@ -35,7 +28,7 @@ protected function getDecoratedPackageName(DiffEntry $entry)
3528
return '';
3629
}
3730

38-
return $this->terminalLink($entry->getProjectUrl($this->generators), $package->getName());
31+
return $this->terminalLink($entry->getProjectUrl(), $package->getName());
3932
}
4033

4134
/**

src/Formatter/FormatterContainer.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace IonBazan\ComposerDiff\Formatter;
44

5-
use IonBazan\ComposerDiff\Url\GeneratorContainer;
65
use Symfony\Component\Console\Output\OutputInterface;
76

87
class FormatterContainer
@@ -14,13 +13,13 @@ class FormatterContainer
1413
*/
1514
private $formatters;
1615

17-
public function __construct(OutputInterface $output, GeneratorContainer $generators)
16+
public function __construct(OutputInterface $output)
1817
{
1918
$this->formatters = array(
20-
'mdtable' => new MarkdownTableFormatter($output, $generators),
21-
'mdlist' => new MarkdownListFormatter($output, $generators),
22-
'github' => new GitHubFormatter($output, $generators),
23-
'json' => new JsonFormatter($output, $generators),
19+
'mdtable' => new MarkdownTableFormatter($output),
20+
'mdlist' => new MarkdownListFormatter($output),
21+
'github' => new GitHubFormatter($output),
22+
'json' => new JsonFormatter($output),
2423
);
2524
}
2625

src/Formatter/GitHubFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private function transformEntries(DiffEntries $entries, $withUrls, $withLicenses
5858
private function transformEntry(DiffEntry $entry, $withUrls, $withLicenses)
5959
{
6060
$operation = $entry->getOperation();
61-
$url = $withUrls ? $entry->getUrl($this->generators) : null;
61+
$url = $withUrls ? $entry->getUrl() : null;
6262
$url = (null !== $url) ? ' '.$url : '';
6363
$licenses = $withLicenses ? implode(', ', $entry->getLicenses()) : '';
6464
$licenses = ('' !== $licenses) ? ' (License: '.$licenses.')' : '';

src/Formatter/JsonFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private function transformEntries(DiffEntries $entries, $withUrls, $withLicenses
4949

5050
/** @var DiffEntry $entry */
5151
foreach ($entries as $entry) {
52-
$row = $entry->toArray($this->generators);
52+
$row = $entry->toArray();
5353

5454
if (!$withUrls) {
5555
unset($row['compare'], $row['link']);

src/Formatter/MarkdownListFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicen
4747
*/
4848
private function getRow(DiffEntry $entry, $withUrls, $withLicenses)
4949
{
50-
$url = $withUrls ? $this->formatUrl($entry->getUrl($this->generators), 'Compare') : null;
50+
$url = $withUrls ? $this->formatUrl($entry->getUrl(), 'Compare') : null;
5151
$url = (null !== $url && '' !== $url) ? ' '.$url : '';
5252
$licenses = $withLicenses ? implode(', ', $entry->getLicenses()) : '';
5353
$licenses = ('' !== $licenses) ? ' (License: '.$licenses.')' : '';
5454
$operation = $entry->getOperation();
5555
$package = $entry->getPackage();
5656

5757
$packageName = $package ? $package->getName() : null;
58-
$packageUrl = $withUrls ? $this->formatUrl($entry->getProjectUrl($this->generators), $packageName) : $packageName;
58+
$packageUrl = $withUrls ? $this->formatUrl($entry->getProjectUrl(), $packageName) : $packageName;
5959

6060
if ($operation instanceof InstallOperation) {
6161
return sprintf(

src/Formatter/MarkdownTableFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicen
3535
$row = $this->getTableRow($entry, $withUrls);
3636

3737
if ($withUrls) {
38-
$row[] = $this->formatUrl($entry->getUrl($this->generators), 'Compare');
38+
$row[] = $this->formatUrl($entry->getUrl(), 'Compare');
3939
}
4040

4141
if ($withLicenses) {
@@ -69,7 +69,7 @@ private function getTableRow(DiffEntry $entry, $withUrls)
6969
{
7070
$operation = $entry->getOperation();
7171
$packageName = $this->getDecoratedPackageName($entry);
72-
$packageUrl = $withUrls ? $this->formatUrl($entry->getProjectUrl($this->generators), $packageName) : $packageName;
72+
$packageUrl = $withUrls ? $this->formatUrl($entry->getProjectUrl(), $packageName) : $packageName;
7373

7474
if ($operation instanceof InstallOperation) {
7575
return array(

src/PackageDiff.php

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace IonBazan\ComposerDiff;
44

55
use Composer\DependencyResolver\Operation\InstallOperation;
6-
use Composer\DependencyResolver\Operation\OperationInterface;
76
use Composer\DependencyResolver\Operation\UninstallOperation;
87
use Composer\DependencyResolver\Operation\UpdateOperation;
98
use Composer\Package\AliasPackage;
@@ -13,6 +12,8 @@
1312
use Composer\Repository\RepositoryInterface;
1413
use IonBazan\ComposerDiff\Diff\DiffEntries;
1514
use IonBazan\ComposerDiff\Diff\DiffEntry;
15+
use IonBazan\ComposerDiff\Url\GeneratorContainer;
16+
use IonBazan\ComposerDiff\Url\UrlGenerator;
1617

1718
class PackageDiff
1819
{
@@ -21,13 +22,50 @@ class PackageDiff
2122
const EXTENSION_JSON = '.json';
2223
const GIT_SEPARATOR = ':';
2324

25+
/** @var UrlGenerator */
26+
protected $urlGenerator;
27+
28+
public function __construct()
29+
{
30+
$this->urlGenerator = new GeneratorContainer();
31+
}
32+
33+
/**
34+
* @return void
35+
*/
36+
public function setUrlGenerator(UrlGenerator $urlGenerator)
37+
{
38+
$this->urlGenerator = $urlGenerator;
39+
}
40+
2441
/**
2542
* @param string[] $directPackages
2643
* @param bool $onlyDirect
2744
*
2845
* @return DiffEntries
2946
*/
3047
public function getDiff(RepositoryInterface $oldPackages, RepositoryInterface $targetPackages, array $directPackages = array(), $onlyDirect = false)
48+
{
49+
$entries = array();
50+
51+
foreach ($this->getOperations($oldPackages, $targetPackages) as $operation) {
52+
$package = $operation instanceof UpdateOperation ? $operation->getTargetPackage() : $operation->getPackage();
53+
$direct = in_array($package->getName(), $directPackages, true);
54+
55+
if ($onlyDirect && !$direct) {
56+
continue;
57+
}
58+
59+
$entries[] = new DiffEntry($operation, $this->urlGenerator, $direct);
60+
}
61+
62+
return new DiffEntries($entries);
63+
}
64+
65+
/**
66+
* @return array<InstallOperation|UpdateOperation|UninstallOperation>
67+
*/
68+
public function getOperations(RepositoryInterface $oldPackages, RepositoryInterface $targetPackages)
3169
{
3270
$operations = array();
3371

@@ -65,19 +103,7 @@ public function getDiff(RepositoryInterface $oldPackages, RepositoryInterface $t
65103
}
66104
}
67105

68-
$entries = array_map(function (OperationInterface $operation) use ($directPackages) {
69-
$package = $operation instanceof UpdateOperation ? $operation->getTargetPackage() : $operation->getPackage();
70-
71-
return new DiffEntry($operation, in_array($package->getName(), $directPackages, true));
72-
}, $operations);
73-
74-
if ($onlyDirect) {
75-
$entries = array_values(array_filter($entries, function (DiffEntry $entry) {
76-
return $entry->isDirect();
77-
}));
78-
}
79-
80-
return new DiffEntries($entries);
106+
return $operations;
81107
}
82108

83109
/**

0 commit comments

Comments
 (0)