Skip to content

Commit 1238562

Browse files
committed
refactor
1 parent b88f135 commit 1238562

18 files changed

+274
-212
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: 81 additions & 56 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
/**
@@ -102,7 +113,7 @@ public function isChange()
102113
}
103114

104115
/**
105-
* @return PackageInterface|null
116+
* @return PackageInterface
106117
*/
107118
public function getPackage()
108119
{
@@ -116,7 +127,7 @@ public function getPackage()
116127
return $operation->getPackage();
117128
}
118129

119-
return null;
130+
throw new \InvalidArgumentException('Invalid operation');
120131
}
121132

122133
/**
@@ -134,87 +145,101 @@ 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
{
141-
$array = $this->toBaseArray();
142-
$array['compare'] = $this->getUrl($generator);
143-
$array['link'] = $this->getProjectUrl($generator);
144-
145-
return $array;
161+
return array(
162+
'name' => $this->getPackage()->getName(),
163+
'direct' => $this->isDirect(),
164+
'operation' => $this->getType(),
165+
'version_base' => $this->getBaseVersion(),
166+
'version_target' => $this->getTargetVersion(),
167+
'licenses' => $this->getLicenses(),
168+
'compare' => $this->getUrl(),
169+
'link' => $this->getProjectUrl(),
170+
);
146171
}
147172

148173
/**
149-
* @return array<string, string|bool|null|string[]>
174+
* @return string|null
150175
*/
151-
public function toBaseArray()
176+
public function getBaseVersion()
152177
{
153-
$operation = $this->getOperation();
154-
155-
if ($operation instanceof InstallOperation) {
156-
return array(
157-
'name' => $operation->getPackage()->getName(),
158-
'direct' => $this->isDirect(),
159-
'operation' => $this->getType(),
160-
'version_base' => null,
161-
'version_target' => $operation->getPackage()->getFullPrettyVersion(),
162-
'licenses' => $this->getLicenses(),
163-
);
164-
}
165-
166-
if ($operation instanceof UpdateOperation) {
167-
return array(
168-
'name' => $operation->getInitialPackage()->getName(),
169-
'direct' => $this->isDirect(),
170-
'operation' => $this->getType(),
171-
'version_base' => $operation->getInitialPackage()->getFullPrettyVersion(),
172-
'version_target' => $operation->getTargetPackage()->getFullPrettyVersion(),
173-
'licenses' => $this->getLicenses(),
174-
);
178+
if ($this->operation instanceof UpdateOperation) {
179+
return $this->operation->getInitialPackage()->getFullPrettyVersion();
175180
}
176181

177-
if ($operation instanceof UninstallOperation) {
178-
return array(
179-
'name' => $operation->getPackage()->getName(),
180-
'direct' => $this->isDirect(),
181-
'operation' => $this->getType(),
182-
'version_base' => $operation->getPackage()->getFullPrettyVersion(),
183-
'version_target' => null,
184-
'licenses' => $this->getLicenses(),
185-
);
182+
if ($this->operation instanceof UninstallOperation) {
183+
return $this->operation->getPackage()->getFullPrettyVersion();
186184
}
187185

188-
throw new \InvalidArgumentException('Invalid operation');
186+
return null;
189187
}
190188

191189
/**
192190
* @return string|null
193191
*/
194-
public function getUrl(UrlGenerator $generator)
192+
public function getTargetVersion()
195193
{
196-
$operation = $this->getOperation();
197-
198-
if ($operation instanceof UpdateOperation) {
199-
return $generator->getCompareUrl($operation->getInitialPackage(), $operation->getTargetPackage());
194+
if ($this->operation instanceof UpdateOperation) {
195+
return $this->operation->getTargetPackage()->getFullPrettyVersion();
200196
}
201197

202-
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
203-
return $generator->getReleaseUrl($operation->getPackage());
198+
if ($this->operation instanceof InstallOperation) {
199+
return $this->operation->getPackage()->getFullPrettyVersion();
204200
}
205201

206202
return null;
207203
}
208204

209-
public function getProjectUrl(UrlGenerator $generator)
205+
/**
206+
* @return string|null
207+
*/
208+
public function getUrl()
209+
{
210+
return $this->compareUrl;
211+
}
212+
213+
/**
214+
* @return string|null
215+
*/
216+
public function getProjectUrl()
217+
{
218+
return $this->projectUrl;
219+
}
220+
221+
/**
222+
* @return void
223+
*/
224+
private function setUrls(UrlGenerator $generator)
210225
{
211226
$package = $this->getPackage();
212227

213-
if (!isset($package)) {
214-
return null;
228+
if (isset($package)) {
229+
$this->projectUrl = $generator->getProjectUrl($package);
215230
}
216231

217-
return $generator->getProjectUrl($package);
232+
$operation = $this->getOperation();
233+
234+
if ($operation instanceof UpdateOperation) {
235+
$this->compareUrl = $generator->getCompareUrl($operation->getInitialPackage(), $operation->getTargetPackage());
236+
237+
return;
238+
}
239+
240+
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
241+
$this->compareUrl = $generator->getReleaseUrl($operation->getPackage());
242+
}
218243
}
219244

220245
/**

src/Formatter/AbstractFormatter.php

Lines changed: 2 additions & 15 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,29 +12,17 @@ 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
/**
2821
* @return string
2922
*/
3023
protected function getDecoratedPackageName(DiffEntry $entry)
3124
{
32-
$package = $entry->getPackage();
33-
34-
if (null === $package) {
35-
return '';
36-
}
37-
38-
return $this->terminalLink($entry->getProjectUrl($this->generators), $package->getName());
25+
return $this->terminalLink($entry->getProjectUrl(), $entry->getPackage()->getName());
3926
}
4027

4128
/**

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: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace IonBazan\ComposerDiff\Formatter;
44

5-
use Composer\DependencyResolver\Operation\InstallOperation;
6-
use Composer\DependencyResolver\Operation\UninstallOperation;
7-
use Composer\DependencyResolver\Operation\UpdateOperation;
85
use IonBazan\ComposerDiff\Diff\DiffEntries;
96
use IonBazan\ComposerDiff\Diff\DiffEntry;
107

@@ -57,44 +54,39 @@ private function transformEntries(DiffEntries $entries, $withUrls, $withLicenses
5754
*/
5855
private function transformEntry(DiffEntry $entry, $withUrls, $withLicenses)
5956
{
60-
$operation = $entry->getOperation();
61-
$url = $withUrls ? $entry->getUrl($this->generators) : null;
57+
$url = $withUrls ? $entry->getUrl() : null;
6258
$url = (null !== $url) ? ' '.$url : '';
6359
$licenses = $withLicenses ? implode(', ', $entry->getLicenses()) : '';
6460
$licenses = ('' !== $licenses) ? ' (License: '.$licenses.')' : '';
6561

66-
if ($operation instanceof InstallOperation) {
62+
if ($entry->isInstall()) {
6763
return sprintf(
6864
' - Install %s (%s)%s%s',
69-
$operation->getPackage()->getName(),
70-
$operation->getPackage()->getFullPrettyVersion(),
65+
$entry->getPackage()->getName(),
66+
$entry->getTargetVersion(),
7167
$url,
7268
$licenses
7369
);
7470
}
7571

76-
if ($operation instanceof UpdateOperation) {
77-
return sprintf(
78-
' - %s %s (%s => %s)%s%s',
79-
ucfirst($entry->getType()),
80-
$operation->getInitialPackage()->getName(),
81-
$operation->getInitialPackage()->getFullPrettyVersion(),
82-
$operation->getTargetPackage()->getFullPrettyVersion(),
83-
$url,
84-
$licenses
85-
);
86-
}
87-
88-
if ($operation instanceof UninstallOperation) {
72+
if ($entry->isRemove()) {
8973
return sprintf(
9074
' - Uninstall %s (%s)%s%s',
91-
$operation->getPackage()->getName(),
92-
$operation->getPackage()->getFullPrettyVersion(),
75+
$entry->getPackage()->getName(),
76+
$entry->getBaseVersion(),
9377
$url,
9478
$licenses
9579
);
9680
}
9781

98-
throw new \InvalidArgumentException('Invalid operation');
82+
return sprintf(
83+
' - %s %s (%s => %s)%s%s',
84+
ucfirst($entry->getType()),
85+
$entry->getPackage()->getName(),
86+
$entry->getBaseVersion(),
87+
$entry->getTargetVersion(),
88+
$url,
89+
$licenses
90+
);
9991
}
10092
}

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']);

0 commit comments

Comments
 (0)