Skip to content

Commit b88f135

Browse files
committed
update
1 parent e58fd74 commit b88f135

13 files changed

+257
-305
lines changed

src/Diff/DiffEntry.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Composer\DependencyResolver\Operation\OperationInterface;
77
use Composer\DependencyResolver\Operation\UninstallOperation;
88
use Composer\DependencyResolver\Operation\UpdateOperation;
9+
use Composer\Package\CompletePackageInterface;
910
use Composer\Package\PackageInterface;
11+
use IonBazan\ComposerDiff\Url\UrlGenerator;
1012

1113
class DiffEntry
1214
{
@@ -117,6 +119,104 @@ public function getPackage()
117119
return null;
118120
}
119121

122+
/**
123+
* @return string[]
124+
*/
125+
public function getLicenses()
126+
{
127+
$package = $this->getPackage();
128+
129+
if (!$package instanceof CompletePackageInterface) {
130+
return array();
131+
}
132+
133+
return $package->getLicense();
134+
}
135+
136+
/**
137+
* @return array<string, string|bool|null|string[]>
138+
*/
139+
public function toArray(UrlGenerator $generator)
140+
{
141+
$array = $this->toBaseArray();
142+
$array['compare'] = $this->getUrl($generator);
143+
$array['link'] = $this->getProjectUrl($generator);
144+
145+
return $array;
146+
}
147+
148+
/**
149+
* @return array<string, string|bool|null|string[]>
150+
*/
151+
public function toBaseArray()
152+
{
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+
);
175+
}
176+
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+
);
186+
}
187+
188+
throw new \InvalidArgumentException('Invalid operation');
189+
}
190+
191+
/**
192+
* @return string|null
193+
*/
194+
public function getUrl(UrlGenerator $generator)
195+
{
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+
}
205+
206+
return null;
207+
}
208+
209+
public function getProjectUrl(UrlGenerator $generator)
210+
{
211+
$package = $this->getPackage();
212+
213+
if (!isset($package)) {
214+
return null;
215+
}
216+
217+
return $generator->getProjectUrl($package);
218+
}
219+
120220
/**
121221
* @return string
122222
*/

src/Formatter/AbstractFormatter.php

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
namespace IonBazan\ComposerDiff\Formatter;
44

5-
use Composer\DependencyResolver\Operation\InstallOperation;
6-
use Composer\DependencyResolver\Operation\OperationInterface;
7-
use Composer\DependencyResolver\Operation\UninstallOperation;
8-
use Composer\DependencyResolver\Operation\UpdateOperation;
9-
use Composer\Package\CompletePackageInterface;
105
use IonBazan\ComposerDiff\Diff\DiffEntry;
116
use IonBazan\ComposerDiff\Url\GeneratorContainer;
127
use Symfony\Component\Console\Output\OutputInterface;
@@ -29,62 +24,6 @@ public function __construct(OutputInterface $output, GeneratorContainer $generat
2924
$this->generators = $generators;
3025
}
3126

32-
/**
33-
* @return string|null
34-
*/
35-
public function getUrl(DiffEntry $entry)
36-
{
37-
$operation = $entry->getOperation();
38-
39-
if ($operation instanceof UpdateOperation) {
40-
return $this->generators->getCompareUrl($operation->getInitialPackage(), $operation->getTargetPackage());
41-
}
42-
43-
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
44-
return $this->generators->getReleaseUrl($operation->getPackage());
45-
}
46-
47-
return null;
48-
}
49-
50-
/**
51-
* @return string|null
52-
*/
53-
public function getLicenses(DiffEntry $entry)
54-
{
55-
if (!$entry->getPackage() instanceof CompletePackageInterface) {
56-
return null;
57-
}
58-
59-
$licenses = $entry->getPackage()->getLicense();
60-
61-
if (empty($licenses)) {
62-
return null;
63-
}
64-
65-
return implode(', ', $licenses);
66-
}
67-
68-
/**
69-
* @return string|null
70-
*/
71-
public function getProjectUrl(OperationInterface $operation)
72-
{
73-
if ($operation instanceof UpdateOperation) {
74-
$package = $operation->getInitialPackage();
75-
}
76-
77-
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
78-
$package = $operation->getPackage();
79-
}
80-
81-
if (!isset($package)) {
82-
return null;
83-
}
84-
85-
return $this->generators->getProjectUrl($package);
86-
}
87-
8827
/**
8928
* @return string
9029
*/
@@ -96,7 +35,7 @@ protected function getDecoratedPackageName(DiffEntry $entry)
9635
return '';
9736
}
9837

99-
return $this->terminalLink($this->getProjectUrl($entry->getOperation()), $package->getName());
38+
return $this->terminalLink($entry->getProjectUrl($this->generators), $package->getName());
10039
}
10140

10241
/**

src/Formatter/Formatter.php

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

55
use IonBazan\ComposerDiff\Diff\DiffEntries;
6-
use IonBazan\ComposerDiff\Diff\DiffEntry;
76

87
interface Formatter
98
{
@@ -23,14 +22,4 @@ public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withU
2322
* @return void
2423
*/
2524
public function renderSingle(DiffEntries $entries, $title, $withUrls, $withLicenses);
26-
27-
/**
28-
* @return string|null
29-
*/
30-
public function getUrl(DiffEntry $entry);
31-
32-
/**
33-
* @return string|null
34-
*/
35-
public function getLicenses(DiffEntry $entry);
3625
}

src/Formatter/GitHubFormatter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ private function transformEntries(DiffEntries $entries, $withUrls, $withLicenses
5858
private function transformEntry(DiffEntry $entry, $withUrls, $withLicenses)
5959
{
6060
$operation = $entry->getOperation();
61-
$url = $withUrls ? $this->getUrl($entry) : null;
61+
$url = $withUrls ? $entry->getUrl($this->generators) : null;
6262
$url = (null !== $url) ? ' '.$url : '';
63-
$licenses = $withLicenses ? $this->getLicenses($entry) : null;
64-
$licenses = (null !== $licenses) ? ' (License: '.$licenses.')' : '';
63+
$licenses = $withLicenses ? implode(', ', $entry->getLicenses()) : '';
64+
$licenses = ('' !== $licenses) ? ' (License: '.$licenses.')' : '';
6565

6666
if ($operation instanceof InstallOperation) {
6767
return sprintf(

src/Formatter/JsonFormatter.php

Lines changed: 6 additions & 49 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

@@ -50,61 +47,21 @@ private function transformEntries(DiffEntries $entries, $withUrls, $withLicenses
5047
{
5148
$rows = array();
5249

50+
/** @var DiffEntry $entry */
5351
foreach ($entries as $entry) {
54-
$row = $this->transformEntry($entry);
52+
$row = $entry->toArray($this->generators);
5553

56-
if ($withUrls) {
57-
$row['compare'] = $this->getUrl($entry);
58-
$row['link'] = $this->getProjectUrl($entry->getOperation());
54+
if (!$withUrls) {
55+
unset($row['compare'], $row['link']);
5956
}
6057

61-
if ($withLicenses) {
62-
$row['license'] = $this->getLicenses($entry);
58+
if (!$withLicenses) {
59+
unset($row['licenses']);
6360
}
6461

6562
$rows[$row['name']] = $row;
6663
}
6764

6865
return $rows;
6966
}
70-
71-
/**
72-
* @return array<string, string|bool|null>
73-
*/
74-
private function transformEntry(DiffEntry $entry)
75-
{
76-
$operation = $entry->getOperation();
77-
78-
if ($operation instanceof InstallOperation) {
79-
return array(
80-
'name' => $operation->getPackage()->getName(),
81-
'direct' => $entry->isDirect(),
82-
'operation' => $entry->getType(),
83-
'version_base' => null,
84-
'version_target' => $operation->getPackage()->getFullPrettyVersion(),
85-
);
86-
}
87-
88-
if ($operation instanceof UpdateOperation) {
89-
return array(
90-
'name' => $operation->getInitialPackage()->getName(),
91-
'direct' => $entry->isDirect(),
92-
'operation' => $entry->getType(),
93-
'version_base' => $operation->getInitialPackage()->getFullPrettyVersion(),
94-
'version_target' => $operation->getTargetPackage()->getFullPrettyVersion(),
95-
);
96-
}
97-
98-
if ($operation instanceof UninstallOperation) {
99-
return array(
100-
'name' => $operation->getPackage()->getName(),
101-
'direct' => $entry->isDirect(),
102-
'operation' => $entry->getType(),
103-
'version_base' => $operation->getPackage()->getFullPrettyVersion(),
104-
'version_target' => null,
105-
);
106-
}
107-
108-
throw new \InvalidArgumentException('Invalid operation');
109-
}
11067
}

src/Formatter/MarkdownListFormatter.php

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

56-
if ($operation instanceof InstallOperation) {
57-
$packageName = $operation->getPackage()->getName();
58-
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
57+
$packageName = $package ? $package->getName() : null;
58+
$packageUrl = $withUrls ? $this->formatUrl($entry->getProjectUrl($this->generators), $packageName) : $packageName;
5959

60+
if ($operation instanceof InstallOperation) {
6061
return sprintf(
6162
' - Install <fg=green>%s</> (<fg=yellow>%s</>)%s%s',
6263
$packageUrl ?: $packageName,
@@ -67,13 +68,10 @@ private function getRow(DiffEntry $entry, $withUrls, $withLicenses)
6768
}
6869

6970
if ($operation instanceof UpdateOperation) {
70-
$packageName = $operation->getInitialPackage()->getName();
71-
$projectUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
72-
7371
return sprintf(
7472
' - %s <fg=green>%s</> (<fg=yellow>%s</> => <fg=yellow>%s</>)%s%s',
7573
ucfirst($entry->getType()),
76-
$projectUrl ?: $packageName,
74+
$packageUrl ?: $packageName,
7775
$operation->getInitialPackage()->getFullPrettyVersion(),
7876
$operation->getTargetPackage()->getFullPrettyVersion(),
7977
$url,
@@ -82,9 +80,6 @@ private function getRow(DiffEntry $entry, $withUrls, $withLicenses)
8280
}
8381

8482
if ($operation instanceof UninstallOperation) {
85-
$packageName = $operation->getPackage()->getName();
86-
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
87-
8883
return sprintf(
8984
' - Uninstall <fg=green>%s</> (<fg=yellow>%s</>)%s%s',
9085
$packageUrl ?: $packageName,

src/Formatter/MarkdownTableFormatter.php

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

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

4141
if ($withLicenses) {
42-
$row[] = $this->getLicenses($entry);
42+
$row[] = implode(', ', $entry->getLicenses());
4343
}
4444

4545
$rows[] = $row;
@@ -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($this->getProjectUrl($operation), $packageName) : $packageName;
72+
$packageUrl = $withUrls ? $this->formatUrl($entry->getProjectUrl($this->generators), $packageName) : $packageName;
7373

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

src/PackageDiff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public function loadPackagesFromArray(array $composerLock, $dev, $withPlatform)
110110
{
111111
$loader = new ArrayLoader();
112112
$packages = array();
113-
$packagesKey = 'packages' . ($dev ? '-dev' : '');
114-
$platformKey = 'platform' . ($dev ? '-dev' : '');
113+
$packagesKey = 'packages'.($dev ? '-dev' : '');
114+
$platformKey = 'platform'.($dev ? '-dev' : '');
115115

116116
if (isset($composerLock[$packagesKey])) {
117117
foreach ($composerLock[$packagesKey] as $packageInfo) {

0 commit comments

Comments
 (0)